Tom J Muthirenthi
Tom J Muthirenthi

Reputation: 3340

Convert float to YYYYMM Pandas

The date column that I have in the dataframe is of float type.

I was able to convert it in to YYYY-MM-DD format using the below:

pd.to_datetime(df['Date'], format = '%Y%m%d')

How can I convert it to YYYY-MM? tried the below and it didn't help.

pd.to_datetime(df['Date'], format = '%Y%m')

Upvotes: 0

Views: 479

Answers (1)

jezrael
jezrael

Reputation: 862831

I think you need strftime for string:

pd.to_datetime(df['Date'], format = '%Y%m%d').dt.strftime('%Y%m')

Or for month periods:

pd.to_datetime(df['Date'], format = '%Y%m%d').dt.to_period('m')

Upvotes: 2

Related Questions