pandas 日期时间到 unix 时间戳秒 [英] pandas datetime to unix timestamp seconds

查看:76
本文介绍了 pandas 日期时间到 unix 时间戳秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自pandas.to_datetime 我们可以说,

unit : 字符串,默认‘ns’

<块引用>

arg 的单位 (D,s,ms,us,ns) 表示单位,为整数或浮点数.这将基于原点.例如,与unit='ms' 和 origin='unix'(默认),这将计算Unix 纪元开始的毫秒数.

所以当我这样尝试时,

将pandas导入为pddf = pd.DataFrame({'time': [pd.to_datetime('2019-01-15 13:25:43')]})df_unix_sec = pd.to_datetime(df['time'],unit='ms',origin='unix')打印(df)打印(df_unix_sec)时间0 2019-01-15 13:25:430 2019-01-15 13:25:43名称:时间,数据类型:datetime64[ns]

后一个的输出没有改变.每次它显示日期时间值而不是第二个unix纪元开始的毫秒数.这是为什么?我错过了什么吗?

解决方案

我想你误解了争论的目的.origin='unix' 的目的是将整数时间戳转换为 datetime,而不是相反.

pd.to_datetime(1.547559e+09, unit='s', origin='unix')#时间戳('2019-01-15 13:30:00')

相反,您可以通过转换为整数(以获得纳秒)并除以 109 来获得时间戳.

pd.to_datetime(['2019-01-15 13:30:00']).astype(int)/10**9# Float64Index([1547559000.0], dtype='float64')


更新

Pandas 文档推荐使用以下方法:

#创建测试数据日期 = pd.to_datetime(['2019-01-15 13:30:00'])# 计算unix日期时间(日期 - pd.Timestamp("1970-01-01"))//pd.Timedelta('1s')[出去]:Int64Index([1547559000], dtype='int64')

没有上面显示的方法那么快,但这并没有假设 pandas 在内部如何存储其 datetime 对象.

From the official documentation of pandas.to_datetime we can say,

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.

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?

解决方案

I think you misunderstood what the argument is for. The purpose of origin='unix' is to convert an integer timestamp to datetime, not the other way.

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.

pd.to_datetime(['2019-01-15 13:30:00']).astype(int) / 10**9
# Float64Index([1547559000.0], dtype='float64')


Update

Pandas docs recommend using the following method:

# create test data
dates = pd.to_datetime(['2019-01-15 13:30:00'])

# calculate unix datetime
(dates - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')

[out]:
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 日期时间到 unix 时间戳秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆