Fungie
Fungie

Reputation: 21

Convert pandas date object to integer of form YYYYMMDD

I have a date column in my pandas dataframe in the form of YYYY-MM-DD, I need to convert this to a integer in the form YYYYMMDD. I have been at it for a long time now and can't find a simple solution.

Upvotes: 2

Views: 5548

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

If your pandas column is datetime dtype then use datetime access .dt and strftime, if you are talking about a datetimeindex then you don't need the .dt:

df['Date'].dt.strftime('%Y%m%d')

Upvotes: 5

Rayhane Mama
Rayhane Mama

Reputation: 2424

you can try:

df['column_to_change']=df['column_to_change'].str.replace('-','').apply(int)

it will change all strings (or datetime) in your column column_to_change of you DataFrame df into format YYYYMMDD with the type integer as you can check with:

df.dtypes

Upvotes: 0

Related Questions