Bishu
Bishu

Reputation: 57

How can I identify date columns and format it to YYYY-MM-DD in Python using pandas?

I am trying to format the dates in Python using Pandas. Basically, I want to identify all the date columns and convert it to YYYY-MM-DD format, overwrite and save it.

Input:

ID NPC_code Date1 Date2 Date3 Date4
1 10001 10-01-2020 11012019 27-Jan-18 27Jan2016
2 10002 11-01-2020 11012020 28-Jan-18 27Jan2017
3 10003 12-01-2020 11012021 29-Jan-18 27Jan2018
4 10004 13-01-2020 11012022 30-Jan-18 27Jan2019
5 10005 14-01-2020 11012023 31-Jan-18 27Jan2020

Output:

ID NPC_code Date1 Date2 Date3 Date4
1 10001 2020-01-10 2019-01-11 2018-01-27 2016-01-27
2 10002 2020-01-11 2020-01-11 2018-01-28 2016-01-28
3 10003 2020-01-12 2021-01-11 2018-01-29 2016-01-29
4 10004 2020-01-13 2022-01-11 2018-01-30 2016-01-30
5 10005 2020-01-14 2023-01-11 2018-01-31 2016-01-31

Upvotes: 0

Views: 916

Answers (2)

Ferris
Ferris

Reputation: 5601

refer:

  1. strftime-and-strptime-behavior
  2. pd.to_datetime
df['Date1'] = pd.to_datetime(df.Date1, format='%d-%m-%Y')
pd.to_datetime(df.Date2, format='%d%m%Y')
pd.to_datetime(df.Date3, format='%d-%b-%y')
pd.to_datetime(df.Date4, format='%d%b%Y')

convert to string format:

pd.to_datetime(df.Date1, format='%d-%m-%Y').dt.strftime('%Y-%m-%d')

Upvotes: 0

Shag
Shag

Reputation: 430

If your column is a string, you will need to first use `pd.to_datetime',

df['Date'] = pd.to_datetime(df['Date'])

Then, use .dt datetime accessor with strftime:

df = pd.DataFrame({'Date':pd.date_range('2017-01-01', periods = 60, freq='D')})

df.Date.dt.strftime('%Y-%m-%d').astype(int)

Or use lambda function:

df.Date.apply(lambda x: x.strftime('%Y-%m-%d')).astype(int)

Upvotes: 2

Related Questions