gpt4 book ai didi

python - 将日期时间字段添加到重新数组中

转载 作者:太空宇宙 更新时间:2023-11-03 18:06:46 24 4
gpt4 key购买 nike

我正在尝试将日期时间字段 (datetime64) 附加到现有的记录中 - 但没有取得太大成功。我可以创建日期时间字段,但是当我尝试将其附加到记录数组时,出现错误:

ValueError:解析日期时间字符串“?”时出错在位置0

但是,如果我将数据转换为 int64,我可以毫无问题地以该格式添加它。 (代码如下所示)

有人知道为什么这不起作用吗?

(我的最终目标是将记录写入 netcdf 文件,因此考虑到该目标的适当日期时间格式也会有所帮助)

我使用的是 python 2.7.6.1,numpy 1.8.1

谢谢,罗布

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)

最佳答案

回溯是:

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

因此,此附加函数构造了一个掩码数组 (ma),并检查附加“dtype”的“fill_value”。显然 _check_fill_value 不理解 datetime dtype。看起来屏蔽数组和日期时间之间不兼容。可能需要一份 numpy 错误报告。

<小时/>

这是一个简单的、自己动手的附加:

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

我用联合数据类型构造了一个空数组。然后,我逐字段复制 dataarraymydatetime 中的数据。由于与 shape 相比,字段的数量通常相当小,因此此复制速度相当快。我非常确定 rf 函数具有相同的功能。

'f3' 是添加字段的默认名称。您可以在创建dt1时更改它。

结果是:

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]')])

将此newarray 转换为掩码数组会产生相同的_check_fill_value 错误。

np.ma.masked_array(newarray)

关于python - 将日期时间字段添加到重新数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26739733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com