与时俱进的numpy recordarray?

时间:2013-06-09 21:46:55

标签: python arrays numpy recarray

我正在尝试将XML文件读入NumPy记录数组。时间在祖鲁时间u'2013-06-06T17:47:38Z',其他列是floats

时间和floats都可以转换为NumPy数组。但是,如果我尝试创建recordarray,它会以各种方式失败(这可能表明我不知道如何创建记录数组):

In [124]: dataarr = np.array(zip(*[datadict[k] for k in keys]),
   .....:                     dtype=[(k,dtypes[k]) for k in keys])
Traceback (most recent call last):
  File "<ipython-input-124-d59123796cfa>", line 2, in <module>
    dtype=[(k,dtypes[k]) for k in keys])
ValueError: Cannot create a NumPy datetime other than NaT with generic units

In [125]: dataarr = np.array([datadict[k] for k in keys],
                    dtype=[(k,dtypes[k]) for k in keys])
Traceback (most recent call last):
  File "<ipython-input-125-ee9077bf1961>", line 2, in <module>
    dtype=[(k,dtypes[k]) for k in keys])
TypeError: expected a readable buffer object

In [126]: dataarr = np.array([datadict[k] for k in keys],
                    dtype=[dtypes[k] for k in keys])
Traceback (most recent call last):
  File "<ipython-input-126-a456052bdfd4>", line 2, in <module>
    dtype=[dtypes[k] for k in keys])
TypeError: data type not understood

In [127]: dtypes
Out[127]: {'altitude': 'float', 'distance': 'float', 'time': 'datetime64'}

创建包含时间的recordarray的正确方法是什么?

keys是一个列表,datadictdtype是dicts)

1 个答案:

答案 0 :(得分:1)

糟糕,使用numpy datetime64 in recarray

计算出来

我尝试使用失败的datetime[D]

In [19]: dtypes
Out[19]: {'altitude': 'float', 'distance': 'float', 'time': 'datetime64[D]'}

In [20]: dataarr = np.array(zip(*[datadict[k] for k in keys]),
                    dtype=[(k,dtypes[k]) for k in keys])
Traceback (most recent call last):
  File "<ipython-input-20-d59123796cfa>", line 2, in <module>
    dtype=[(k,dtypes[k]) for k in keys])
TypeError: Cannot cast NumPy timedelta64 scalar from metadata [s] to [D] according to the rule 'same_kind'

datetime[s]有效:

In [22]: dtypes
Out[22]: {'altitude': 'float', 'distance': 'float', 'time': 'datetime64[s]'}

In [23]: dataarr = np.array(zip(*[datadict[k] for k in keys]),
                    dtype=[(k,dtypes[k]) for k in keys])