Python 使用pandas将parsedtime转换为时间戳

Python 使用pandas将parsedtime转换为时间戳,python,python-3.x,pandas,timestamp,Python,Python 3.x,Pandas,Timestamp,最近,我收到了一些带有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

最近,我收到了一些带有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
提供商表示时间单位为秒,但例如

1504279457970   

pd.to_datetime(1504279457970, infer_datetime_format=True)
Timestamp('1970-01-01 00:25:04.279457970')

他们说我做错了什么

我是Python新手,所以我不知道自己是否幼稚


谢谢

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

当您为该方法提供
inferdatetime\u 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')

您确定
1504279457970
没有丢失浮点吗?如果您进行手动转换,则是47700年,这就是为什么该错误被称为
OutofBounds
Thank you!:)这是ms,而不是s。
>>> 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')