使用pandas将时间解析为时间戳

2024-09-21 03:20:26 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我收到了一些epoch timeparse的数据。在使用pandas进行时间戳后,我注意到返回的年份是1970年,但数据来自于2018年左右的视频游戏统计数据。在

我试过了

df['date'] = pd.to_datetime(df.creationTime, inferdatetime_format=True)

df['date'].describe()

count 51490
unique 51052
top 1970-01-01 00:25:04.380431622
freq 3
first 1970-01-01 00:24:56.891694922
last 1970-01-01 00:25:04.707332198
Name: date, dtype: object

提供者说时间单位是秒,但是,例如

^{pr2}$

以及

pd.to_datetime(1504279457970, unit = 's')
...
OutOfBoundsDatetime: cannot convert input with unit 's'

他们做错什么了?在

我是Python新手,所以我不知道我是不是太天真了。在

谢谢!在


Tags: to数据游戏pandasdfdatetimedate视频
1条回答
网友
1楼 · 发布于 2024-09-21 03:20:26

很可能时间戳是以毫秒精度给你的。如您所示,尝试使用第二精度将时间戳转换为datetime会导致OutOfBoundsDatetime错误。如果你假设时间戳的精度是毫秒,那么你得到的日期可能是2017年。在

当您向方法提供inferdatetime_format=True参数时,pandas似乎在猜测您使用的是纳秒级的精确时间戳。在

>>> pd.to_datetime(1504279457970, unit = 's')
Traceback (most recent call last):
  ...
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: cannot convert input with unit 's'
>>> pd.to_datetime(1504279457970, unit = 'ms')
Timestamp('2017-09-01 15:24:17.970000')
>>> pd.to_datetime(1504279457970, unit = 'ns')
Timestamp('1970-01-01 00:25:04.279457970')

相关问题 更多 >

    热门问题