SM079
SM079

Reputation: 713

DataFrame: convert a date column to specific format

I have a date columns in a DF like "FY19 Jun". How to convert the data format to 2019-01-01

Upvotes: 0

Views: 50

Answers (1)

mozway
mozway

Reputation: 260290

IIUC, use:

df = pd.DataFrame({'date': ['FY19 Jun']})

df['converted'] = pd.to_datetime(df['date'], format='FY%y %b')

output:

       date  converted
0  FY19 Jun 2019-06-01

If you want the start of year, ignore the month:

df['converted'] = pd.to_datetime(df['date'].str.extract(r'FY(\d+)', expand=False), format='%y')

output:

       date  converted
0  FY19 Jun 2019-01-01

Upvotes: 3

Related Questions