FalloutATS21
FalloutATS21

Reputation: 65

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

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.

Upvotes: 1

Views: 377

Answers (2)

mozway
mozway

Reputation: 261900

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

Upvotes: 1

Naveed
Naveed

Reputation: 11650

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

Upvotes: 1

Related Questions