Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

python - isin holidays only recognizing the first hour

I have created a class with the holidays in Spain

class SpainBusinessCalendar(AbstractHolidayCalendar):
   rules = [
     #Spain - If one holiday is on a Sunday, each Autonomous Community can change it to a Monday.
     Holiday('A?o Nuevo', month=1, day=1, observance=sunday_to_monday),
     Holiday('Epifanía del Se?or', month=1, day=6, observance=sunday_to_monday),
     Holiday('Viernes Santo', month=1, day=1, offset=[Easter(), Day(-2)]),
     Holiday('Día del Trabajador', month=5, day=1, observance=sunday_to_monday),
     Holiday('Asunción de la Virgen', month=8, day=15, observance=sunday_to_monday),
     Holiday('Día de la Hispanidad', month=10, day=12, observance=sunday_to_monday),
     Holiday('Todos los Santos', month=11, day=1, observance=sunday_to_monday),
     Holiday('Día Constitución', month=12, day=6, observance=sunday_to_monday),
     Holiday('Inmaculada Concepción', month=12, day=8, observance=sunday_to_monday),        
     Holiday('Navidad', month=12, day=25, observance=sunday_to_monday)

Then I generated an with the size equals to the Date column in my dataframe

cal = SpainBusinessCalendar()
holidays = cal.holidays(start=df['Date'].min(), end=df['Date'].max())

Which gives the following

enter image description here

In order to generate a df column with the holidays, resulting from the values in the column "Date", I have done

df['Feriado'] = df['Date'].isin(holidays).astype(int)

However, as one can guess from the image of the holidays output, if one is working with hourly data, which is the case, it will only pick up as holiday the first hour (with the time 00:00).

How should I proceed in order to, in the analysis of holidays, the hour is ignored and for a specific holiday date, assign the respective value.



holidays = cal.holidays(start=df['Data'].dt.date.min(), end=df['Data'].dt.date.max())


holidays = cal.holidays(start=df['Data'].dt.floor('d').min(), end=df['Data'].dt.floor('d').max())

Give the same output as the image above.

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Created a column with the Date without the time component

df['Date_notime'] = df['Data'].dt.floor('d')

Then generated the holidays from that column

holidays = cal.holidays(start=df['Date_notime'].dt.date.min(), end=df['Date_notime'].dt.date.max())

And like then

df['Feriado'] = df['Date_notime'].isin(holidays).astype(int)

And at the end dropped the columns that I didn't want.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers


55.5k users