Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
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.


Edit

Both

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

and

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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...