Grzegorz Lippe
Grzegorz Lippe

Reputation: 109

How to use pandas.tseries.holiday with a timeseries different frequency?

This answer shows how to create a column, which contains whether a day is a (US) holiday or not.

How can I get this to work with a dataframe, that is not sampled in days like in this example?

import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar

df = pd.DataFrame(index=pd.date_range(start='2001-07-03 22:00', end='2001-07-04 02:00', freq='1h'))

holidays = calendar().holidays(start=df.index[0], end=df.index[-1])
df['holiday'] = df.index.isin(holidays)
df

Output

                     holiday
2001-07-03 22:00:00    False
2001-07-03 23:00:00    False
2001-07-04 00:00:00     True
2001-07-04 01:00:00    False
2001-07-04 02:00:00    False

Desired Output

                     holiday
2001-07-03 22:00:00    False
2001-07-03 23:00:00    False
2001-07-04 00:00:00     True
2001-07-04 01:00:00     True
2001-07-04 02:00:00     True

Upvotes: 0

Views: 631

Answers (1)

Salem Moussa
Salem Moussa

Reputation: 35

Convert the datetime index to date only index as follow, and it should work:

df.index = pd.to_datetime(df.index.date)

Based on your example :

    import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar

df = pd.DataFrame(index=pd.date_range(start='2001-07-03 22:00', end='2001-07-04 02:00', freq='1h'))

# new line
df.index = pd.to_datetime(df.index.date)

holidays = calendar().holidays(start=df.index[0], end=df.index[-1])
df['holiday'] = df.index.isin(holidays)
df

Upvotes: 0

Related Questions