【发布时间】:2020-06-05 09:36:55
【问题描述】:
我想要一个所有列都采用datetime.time 格式的数据框。但我原来的数据框是这样的
Moorabbin Mordialloc Aspendale Edithvale Chelsea
0 04:48:00 05:00:00 05:05:00 05:10:00 05:15:00
1 06:45:00 06:57:00 07:02:00 07:07:00 07:12:00
2 1900-01-01 00:48:00 NaN 1900-01-01 01:03:00 1900-01-01 01:08:00 1900-01-01 01:13:00
3 05:09:00 NaN NaN 05:36:00 05:41:00
我想得到的是
Moorabbin Mordialloc Aspendale Edithvale Chelsea
0 04:48:00 05:00:00 05:05:00 05:10:00 05:15:00
1 06:45:00 06:57:00 07:02:00 07:07:00 07:12:00
2 00:48:00 NaN 01:03:00 01:08:00 01:13:00
3 05:09:00 NaN NaN 05:36:00 05:41:00
这些值的数据类型是
> type(test_result.iloc[0,0])
datetime.time
> type(test_result.iloc[2,0])
pandas._libs.tslibs.timestamps.Timestamp
我尝试了to_datetime(format= "%H:%M:%S", error = "coerce")、datetime.strptime(test_result['Moorabbin'],"%H:%M:%S").time() 和test_result.astype('datetime64[ns]', copy=True, errors='ignore'),但没有任何效果。有人可以帮忙吗?
【问题讨论】:
标签: python pandas dataframe datetime timestamp