Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a day-of-week column in a Pandas dataframe using Python

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

like image 712
James Eaves Avatar asked May 13 '15 18:05

James Eaves


People also ask

How do you get the day of the week in pandas?

The dayofweek property is used to get the day of the week. The day of the week with Monday=0, Sunday=6. Note: It is assumed the week starts on Monday, which is denoted by 0 and ends on Sunday which is denoted by 6. This method is available on both Series with datetime values (using the dt accessor) or DatetimeIndex.

How do you convert a date to a day of the week in Python?

isoweekday() to get a weekday of a given date in Python Use the isoweekday() method to get the day of the week as an integer, where Monday is 1 and Sunday is 7. i.e., To start from the weekday number from 1, we can use isoweekday() in place of weekday() . The output is 1, which is equivalent to Monday as Monday is 1.


1 Answers

Pandas 0.23+

Use pandas.Series.dt.day_name(), since pandas.Timestamp.weekday_name has been deprecated:

import pandas as pd   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['day_of_week'] = df['my_dates'].dt.day_name() 

Output:

    my_dates  myvals day_of_week 0 2015-01-01       1    Thursday 1 2015-01-02       2      Friday 2 2015-01-03       3    Saturday 

Pandas 0.18.1+

As user jezrael points out below, dt.weekday_name was added in version 0.18.1 Pandas Docs

import pandas as pd  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['day_of_week'] = df['my_dates'].dt.weekday_name 

Output:

    my_dates  myvals day_of_week 0 2015-01-01       1    Thursday 1 2015-01-02       2      Friday 2 2015-01-03       3    Saturday 

Original Answer:

Use this:

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.dayofweek.html

See this:

Get weekday/day-of-week for Datetime column of DataFrame

If you want a string instead of an integer do something like this:

import pandas as pd  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['day_of_week'] = df['my_dates'].dt.dayofweek  days = {0:'Mon',1:'Tues',2:'Weds',3:'Thurs',4:'Fri',5:'Sat',6:'Sun'}  df['day_of_week'] = df['day_of_week'].apply(lambda x: days[x]) 

Output:

    my_dates  myvals day_of_week 0 2015-01-01       1       Thurs 1 2015-01-02       2         Fri 2 2015-01-01       3       Thurs 
like image 103
Liam Foley Avatar answered Oct 23 '22 06:10

Liam Foley