Home > Blockchain >  Change datatype of a column that strictly only holds year in the format yyyy
Change datatype of a column that strictly only holds year in the format yyyy

Time:10-25

df.assign(Year=pd.to_datetime(df.Year, format='%Y')).set_index('Year')

Consider the df['Year'] has n rows with data listed in YYYY format. How can I change this to date-time format without adding month and day, the above code converts YYYY to 2015-01-01.

CodePudding user response:

You might be looking for a Period:

df.assign(year=pd.to_datetime(df['Year'],format='%Y').dt.to_period('Y')).set_index('year')

Or with PeriodIndex:

df.assign(year=pd.PeriodIndex(df['Year'], freq='Y')).set_index('year')

CodePudding user response:

extract the year using dt

# Year with capital Y is column in DF
# year with small letter y is a calculated year and is index

df=df.assign(year=pd.to_datetime(df['Year'],format='%Y').dt.year).set_index('year')
        Year    height
year        
2014    2014    175
2014    2014    180
2014    2014    160
  • Related