pandas 日期时间到 unix 时间戳秒

编程入门 行业动态 更新时间:2024-10-25 02:22:40
本文介绍了 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 时间戳秒

本文发布于:2023-10-25 14:17:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   日期   pandas   unix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!