I am getting an error. ValueError: time data "1922" doesn't match format "%Y-%m-%d", at position 3

23 Views Asked by At

I have been trying to run the below code.

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()

But I am getting error as:

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.

Not sure how to resolve it. Can anyone please help?

1

There are 1 best solutions below

0
Muhammed Samed Özmen On

Looks like your index has only year values. So you should set format in your pd.to_datetime which for your case format = "Y". So, you can use that.

df_tracks.index=pd.to_datetime(df_tracks.index, format='%Y')

It will convert 1992 to 1992-01-01