Ram
Ram

Reputation: 290

Replace the year in pandas.datetime column

I have a dataframe with a date column converted using pd.to_datetime(). When I inspected the data I found few of these dates with year mentioned as 2216, which should have been 2016. Can you please help me change the year for these dates from 2216 to 2016

     Date
0   2216-12-21
1   2216-12-23
2   2216-01-31
3   2016-12-23
4   2216-12-27
5   2216-12-25
6   2016-12-23

I tried using str.replace

 df['Date'] = df['Date'].str.replace("2216","2016")

but got the following error

 Can only use .str accessor with string values, which use np.object_ dtype in pandas

Thanks In advance

Upvotes: 5

Views: 14623

Answers (1)

jezrael
jezrael

Reputation: 863731

Use:

df['Date'] = df['Date'].mask(df['Date'].dt.year == 2216, 
                             df['Date'] + pd.offsets.DateOffset(year=2016))
print (df)
        Date
0 2016-12-21
1 2016-12-23
2 2016-01-31
3 2016-12-23
4 2016-12-27
5 2016-12-25
6 2016-12-23

For better performance:

df['Date'] = df['Date'].mask(df['Date'].dt.year == 2216, df['Date'] - 
                                                         pd.to_timedelta(200, unit='y') + 
                                                         pd.to_timedelta(12, unit='h'))
print (df)
        Date
0 2016-12-21
1 2016-12-23
2 2016-01-31
3 2016-12-23
4 2016-12-27
5 2016-12-25
6 2016-12-23

Upvotes: 15

Related Questions