Reputation: 197
I have a pandas dataframe with one if its columns in datetime format (year-month-day
). Is there any way to create a new column that says weekend or weekday, and a column that says what season it is? Even better (but this one seems much more complex so not totally necessary) would also be whether it's a federal holiday (United States) or not.
For example, an instance at date 2019-10-23 is a Wednesday, so I'd want to create a column called day_type
that fills 'weekday', and a column called season
that fills 'fall'.
Upvotes: 2
Views: 2328
Reputation: 595
It's easy with strftime:
import pandas as pd
df = pd.DataFrame({'string_date': ['2019-10-23', '2019-10-24', '2019-10-23']})
df['date'] = pd.to_datetime(df['string_date'], format='%Y-%m-%d', errors='ignore')
df['day_of_week'] = df['date'].dt.strftime('%A')
Result1 :
string_date date day_of_week
0 2019-10-23 2019-10-23 Wednesday
1 2019-10-24 2019-10-24 Thursday
2 2019-10-23 2019-10-23 Wednesday
For adding the season you can use this formula : Link
df['season'] = (df['date'].dt.month%12 + 3)//3
Result2:
string_date date day_of_week season
0 2019-10-23 2019-10-23 Wednesday 4
1 2019-10-24 2019-10-24 Thursday 4
2 2019-10-23 2019-10-23 Wednesday 4
Strftime reference: http://strftime.org/
Upvotes: 6
Reputation: 1807
I wrote a function that takes in a string argument in the form 'yyyy-mm-dd' and returns a tuple of season and day-of-week. Please modify the function accordingly to your needs.
from datetime import date, datetime
def date_week(date_str):
"""
this method returns season and day of week tuple from str
arg in the format 'yyyy-mm-dd'
"""
datetime_obj = datetime.strptime(date_str, '%Y-%m-%d')
weekdays = {0:'monday',
1:'tuesday',
2:'wednesday',
3:'thursday',
4:'friday',
5:'saturday',
6:'sunday'}
day_of_week = weekdays[datetime_obj.weekday()] # returns day of week
Y = 2000 # dummy leap year to allow input X-02-29 (leap day)
seasons = [('winter', (date(Y, 1, 1), date(Y, 3, 20))),
('spring', (date(Y, 3, 21), date(Y, 6, 20))),
('summer', (date(Y, 6, 21), date(Y, 9, 22))),
('autumn', (date(Y, 9, 23), date(Y, 12, 20))),
('winter', (date(Y, 12, 21), date(Y, 12, 31)))]
if isinstance(datetime_obj, datetime):
datetime_obj = datetime_obj.date()
datetime_obj = datetime_obj.replace(year=Y)
season = next(season for season, (start, end) in seasons
if start <= datetime_obj <= end) # returns season
return day_of_week, season
If we call date_week("2019-10-23")
, it will return ('wednesday', 'autumn')
Upvotes: 3