0

I am getting the following error while trying to fetch the particular days of the week. The code which I have used is mentioned below:

df['weekday'] = df['Date'].dt.dayofweek

The date is in the following format.

enter image description here

But, I am not able to fetch the following days of the week and is getting an error as follows:

AttributeError: Can only use .dt accessor with datetimelike values

Can someone please help me out with this?

Vivek
  • 336
  • 2
  • 4
  • 18

1 Answers1

1

The error is telling that the .dt accessor can only be used with 'datetimelike' values. Unfortunately your question is incomplete since you are not showing us the dtype of the 'Date' column, see How to create a Minimal, Reproducible Example. But if the dtype isn't 'datetime64[ns]' I take from your painting that it must be 'object' (string).

Solution: Convert object into datetime and then use the .dt accessor:

df['weekday'] = df['Date'].apply(pd.to_datetime).dt.dayofweek

The result in your example is all zero, because 2005-07-25 was a Monday.

Peter
  • 10,959
  • 2
  • 30
  • 47