1

I've got a DataFrame that has dates such as Nov-28 and Jan-12. Now the date such as Nov-28 is for this year (2020) and the other dates are for next (2021).

I'm wondering if there's a way to determine if a date is less than today to add next years year to it?

James George Dunn
  • 523
  • 1
  • 6
  • 16
  • 1
    Can you share the DataFrame? Is the date a string, or a datetime object? More info is required to give a proper answer. Pleaase follow https://stackoverflow.com/help/minimal-reproducible-example and this https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – alec_djinn Nov 24 '20 at 14:09

1 Answers1

1

Idea is first add actual year to to_datetime and then test if greater like now add one year by offsets.DateOffset:

df = pd.DataFrame({'date':['Nov-28','Jan-12']})

now = pd.Timestamp('now')

df['date'] = pd.to_datetime(str(now.year) + df['date'], format='%Y%b-%d')

df.loc[df['date'] > now, 'date'] += pd.offsets.DateOffset(years=1)
print (df)
        date
0 2021-11-28
1 2020-01-12
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252