简体   繁体   English

将日期时间字段添加到RecArray

[英]Adding datetime field to recarray

I am trying to append a date-time field (datetime64) to an existing recarray - without much success. 我试图将日期时间字段(datetime64)附加到现有的Recarray-没有太大的成功。 I can create the datetime field, but when I attempt to append it to the record array I get the error: 我可以创建datetime字段,但是当我尝试将其追加到记录数组时,出现错误:

ValueError: Error parsing datetime string "?" ValueError:解析日期时间字符串“?”时出错 at position 0 在位置0

However, if I cast the data as int64 I can add it in that format without problem. 但是,如果将数据强制转换为int64,则可以毫无问题地以该格式添加数据。 (code shown below) (下面显示的代码)

Anyone know why this does not work? 有人知道为什么这行不通吗?

(my ultimate goal is to write the recarray to a netcdf file, so an appropriate datetime format with that goal in mind would also be helpful) (我的最终目标是将recarray写入netcdf文件,因此牢记该目标的适当日期时间格式也将有所帮助)

I am using python 2.7.6.1, numpy 1.8.1 我正在使用python 2.7.6.1,numpy 1.8.1

Thanks, Rob 谢谢,罗伯

import numpy as np
import numpy.lib.recfunctions as rf

# ----- make a recarray ---------    
dummy = np.arange(0,10)
datarray = np.core.records.fromarrays([dummy,dummy,dummy],names='a,b,c')

# ----- make some time data using datetime64 ---------
sec = np.arange(0,10)*1000
millisec = np.arange(0,10) 
mytime = sec + millisec

mytime64 = mytime.astype('timedelta64[ms]')         
basetime =  np.datetime64('1990-01-01') 
mydatetime = mytime64+basetime  

# ----- convert time data to int64 ---------
idatetime = mydatetime.astype('int64');

#------ try and append to recarray ---------
#  this works
datarray = rf.append_fields(datarray, 'iDateTime', data=idatetime)
# this doesnt
datarray = rf.append_fields(datarray, 'DateTime', data=mydatetime)

The traceback is: 追溯为:

Traceback (most recent call last):
  File "stack26739733.py", line 30, in <module>
    datarray = rf.append_fields(datarray, 'DateTime', data=mydatetime, usemask=False, dtypes=mydatetime.dtype)
  File "/usr/local/lib/python2.7/site-packages/numpy/lib/recfunctions.py", line 641, in append_fields
    dtype=base.dtype.descr + data.dtype.descr)
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/extras.py", line 163, in masked_all
    mask=np.ones(shape, make_mask_descr(dtype)))
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line 2644, in __new__
    _data = ndarray.view(_data, cls)
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line 2800, in __array_finalize__
    self._fill_value = _check_fill_value(None, self.dtype)
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line 402, in _check_fill_value
    dtype=ndtype,)
ValueError: Error parsing datetime string "?" at position 0

So this append function constructs a masked array ( ma ), and checks the 'fill_value' for the appended 'dtype'. 因此,此append函数构造了一个掩码数组( ma ),并为附加的'dtype'检查了'fill_value'。 Apparently _check_fill_value doesn't understand the datetime dtype. 显然_check_fill_value不了解datetime dtype。 Looks like an incompatibility between masked array and datetime. 看起来好像是掩码数组和日期时间之间的不兼容。 A numpy bug report might be in order. 可能有一些numpy错误报告。


Here's a simple, do-it-yourself append: 这是一个简单的自己动手的附件:

dt1 = np.dtype(datarray.dtype.descr + mydatetime.dtype.descr)
newarray = np.empty(datarray.shape, dtype=dt1)
for n in datarray.dtype.names:
    newarray[n] = datarray[n]
newarray['f3'] = mydatetime

I construct an empty array with a union dtype. 我用联合dtype构造一个空数组。 Then I copy the data from both datarray and mydatetime field by field. 然后,我mydatetime字段地同时从datarraymydatetime复制数据。 Since the number of fields is normally quite small compared to the shape , this copy is quite fast. 由于与shape相比,字段数通常很小,因此此副本非常快。 I'm pretty sure the rf function does the same. 我很确定rf函数的作用相同。

'f3' is the default name of the added field. 'f3'是添加字段的默认名称。 You can change that when creating dt1 . 您可以在创建dt1时更改它。

The result is: 结果是:

array([(0, 0, 0, datetime.datetime(1990, 1, 1, 0, 0)),
       (1, 1, 1, datetime.datetime(1990, 1, 1, 0, 0, 1, 1000)),
       (2, 2, 2, datetime.datetime(1990, 1, 1, 0, 0, 2, 2000)),
       ...
       (9, 9, 9, datetime.datetime(1990, 1, 1, 0, 0, 9, 9000))], 
      dtype=[('a', '<i4'), ('b', '<i4'), ('c', '<i4'), ('f3', '<M8[ms]')])

Turning this newarray into a masked array produces the same _check_fill_value error. 将此newarray数组转换为掩码数组会产生相同的_check_fill_value错误。

np.ma.masked_array(newarray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM