[英]Python converting index to datetime and ValueError: ('String does not contain a date:', ' ')
我将一列转换为索引,现在我尝试将其转换为日期时间索引。 但是得到了错误。
我的代码:
df.set_index('Time',inplace=True) % This worked perfectly
df.index =
ndex(['5/7/2020 7:01', '5/7/2020 7:02', '5/7/2020 7:03', '5/7/2020 7:04',
'5/7/2020 7:05', '5/7/2020 7:06', '5/7/2020 7:07', '5/7/2020 7:08',
'5/7/2020 7:09', '5/7/2020 7:10',
...
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
dtype='object', name='Time', length=1441)
df.index = pd.to_datetime(df.index)
目前output:
ValueError: ('String does not contain a date:', ' ')
如果您想省略无法转换为 DateTime 对象的值,您可以在 function 中使用参数errors='coerce'
,这将为可能引发错误的值返回NaT
。 此外,您应该添加格式,或者使用infer_datetime_format=True
,这也可能会节省一些问题。 总而言之,您应该尝试:
df.index = pd.to_datetime(df.index,infer_datetime_format=True,errors='coerce')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.