gpt4 book ai didi

python - 为什么我在 Pandas 的时间列中得到 '1900-01-01'

转载 作者:行者123 更新时间:2023-12-04 12:12:01 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Pandas - convert strings to time without date

(3 个回答)


4年前关闭。




我正在处理 pandas 中的数据帧.我想转换我的 time列成to_datetime , 我收到 1900-01-01 在我的时间之前。

一些样本数据,

  Date              hour
01-06-2013 23:00:00
02-06-2013 01:00:00
02-06-2013 21:00:00
02-06-2013 22:00:00
02-06-2013 23:00:00
03-06-2013 01:00:00
03-06-2013 21:00:00
03-06-2013 22:00:00
03-06-2013 23:00:00
04-06-2013 01:00:00

这是我试过的,
df['hour'] = pd.to_datetime(df['hour'],format ='%H:%M:%S', errors = 'coerce')

我越来越像这样
print(df['hour'])

0 1900-01-01 23:00:00
1 1900-01-01 01:00:00
2 1900-01-01 21:00:00
3 1900-01-01 00:00:00

我的 df 中不需要 1900-01-01 .为什么会发生这种情况,我该如何摆脱这种情况?

最佳答案

非常接近,只需将to_datetime()末尾的列设置为time

df['hour'] = pd.to_datetime(df.hour,format ='%H:%M:%S', errors = 'coerce').dt.time

Date hour
0 01-06-2013 23:00:00
1 02-06-2013 01:00:00
2 02-06-2013 21:00:00
3 02-06-2013 22:00:00
4 02-06-2013 23:00:00
5 03-06-2013 01:00:00
6 03-06-2013 21:00:00
7 03-06-2013 22:00:00
8 03-06-2013 23:00:00
9 04-06-2013 01:00:00

关于python - 为什么我在 Pandas 的时间列中得到 '1900-01-01',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45623799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com