Python 如何避免将np.datetime64添加到numpy数组时自动转换为datetime?
对于以下示例,当dtype为Python 如何避免将np.datetime64添加到numpy数组时自动转换为datetime?,python,numpy,Python,Numpy,对于以下示例,当dtype为np.datetime64的元素添加到另一个numpy数组时,它们会自动转换为datetime.datetime 如何避免这种自动转换 import numpy as np a = np.array([['2018-04-01T15:30:00'], ['2018-04-01T15:31:00'], ['2018-04-01T15:32:00'], ['2018-04-01T15:33:00'], ['2018-
np.datetime64
的元素添加到另一个numpy数组时,它们会自动转换为datetime.datetime
如何避免这种自动转换
import numpy as np
a = np.array([['2018-04-01T15:30:00'],
['2018-04-01T15:31:00'],
['2018-04-01T15:32:00'],
['2018-04-01T15:33:00'],
['2018-04-01T15:34:00']], dtype='datetime64[s]')
c = np.array([0,1,2,3,4]).reshape(-1,1)
c = c.astype("object")
d = np.append(c,a,axis=1)
d
有时,我们必须制作一个“空白”对象数组,并逐块填充它
In [57]: d = np.empty((5,2), object)
In [58]: d
Out[58]:
array([[None, None],
[None, None],
[None, None],
[None, None],
[None, None]], dtype=object)
我们可以按列填充它,但结果与串联
(不要使用np.append
):
与a.astype(object)
一样,它“取消”了日期
但是如果我一个接一个地分配元素:
In [62]: for i in range(5):
...: d[i,1]=a[i,0]
...:
In [63]: d
Out[63]:
array([[0, numpy.datetime64('2018-04-01T15:30:00')],
[1, numpy.datetime64('2018-04-01T15:31:00')],
[2, numpy.datetime64('2018-04-01T15:32:00')],
[3, numpy.datetime64('2018-04-01T15:33:00')],
[4, numpy.datetime64('2018-04-01T15:34:00')]], dtype=object)
但是这样一个数组的价值是什么呢
我可以向原始时间数组添加时间增量:
In [67]: a + np.array(10, 'timedelta64[m]')
Out[67]:
array([['2018-04-01T15:40:00'],
['2018-04-01T15:41:00'],
['2018-04-01T15:42:00'],
['2018-04-01T15:43:00'],
['2018-04-01T15:44:00']], dtype='datetime64[s]')
但我不能对对象数组列执行相同的操作:
In [68]: d[:,1] + np.array(10, 'timedelta64[m]')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-68-f82827d3d355> in <module>()
----> 1 d[:,1] + np.array(10, 'timedelta64[m]')
TypeError: ufunc add cannot use operands with types dtype('O') and dtype('<m8[m]')
有时,我们必须制作一个“空白”对象数组,并逐块填充它
In [57]: d = np.empty((5,2), object)
In [58]: d
Out[58]:
array([[None, None],
[None, None],
[None, None],
[None, None],
[None, None]], dtype=object)
我们可以按列填充它,但结果与串联
(不要使用np.append
):
与a.astype(object)
一样,它“取消”了日期
但是如果我一个接一个地分配元素:
In [62]: for i in range(5):
...: d[i,1]=a[i,0]
...:
In [63]: d
Out[63]:
array([[0, numpy.datetime64('2018-04-01T15:30:00')],
[1, numpy.datetime64('2018-04-01T15:31:00')],
[2, numpy.datetime64('2018-04-01T15:32:00')],
[3, numpy.datetime64('2018-04-01T15:33:00')],
[4, numpy.datetime64('2018-04-01T15:34:00')]], dtype=object)
但是这样一个数组的价值是什么呢
我可以向原始时间数组添加时间增量:
In [67]: a + np.array(10, 'timedelta64[m]')
Out[67]:
array([['2018-04-01T15:40:00'],
['2018-04-01T15:41:00'],
['2018-04-01T15:42:00'],
['2018-04-01T15:43:00'],
['2018-04-01T15:44:00']], dtype='datetime64[s]')
但我不能对对象数组列执行相同的操作:
In [68]: d[:,1] + np.array(10, 'timedelta64[m]')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-68-f82827d3d355> in <module>()
----> 1 d[:,1] + np.array(10, 'timedelta64[m]')
TypeError: ufunc add cannot use operands with types dtype('O') and dtype('<m8[m]')
使用记录数组而不是dtype=object
通过构造一个可以正确处理不同类型列的数组来修复此问题。最简单的方法是做一个
rarr = np.rec.fromarrays([a, c], names=('date', 'val'))
print(rarr)
# output
# rec.array([[('2018-04-01T15:30:00', 0)],
# [('2018-04-01T15:31:00', 1)],
# [('2018-04-01T15:32:00', 2)],
# [('2018-04-01T15:33:00', 3)],
# [('2018-04-01T15:34:00', 4)]],
# dtype=[('date', '<M8[s]'), ('val', '<i8')])
print(rarr.date)
# output
# array([['2018-04-01T15:30:00'],
# ['2018-04-01T15:31:00'],
# ['2018-04-01T15:32:00'],
# ['2018-04-01T15:33:00'],
# ['2018-04-01T15:34:00']], dtype='datetime64[s]')
使用记录数组而不是dtype=object
通过构造一个可以正确处理不同类型列的数组来修复此问题。最简单的方法是做一个
rarr = np.rec.fromarrays([a, c], names=('date', 'val'))
print(rarr)
# output
# rec.array([[('2018-04-01T15:30:00', 0)],
# [('2018-04-01T15:31:00', 1)],
# [('2018-04-01T15:32:00', 2)],
# [('2018-04-01T15:33:00', 3)],
# [('2018-04-01T15:34:00', 4)]],
# dtype=[('date', '<M8[s]'), ('val', '<i8')])
print(rarr.date)
# output
# array([['2018-04-01T15:30:00'],
# ['2018-04-01T15:31:00'],
# ['2018-04-01T15:32:00'],
# ['2018-04-01T15:33:00'],
# ['2018-04-01T15:34:00']], dtype='datetime64[s]')
追加前的
c
是什么a.tolist()
生成datetime
对象astype(object)
可能正在执行相同的操作。为什么要尝试加入这些数组?你打算对结果做什么?不管你怎么做,它不是一个标准的numpy数组。我刚刚检查过x.astype(对象)
“取消装箱”所有内容np.float64
数字变成float
<代码>关于“您为什么尝试加入这些阵列?”-对于我达到pandas性能极限的情况,这应该是pandas的外行替代方案。现在我明白了我必须用numpy做不同的事情:-)追加之前的c
是什么a.tolist()
生成datetime
对象astype(object)
可能正在执行相同的操作。为什么要尝试加入这些数组?你打算对结果做什么?不管你怎么做,它不是一个标准的numpy数组。我刚刚检查过x.astype(对象)
“取消装箱”所有内容np.float64
数字变成float
<代码>关于“您为什么尝试加入这些阵列?”-对于我达到pandas性能极限的情况,这应该是pandas的外行替代方案。现在我知道我必须用numpy做不同的事情:-)