0

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.

  • See @BillHuang's answer below but if you must use a format string, make sure it matches your data, i.e., `'%a, %b %d, %Y'`. Note the spaces and the commas. –  Apr 07 '21 at 15:40

1 Answers1

1

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
Bill Huang
  • 4,491
  • 2
  • 13
  • 31