pandas 如何将日期时间对象转换为毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33477604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert datetime object to milliseconds
提问by Klausos Klausos
I am parsing datetime values as follows:
我解析日期时间值如下:
df['actualDateTime'] = pd.to_datetime(df['actualDateTime'])
How can I convert this datetime objects to milliseconds?
如何将此日期时间对象转换为毫秒?
I didn't see mention of milliseconds in the doc of to_datetime.
我在to_datetime的文档中没有看到提到毫秒。
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).
更新(基于反馈):这是提供错误的代码的当前版本TypeError: Cannot convert input to Timestamp
。该列Date3
必须包含毫秒(作为日期时间对象的数字等效项)。
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
回答by notrai
You can try pd.to_datetime(df['actualDateTime'], unit='ms')
你可以试试 pd.to_datetime(df['actualDateTime'], unit='ms')
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html
says this will denote in epoch, with variations 's','ms', 'ns' ...
说这将在纪元中表示,带有变体“s”、“ms”、“ns”……
Update
更新
If you want in epoch timestamp of the form 14567899..
如果您想要格式为 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
回答by Mike Müller
Timestamps in pandas are always in nanoseconds.
pandas 中的时间戳总是以纳秒为单位。
This gives you milliseconds since the epoch (1970-01-01):
这为您提供了自纪元 (1970-01-01) 以来的毫秒数:
df['actualDateTime'] = df['actualDateTime'].astype(np.int64) / int(1e6)
回答by WoodChopper
pandas.to_datetime
is to convert string or few other datatype to pandas datetime[ns]
pandas.to_datetime
是将字符串或其他一些数据类型转换为Pandas datetime[ns]
In your instance initial 'actualDateTime'
is not having milliseconds
.So, if you are parsing a column which has milliseconds you will get data.
在您的实例中,initial'actualDateTime'
没有milliseconds
.So,如果您正在解析具有毫秒数的列,您将获得数据。
for example,
例如,
df
Out[60]:
a b
0 2015-11-02 18:04:32.926 0
1 2015-11-02 18:04:32.928 1
2 2015-11-02 18:04:32.927 2
df.a
Out[61]:
0 2015-11-02 18:04:32.926
1 2015-11-02 18:04:32.928
2 2015-11-02 18:04:32.927
Name: a, dtype: object
df.a = pd.to_datetime(df.a)
df.a
Out[63]:
0 2015-11-02 18:04:32.926
1 2015-11-02 18:04:32.928
2 2015-11-02 18:04:32.927
Name: a, dtype: datetime64[ns]
df.a.dt.nanosecond
Out[64]:
0 0
1 0
2 0
dtype: int64
df.a.dt.microsecond
Out[65]:
0 926000
1 928000
2 927000
dtype: int64
回答by Matt Widjaja
For what it's worth, to convert a single Pandas timestamp object tomilliseconds, I had to do:
对于它的价值,要将单个 Pandas 时间戳对象转换为毫秒,我必须执行以下操作:
import time
time.mktime(<timestamp_object>.timetuple())*1000
回答by Yannick
This will return milliseconds from epoch
这将从纪元返回毫秒
timestamp_object.timestamp() * 1000
timestamp_object.timestamp() * 1000
回答by Prashant Shukla
from datetime import datetime
print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
>>>> OUTPUT >>>>
2015-11-02 18:04:32.926