4 python time dataframe pandas
我正在处理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
Run Code Online (Sandbox Code Playgroud)
这是我试过的,
df['hour'] = pd.to_datetime(df['hour'],format ='%H:%M:%S', errors = 'coerce')
Run Code Online (Sandbox Code Playgroud)
我越来越像这样
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
Run Code Online (Sandbox Code Playgroud)
我不需要 1900-01-01 在我的df. 为什么会发生这种情况,我该如何摆脱这种情况?
非常接近,只需将列设置为末尾的时间 to_datetime()
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
Run Code Online (Sandbox Code Playgroud)