1

I'm trying to create a "workday" column that flags a range of dates as either "True" or "False" based on if it is a weekday and if it is a holiday. If it's a weekday and also not a holiday, then it is considered a "workday," else not a workday.

everything seems to be going as intended until the the last line. I've created the new column named "workday," filled the values as whatever is in "weekday," but then when I go to replace the values in "workday" as false where "holiday" is True, it doesn't seem to be doing anything.

Where am I going wrong here? Also, let me know if you have any thoughts on simplifying my code, I'm clearly a beginner. :) Any help is appreciated!

import pandas as pd
import numpy as np
from datetime import datetime
from pandas.tseries.holiday import USFederalHolidayCalendar as holidaylist

datelist = pd.date_range(start='2018-08-01', end=datetime.today())

bizdaylist = pd.date_range(start='2018-08-01', end=datetime.today(), freq='B')

df1_columns = ['date']
df1 = pd.DataFrame(datelist, columns = df1_columns)

df2_columns = ['date']
df2 = pd.DataFrame(bizdaylist, columns = df2_columns)

df1 = df1.assign(weekday=df1.date.isin(df2.date).astype(str))

df3 = pd.DataFrame()
df3['Date'] = datelist

hl = holidaylist()
holidays = hl.holidays(start=datelist.min(), end=datelist.max())

df1['holiday'] = df3['Date'].isin(holidays)

df1['workday'] = np.where(df1.holiday == 'FALSE', df1.holiday, df1.weekday)

print(df1.loc[877:884])

dataframe index 877-884:

dataframe index 877-884

DapperDuck
  • 2,728
  • 1
  • 9
  • 21
  • Is this something that will be helpful to you... https://stackoverflow.com/questions/29688899/pandas-checking-if-a-date-is-a-holiday-and-assigning-boolean-value – Joe Ferndz Jan 13 '21 at 02:20

1 Answers1

0

The query for workday is incorrect.

You need to change it to

df1['workday'] = np.where((df1.weekday == 'False') | (df1.holiday == True), False, True)

Also, the value is stored as False not FALSE. So your check has to be exact match.

You have weekday as an object because you converted it to astype(str). So you need to check for string value of 'False'

If it is NOT a weekday or if it is a Holiday, then the value will be False. Otherwise it has to be True.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33