Unix日期时间对话上的熊猫错误-OutOfBoundsDatetime:无法将输入转换为单位's'

时间:2019-06-20 02:40:18

标签: python pandas

我收到此错误

  File "pandas/_libs/tslib.pyx", line 356, in pandas._libs.tslib.array_with_unit_to_datetime
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: cannot convert input with unit 's'

当尝试将pandas列转换为datetime格式时。

我检查了这个答案Convert unix time to readable date in pandas dataframe

但是它并没有帮助我解决问题。

github上的一个问题似乎已经关闭,但与此同时人们不断报告问题: https://github.com/pandas-dev/pandas/issues/10987

Dataframe列具有Unix时间格式,此处是前20行的打印内容

0      1420096800
1      1420096800
2      1420097100
3      1420097100
4      1420097400
5      1420097400
6      1420093800
7      1420097700
8      1420097700
9      1420098000
10     1420098480
11     1420098600
12     1420099200
13     1420099500
14     1420099500
15     1420100100
16     1420100400
17     1420096800
18     1420100700
19     1420100820
20     1420101840

关于如何解决的任何想法?

我尝试将单位从s更改为ms,但这没有帮助。

pd.__version__
'0.24.2'

df[key] = pd.to_datetime(df[key], unit='s')

1 个答案:

答案 0 :(得分:2)

如果您添加origin='unix'参数,它将起作用:

pd.to_datetime(df['date'], origin='unix', unit='s')

0    2015-01-01 07:20:00
1    2015-01-01 07:20:00
2    2015-01-01 07:25:00
3    2015-01-01 07:25:00
4    2015-01-01 07:30:00