Home > OS >  Pandas: Date Format Changes from mmmyy to YYYY-MM-DD
Pandas: Date Format Changes from mmmyy to YYYY-MM-DD

Time:04-16

I have a data frame column in mmmyy format and would like to change in yyyy-mm-dd format.

Input :

         Date
         Aug21
         Sept21

Output:

         Date
        2021-08-01
        2021-09-01

tried: conv = lambda x: datetime.strptime(x, "%b %y")

dfi["Date"] = dfi["Date"].apply(conv)

CodePudding user response:

Sept doesn't match the %b format as only 3 letters are valid.

from datetime import datetime
datetime.strptime('Sept21', '%b%y')
# ValueError: time data 'Sept21' does not match format '%b%y'

datetime.strptime('Sep21', '%b%y')
# datetime.datetime(2021, 9, 1, 0, 0)

You could fix the string to keep only 3 letters:

pd.to_datetime(df['Date'].str.replace(r'(?<=...).*(?=..)', '', regex=True),
               format='%b%y')

or .replace(r'(?<=...)\D*', '', regex=True)

output:

0   2021-08-01
1   2021-09-01
Name: Date, dtype: datetime64[ns]
  • Related