如何将日期时间对象转换为毫秒 [英] How to convert datetime object to milliseconds
问题描述
我正在解析日期时间值,如下所示:
I am parsing datetime values as follows:
df['actualDateTime'] = pd.to_datetime(df['actualDateTime'])
如何将日期时间对象转换为毫秒?
How can I convert this datetime objects to milliseconds?
更新(基于反馈):
这是提供错误<$的代码的当前版本c $ c> TypeError:无法将输入转换为时间戳。 Date3
列必须包含毫秒(与日期时间对象的数值等效)。
Update (Based on feedback):
This is the current version of the code that provides error TypeError: Cannot convert input to Timestamp
. The column Date3
must contain milliseconds (as a numeric equivalent of a datetime object).
import pandas as pd
import time
s1 = {'Date' : ['2015-10-20T07:21:00.000','2015-10-19T07:18:00.000','2015-10-19T07:15:00.000']}
df = pd.DataFrame(s1)
df['Date2'] = pd.to_datetime(df['Date'])
t = pd.Timestamp(df['Date2'])
df['Date3'] = time.mktime(t.timetuple())
print df
推荐答案
您可以尝试 pd.to_datetime(df ['actualDateTime'],unit ='ms')
http://pandas.pydata.org/pandas-docs /stable/generated/pandas.to_datetime.html
表示这将代表时代,并带有s','ms','ns'变体。 ..
says this will denote in epoch, with variations 's','ms', 'ns' ...
如果您希望以1456789格式的时代时间戳记9 ..
If you want in epoch timestamp of the form 14567899..
import pandas as pd
import time
t = pd.Timestamp('2015-10-19 07:22:00')
time.mktime(t.timetuple())
>> 1445219520.0
最新更新
Latest update
df = pd.DataFrame(s1)
df1 = pd.to_datetime(df['Date'])
pd.DatetimeIndex(df1)
>>>DatetimeIndex(['2015-10-20 07:21:00', '2015-10-19 07:18:00',
'2015-10-19 07:15:00'],
dtype='datetime64[ns]', freq=None)
df1.astype(np.int64)
>>>0 1445325660000000000
1 1445239080000000000
2 1445238900000000000
df1.astype(np.int64) // 10**9
>>>0 1445325660
1 1445239080
2 1445238900
Name: Date, dtype: int64
这篇关于如何将日期时间对象转换为毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!