-1

I know there are many questions and answers about date format in a data frame, but I was quite able to find the answer to my question.

I got a data frame with a date format like this: "Tue, Oct 22, 2019". However, I would like to try and convert it into this format : %Y-%m-%d.

Here's what I tried :

nba_score['Date'] = pd.to_datetime(nba_score['Date'],format="%a %b %d %Y").strftime('%Y-%m-%d')

But I receive this error : ValueError: time data 'Tue, Oct 22, 2019' does not match format '%a %b %d %Y' (match)

Then I tried to add commas to reflect the right format like this:

nba_score['Date'] = pd.to_datetime(nba_score['Date'],format="%a, %b %d, %Y").strftime('%Y-%m-%d')

And I receive this error: AttributeError: 'Series' object has no attribute 'strftime'

Any help would be appreciated.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
yce
  • 57
  • 2
  • 12

1 Answers1

1

See this answer: Convert column of date objects in Pandas DataFrame to strings. You need to add .dt before .strftime.

Use:

nba_score['Date'] = pd.to_datetime(nba_score['Date'],format="%a, %b %d, %Y").dt.strftime('%Y-%m-%d')
gannonbarnett
  • 1,638
  • 1
  • 16
  • 28