我收到错误。 ValueError:时间数据“1922”与位置 3 处的格式“%Y-%m-%d”不匹配

问题描述 投票:0回答:1

我一直在尝试运行下面的代码。

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.

不知道如何解决。有人可以帮忙吗?

python valueerror
1个回答
0
投票

看起来您的索引只有年份值。你可以用它。

df_tracks.index=pd.to_datetime(df_tracks.index, format='%Y')
© www.soinside.com 2019 - 2024. All rights reserved.