Create a day-of-week column in a Pandas dataframe using Python
I’d like to read a csv file into a pandas dataframe, parse a column of dates from string format to a date object, and then generate a new column that indicates the day of the week.
This is what I’m trying:
What I’d like to do is something like:
import pandas as pd
import csv
df = pd.read_csv('data.csv', parse_dates=['date']))
df['day-of-week'] = df['date'].weekday()
AttributeError: 'Series' object has no attribute 'weekday'
Thank you for your help. James
Just in case if .dt doesn't work for you. Trying .DatetimeIndex might help. Hope the code and our test result here help you fix it. Regards,
import pandas as pd
import datetime
df = pd.DataFrame({'Date':['2015-01-01','2015-01-02','2015-01-03'],'Number':[1,2,3]})
df['Day'] = pd.DatetimeIndex(df['Date']).day_name() # week day name
df.head()
In version 0.18.1
is added dt.weekday_name
:
print df
my_dates myvals
0 2015-01-01 1
1 2015-01-02 2
2 2015-01-03 3
print df.dtypes
my_dates datetime64[ns]
myvals int64
dtype: object
df['day_of_week'] = df['my_dates'].dt.weekday_name
print df
my_dates myvals day_of_week
0 2015-01-01 1 Thursday
1 2015-01-02 2 Friday
2 2015-01-03 3 Saturday
Another solution with assign
:
print df.assign(day_of_week = df['my_dates'].dt.weekday_name)
my_dates myvals day_of_week
0 2015-01-01 1 Thursday
1 2015-01-02 2 Friday
2 2015-01-03 3 Saturday
Using dt.weekday_name
is deprecated since pandas 0.23.0
, instead, use dt.day_name()
:
df = pd.DataFrame({'my_dates':['2015-01-01','2015-01-02','2015-01-03'],'myvals':[1,2,3]})
df['my_dates'] = pd.to_datetime(df['my_dates'])
df['my_dates'].dt.day_name()
0 Thursday
1 Friday
2 Saturday
Name: my_dates, dtype: object
df =df['Date'].dt.dayofweek
dayofweek
is in numeric format
Source: Stackoverflow.com