2
我有一系列datetime64 [ns]对象。Python,将datetime64的系列转换为日期和分钟
我想提取日期和分钟(HH:MM)部分。到目前为止,我使用下面的代码,但它很慢。我怎么能更有效地做到这一点?
>>> type(df['EXECUTION_TIMESTAMP'])
Out[1]: pandas.core.series.Series
>>> df['EXECUTION_TIMESTAMP']
Out[1]:
0 2012-12-13 16:46:37
1 2012-12-13 16:46:42
2 2012-12-13 16:46:47
...
68 2010-09-07 15:21:38
69 2013-07-21 21:40:14
70 2010-07-21 22:44:46
Name: EXECUTION_TIMESTAMP, Length: 769552, dtype: datetime64[ns]
# Get the DateTimes Only
ets = pd.Series(df['EXECUTION_TIMESTAMP'])
print('Converting times')
dt_min = []
dd = []
for x in ets:
dt_min.append(pd.datetime(2000,1,1,x.hour,x.minute))
dd.append(pd.datetime(x.year,x.month,x.day))
感谢您的帮助。最后的zip命令给了我一分钟和二秒的元组列表。有没有办法让它返回日期时间的数据框呢? – Ginger
不确定你的最终目标是什么......但让我知道是否有帮助 – Jeff