pandas 熊猫日期时间到 unix 时间戳秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54313463/
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
pandas datetime to unix timestamp seconds
提问by Always Sunny
From the official documentation of pandas.to_datetimewe can say,
从pandas.to_datetime的官方文档我们可以说,
unit : string, default ‘ns'
unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number. This will be based off the origin. Example, with unit='ms' and origin='unix' (the default), this would calculate the number of milliseconds to the unix epoch start.
参数的单位 (D,s,ms,us,ns) 表示单位,为整数或浮点数。这将基于原点。例如,使用 unit='ms' 和 origin='unix'(默认值),这将计算到 unix 纪元开始的毫秒数。
So when I try like this way,
所以当我像这样尝试时
import pandas as pd
df = pd.DataFrame({'time': [pd.to_datetime('2019-01-15 13:25:43')]})
df_unix_sec = pd.to_datetime(df['time'],unit='ms',origin='unix')
print(df)
print(df_unix_sec)
time
0 2019-01-15 13:25:43
0 2019-01-15 13:25:43
Name: time, dtype: datetime64[ns]
Output is not changing for the later one. Every time it is showing the datetime value not number of milliseconds to the unix epoch start for the 2nd one. Why is that? Am I missing something?
后一个的输出没有改变。每次它显示日期时间值而不是第二个unix纪元开始的毫秒数。这是为什么?我错过了什么吗?
回答by cs95
I think you misunderstood what the argument is for. The purpose of origin='unix'
is to convert an integer timestamp todatetime
, not the other way.
我想你误解了论证的目的。的目的origin='unix'
是将整数时间戳转换为datetime
,而不是其他方式。
pd.to_datetime(1.547559e+09, unit='s', origin='unix')
# Timestamp('2019-01-15 13:30:00')
Conversely, you can get the timestamp by converting to integer (to get nanoseconds) and divide by 109.
相反,您可以通过转换为整数(以获得纳秒)并除以 10 9来获得时间戳。
pd.to_datetime(['2019-01-15 13:30:00']).astype(int) / 10**9
# Float64Index([1547559000.0], dtype='float64')
Update
更新
Pandas docsrecommend using the following method:
Pandas 文档推荐使用以下方法:
dates = pd.to_datetime(['2019-01-15 13:30:00'])
(dates - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
# Int64Index([1547559000], dtype='int64')
Not as fast as the method shown above, but this makes no assumption about how pandas internally stores its datetime objects.
不如上面显示的方法快,但这并没有假设 pandas 如何在内部存储其 datetime 对象。