Home > Mobile >  Get the US holidays in boolean in dataframe
Get the US holidays in boolean in dataframe

Time:02-17

I have a dataframe, I want to get the holidays as a binary. However, I can get them in string. Besides, it seems that in some days, there are a mistake. Like, 2019-01-01 should be holiday. Here is a sample of my data. Could you help me with that?

import pandas as pd
import numpy as np
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar
df = pd.DataFrame()

df['date'] = [ '2019-01-01',  '2015-07-03', '2019-01-12', '2019-01-13']
df['c1'] = [ 4, 3, 8, 5]
df['c2'] = [1 ,  3, 11, 2]
df
cal = calendar()
holidays = cal.holidays(start=df['date'].min(), end=df['date'].max())

df['Holiday'] = df['date'].isin(holidays)
df

CodePudding user response:

Simply change the line to this:

df['Holiday'] = df.index.get_level_values(0).isin(holidays).astype(int)
  • Related