numpy的recordarray与时间?

我正在尝试将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是一个列表,datadict并且dtype是http://stardict.sourceforge.net/Dictionaries.php下载)


GCT1015
浏览 260回答 1
1回答

largeQ

糟糕,在recarray中使用numpy datetime64弄清楚了我尝试使用datetime[D],但失败了:In [19]: dtypesOut[19]: {'altitude': 'float', 'distance': 'float', 'time': 'datetime64[D]'}In [20]: dataarr = np.array(zip(*[datadict[k] for k in keys]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtype=[(k,dtypes[k]) for k in keys])Traceback (most recent call last):&nbsp; File "<ipython-input-20-d59123796cfa>", line 2, in <module>&nbsp; &nbsp; 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]: dtypesOut[22]: {'altitude': 'float', 'distance': 'float', 'time': 'datetime64[s]'}In [23]: dataarr = np.array(zip(*[datadict[k] for k in keys]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtype=[(k,dtypes[k]) for k in keys])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python