Marc Arnold
Marc Arnold

Reputation: 1

Correct fomat of date to parse it

Probably very simple, however I cannot find the right format for parsing dates in a dataframe.

Date to parse: Thu, Apr 1, 2021 (df name: df data, column name: Date )

My attempt:

date_p = pd.to_datetime(nba_data.Date, format = '%a%b%-m%Y')

I am aware that the '-' is according to the Error a bad directive in the format. However solely %m would according to my knowledge refer to 01 and not 1. Am I correct with that assumption.

Would be very thankful for any help.

Upvotes: 0

Views: 48

Answers (1)

Bill Huang
Bill Huang

Reputation: 4648

It seems like no format indication is required, as pandas (v1.2.3 with python 3.8) already recognizes such format.

print(pd.to_datetime(df["Date"])

Out[185]: 
0   2021-04-01
Name: Date, dtype: datetime64[ns]

Test Data

df = pd.read_csv(io.StringIO("""
Date
Thu, Apr 1, 2021
"""), sep=r"\s{2,}", engine='python')

print(df)
               Date
0  Thu, Apr 1, 2021

Upvotes: 1

Related Questions