如何生成 datetime64[ns, UTC] numpy 系列?
How do I generate a datetime64[ns, UTC] numpy series?
我正在尝试生成 datetime64[ns, UTC]
类型的 numpy 系列,但没有成功。
这是我试过的:
test = np.array(np.datetime64('2005-01-03 14:30:00.000000000'))
test
>array('2005-01-03T14:30:00.000000000', dtype='datetime64[ns]')
并进行转换,但没有成功:
test = np.array(np.datetime64('2005-01-03 14:30:00.000000000').tz_localize('UTC'))
test
>>>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-0efefc078d50> in <module>
----> 1 test = np.array(np.datetime64('2005-01-03 14:30:00.000000000').tz_localize('UTC'))
2 test
AttributeError: 'numpy.datetime64' object has no attribute 'tz_localize'
我能够达到这个结果:
0 2005-01-03 14:30:00+00:00
dtype: datetime64[ns, UTC]
使用下面的代码:
import numpy as np
import pandas as pd
s = pd.Series(np.datetime64('2005-01-03 14:30:00.000000000'))
s.dt.tz_localize('UTC')
我正在尝试生成 datetime64[ns, UTC]
类型的 numpy 系列,但没有成功。
这是我试过的:
test = np.array(np.datetime64('2005-01-03 14:30:00.000000000'))
test
>array('2005-01-03T14:30:00.000000000', dtype='datetime64[ns]')
并进行转换,但没有成功:
test = np.array(np.datetime64('2005-01-03 14:30:00.000000000').tz_localize('UTC'))
test
>>>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-0efefc078d50> in <module>
----> 1 test = np.array(np.datetime64('2005-01-03 14:30:00.000000000').tz_localize('UTC'))
2 test
AttributeError: 'numpy.datetime64' object has no attribute 'tz_localize'
我能够达到这个结果:
0 2005-01-03 14:30:00+00:00
dtype: datetime64[ns, UTC]
使用下面的代码:
import numpy as np
import pandas as pd
s = pd.Series(np.datetime64('2005-01-03 14:30:00.000000000'))
s.dt.tz_localize('UTC')