Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace the year in pandas.datetime column

Tags:

python

pandas

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

like image 446
Ram Avatar asked Jun 04 '18 05:06

Ram


1 Answers

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
like image 102
jezrael Avatar answered Oct 03 '22 02:10

jezrael