我遇到了一个错误。ValueError: 时间数据 "1922" 与格式 "%Y-%m-%d" 不匹配,位置3处
我一直在尝试运行下面的代码。
df_tracks.set_index('release_date' ,inplace=True) # inplace mutates the original df
df_tracks.index=pd.to_datetime(df_tracks.index)
df_tracks.head()
但是我遇到了这个错误:
ValueError: time data "1922" doesn't match format "%Y-%m-%d", at position 3. You might want to try:
- passing `format` if your strings have a consistent format;
- passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
- passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.
我不知道该怎么解决。有没有人能帮帮我?
1 个回答
1
看起来你的索引只有年份的值。所以你需要在你的 pd.to_datetime
函数中设置格式,对于你的情况来说,格式应该是 format = "Y"
。这样你就可以使用这个设置了。
df_tracks.index=pd.to_datetime(df_tracks.index, format='%Y')
这样会把 1992 转换成 1992-01-01