pandas datetime to unix timestamp seconds
从pandas.to_datetime的官方文档中,我们可以说,
1 | 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.
所以当我这样尝试时
1 2 3 4 5 6 7 8 9 10 | 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] |
下一个输出不变。 每次显示日期时间值,而不是第二个unix纪元开始的毫秒数。 这是为什么? 我想念什么吗?
我认为您误解了争论的目的。
1 2 | pd.to_datetime(1.547559e+09, unit='s', origin='unix') # Timestamp('2019-01-15 13:30:00') |
相反,您可以通过将其转换为整数(以获取纳秒数)并除以109来获得时间戳记。
1 2 | pd.to_datetime(['2019-01-15 13:30:00']).astype(int) / 10**9 # Float64Index([1547559000.0], dtype='float64') |