【发布时间】:2020-01-11 22:16:30
【问题描述】:
假设我在名为 df 的数据帧中有以下时间戳
time
1 2019-05-03 15:26:37.000
2 2019-05-10 19:26:29.000
3 2019-05-10 23:39:07.000
4 2019-05-08 13:52:08.000
我打算把它转换成
time
1 2019-05-03
2 2019-05-10
3 2019-05-10
4 2019-05-08
这样我就可以分组计算每天有多少个日期点
df2=pd.to_datetime(df['time'], format='%d-%b-%y')
返回以下错误
ValueError: time data '2019-05-04 14:08:33.000' does not match format '%d-%b-%y' (match)
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
如果我这样做:
request_time_date_df2=pd.to_datetime(ride_df['requested_time'], unit='D')
返回
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
pandas/_libs/tslib.pyx in pandas._libs.tslib.array_with_unit_to_datetime()
ValueError: could not convert string to float: '2019-05-04 14:08:33.000'
如果我这样做
df2=pd.to_datetime(df['time'], format='%d-%b-%y', errors='ignore')
返回相同的
time
1 2019-05-03 15:26:37.000
2 2019-05-10 19:26:29.000
3 2019-05-10 23:39:07.000
4 2019-05-08 13:52:08.000
有什么想法吗?谢谢!
【问题讨论】:
标签: python-3.x pandas datetime