Pandas: converting int64 years to datetime dsiplaying only the year

Viewed 2353

I have a dataframe index that looks like this:

df.index
Int64Index([1992, 1993, 1994, 1995], dtype='int64')

but would like it converted to datetime whilst only displaying the year thereby preserving the above yearly format.

I have tried: Pandas: how to convert an index of int64 years to datetime

import pandas as pd
df.index = pd.to_datetime(df.index, format='%Y')

but this gives me:

DatetimeIndex(['1992-01-01', '1993-01-01', '1994-01-01', '1995-01-01'], dtype='datetime64[ns]', freq=None)

Instead is there a better way I can get something along the lines of:

DatetimeIndex(['1992', '1993', '1994', '1995'], dtype='datetime64)
3 Answers

I found this to be useful

df.index = pd.to_datetime(df.index, format='%Y').year
Related