user288609
user288609

Reputation: 13025

calculate the weekday information based on date information

In a dataframe created using Pandas, there is one column, i.e., 'date', storing the value of date, i.e., 2012-10-8, how can I create another column, each entry of which stores the weekdays for the corresponding date entry.

In other words, I would like to have two columns

date         weekday
2012-10-8    Tuesday

The weekday entry need to be computed based on the date column.

Upvotes: 1

Views: 82

Answers (2)

user3483203
user3483203

Reputation: 51155

Use pandas.Series.dt.day_name.

df.date.dt.day_name()

0    Monday
Name: date, dtype: object

While not necessary here, it also takes locale as a parameter

df.date.dt.day_name(locale='es')

0    Lunes
Name: date, dtype: object

pandas.Series.dt.day_name is new in pandas 0.23.0, if you are using a previous version, use weekday_name instead:

df.date.dt.weekday_name

0    Monday
Name: date, dtype: object

Using weekday_name has been deprecated in 0.23.0 in favor of day_name()

Upvotes: 0

rafaelc
rafaelc

Reputation: 59274

IIUC, use .dt accessor and strftime using %A

>>> df.date.dt.strftime('%A')

0    Monday
Name: date, dtype: object

ps: Note that 2012-10-08 was a Monday, and not Tuesday

Upvotes: 1

Related Questions