0

I have a dataframe and one of the columns is called 'Step-up date' which contains objects and it looks like:

 |Step-up date
0|Mar-24
1|Jul-22
2|Feb-21
etc...

Mar-24 represents March 2024, so i was wondering if there is a way to convert that column into a date, so Mar-24 would be 2024-03-01 (yyyy-mm-dd) and just have it automatically become the 1st of the month for each value to make it easier

2 Answers2

1

Because no day specified, pandas add always day=1, for correct parsing use %b-%y for format first 3 letters of months names and last 2 digits of year in to_datetime:

df['Step-up date'] = pd.to_datetime(df['Step-up date'], format='%b-%y')
print (df)
  Step-up date
0   2024-03-01
1   2022-07-01
2   2021-02-01
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

First add 20 before the year, then use pd.to_datetime:

df['Step-up date'] = df['Step-up date'].apply(lambda x: 
                                               pd.to_datetime(x.replace('-', '-20')))
Bruno Mello
  • 4,448
  • 1
  • 9
  • 39