Henrique Chagas
Henrique Chagas

Reputation: 59

Convert datetime column into day of week

I'm trying to convert a datetime type column into day of week with the code below:

date = {'Date': ['2018-10-30', '2018-10-30', '2018-10-30', '2018-10-30', '2018-10-30']}
df = pd.DataFrame(date)
df.Date = df.to_datetime(df.Date)
df.Date.dayofweek()

But its runnig this error: AttributeError: 'Series' object has no attribute 'dayofweek'. Why is it calling Series object if it's datetime ?

When I slice the column like this:

[In] df.Date[0].dayofweek()
[Out] 1 

I get the expected result. Does anyone know what is happening?

Upvotes: 2

Views: 617

Answers (1)

jezrael
jezrael

Reputation: 862511

Remove () because working with Series (column) by Series.dt.dayofweek:

print (df.Date.dt.dayofweek)
0    1
1    1
2    1
3    1
4    1
Name: Date, dtype: int64

Not sure about your pandas version, but for me working Timestamp.dayofweek also without ():

print (df.Date.iat[0].dayofweek)
1

Upvotes: 3

Related Questions