numpy 错误中的屏蔽数组

发布于 2024-11-02 01:15:43 字数 1595 浏览 3 评论 0 原文

我使用 genfromtxt 输入一个文件,但缺少一些值,因此我生成了一个掩码数组。当我尝试对屏蔽数组的记录的某些值进行索引时,我收到一个我无法弄清楚的错误。任何帮助将不胜感激。谢谢。 --Alex

import csv
import datetime
import time
import numpy as np
import numpy.lib.recfunctions as rf
import pprint
import numpy.ma as ma

date_converter = lambda x: datetime.date(int(x[-4:]), int(x[3:5]), int(x[:2]))
input_file = np.genfromtxt("../data/test.csv", usemask=True, converters={0:date_converter}, dtype="O4, i8, i8, i8, i8", names="date, firm, val1, val2, val3", delimiter=",", skip_header=1)

生成:

masked_array(data = [(datetime.date(2001, 3, 1), 1L, --, 14L, 15L)
 (datetime.date(2001, 2, 1), 1L, 10L, 11L, 12L)
 (datetime.date(2001, 5, 1), 1L, 19L, 20L, 21L)
 (datetime.date(2001, 4, 1), 1L, 16L, --, 18L)],
             mask = [(False, False, True, False, False) (False, False, False, False, False)
 (False, False, False, False, False) (False, False, False, True, False)],
       fill_value = ('?', 999999L, 999999L, 999999L, 999999L),
            dtype = [('date', '|O4'), ('firm', '<i8'), ('val1', '<i8'), ('val2', '<i8'), ('val3', '<i8')])

当我运行 input_file[0] 时,出现以下错误:

Traceback (most recent call last):
  File "<pyshell#278>", line 1, in <module>
    input_file[0]
  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 2956, in __getitem__
    dout = mvoid(dout, mask=mask)
  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 5529, in __new__
    _data[()] = data
ValueError: Setting void-array with object members using buffer.

I input a file using genfromtxt and some of the values are missing so I generate a masked array. When I try to index some of the values of the records of the masked array I get an error which I cannot figure out. Any help would be highly appreciated. Thanks. --Alex

import csv
import datetime
import time
import numpy as np
import numpy.lib.recfunctions as rf
import pprint
import numpy.ma as ma

date_converter = lambda x: datetime.date(int(x[-4:]), int(x[3:5]), int(x[:2]))
input_file = np.genfromtxt("../data/test.csv", usemask=True, converters={0:date_converter}, dtype="O4, i8, i8, i8, i8", names="date, firm, val1, val2, val3", delimiter=",", skip_header=1)

Generates:

masked_array(data = [(datetime.date(2001, 3, 1), 1L, --, 14L, 15L)
 (datetime.date(2001, 2, 1), 1L, 10L, 11L, 12L)
 (datetime.date(2001, 5, 1), 1L, 19L, 20L, 21L)
 (datetime.date(2001, 4, 1), 1L, 16L, --, 18L)],
             mask = [(False, False, True, False, False) (False, False, False, False, False)
 (False, False, False, False, False) (False, False, False, True, False)],
       fill_value = ('?', 999999L, 999999L, 999999L, 999999L),
            dtype = [('date', '|O4'), ('firm', '<i8'), ('val1', '<i8'), ('val2', '<i8'), ('val3', '<i8')])

When I run input_file[0] I get the following error:

Traceback (most recent call last):
  File "<pyshell#278>", line 1, in <module>
    input_file[0]
  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 2956, in __getitem__
    dout = mvoid(dout, mask=mask)
  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 5529, in __new__
    _data[()] = data
ValueError: Setting void-array with object members using buffer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-11-09 01:15:43

input_file[0] 不是访问屏蔽数组中数据的正确方法(请参阅 文档

例如:

>>> import numpy as np
>>> arr = np.ma.ones(3, dtype=[('c1', np.int),('c2', np.int)])
>>> arr.mask[0][1] = True
>>> arr.data[0][0] = 2              
>>> np.ma.getdata(arr)[1][0] = 3    
>>> arr.data[2][0] = 4       
>>> print(arr)
   [(2, --) (3, 1) (4, 1)]

input_file[0] is not the right way to access the data in a masked array (see documentation)

for example:

>>> import numpy as np
>>> arr = np.ma.ones(3, dtype=[('c1', np.int),('c2', np.int)])
>>> arr.mask[0][1] = True
>>> arr.data[0][0] = 2              
>>> np.ma.getdata(arr)[1][0] = 3    
>>> arr.data[2][0] = 4       
>>> print(arr)
   [(2, --) (3, 1) (4, 1)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文