Reputation: 3041
I have a dataframe like this,
ID DateCol
1 26/06/2017
2 Employee Not Found
I want to increase the date by 1.
The expected output is,
ID DateCol
1 27/06/2017
2 Employee Not Found
I tried,
temp_result_df['NewDateCol'] = pd.to_datetime(temp_result_df['DateCol']).apply(pd.DateOffset(1))
And it is not working, as I believe there is a string on it.
Upvotes: 0
Views: 108
Reputation: 862441
Here is best working with datetimes
in column with NaT
for missing values - use to_datetime
with Timedelta
or Day
offset:
temp_result_df['NewDateCol'] = pd.to_datetime(temp_result_df['DateCol'], errors='coerce')
temp_result_df['NewDateCol'] += pd.Timedelta(1, 'd')
#alternative
#temp_result_df['NewDateCol'] += pd.offsets.Day(1)
print (temp_result_df)
ID DateCol NewDateCol
0 1 26/06/2017 2017-06-27
1 2 Employee Not Found NaT
If need strings like original data need strftime
with replace
:
s = pd.to_datetime(temp_result_df['DateCol'], errors='coerce') + pd.Timedelta(1, 'd')
temp_result_df['NewDateCol'] = s.dt.strftime('%d/%m/%Y').replace('NaT','Employee Not Found')
print (temp_result_df)
ID DateCol NewDateCol
0 1 26/06/2017 27/06/2017
1 2 Employee Not Found Employee Not Found
Upvotes: 2