def get_encoded_calendar_df(data_final): # Load school holidays for France fr_holidays = SchoolHolidayDates() df_vacances = pd.DataFrame() for year in list(set(data_final['year'])): df_vacances = pd.concat([df_vacances, pd.DataFrame.from_dict(fr_holidays.holidays_for_year(year)).T]) # Load bank holidays for France df_jf = pd.DataFrame() for year in list(set(data_final['year'])): df_jf = pd.concat([df_jf, pd.DataFrame([ {'date': el[0], 'jour_ferie': el[1]} for el in sorted(holidays.FRA(years=year).items())])]) # Merge school and bank holidays df_holidays = pd.merge(df_vacances, df_jf, how='outer', on='date') # Create features from df_holidays dataframes (school holidays and bank holidays): # - 3 binary features for school holidays, taking 1 if the given zone is on holiday, else 0 (vacances_zone_a, # vacances_zone_b, vacances_zone_c) # Definition of a dictionary to encode boolean into numeric dict_map_vac = { True: 1, False: 0 } # Apply dictionary to each holiday column for the three zones (A, B, C) df_holidays['vacances_zone_a'] = df_holidays['vacances_zone_a'].map(dict_map_vac) df_holidays['vacances_zone_b'] = df_holidays['vacances_zone_b'].map(dict_map_vac) df_holidays['vacances_zone_c'] = df_holidays['vacances_zone_c'].map(dict_map_vac) # - 1 binary feature for bank holiday, taking 1 if it is a bank holiday, else 0 # The column "jour ferie" contains either the name of the holiday or a missing value (NaN) # The idea is to put a '1' when it's a holiday (i.e. when the value is different from nan, else 0) df_holidays['jour_ferie'] = df_holidays['jour_ferie'].map(lambda x: 1 if str(x) != 'nan' else 0) # - To go further: Try to create a combined feature with school and bank holidays df_holidays['holiday'] = df_holidays['vacances_zone_a'] + df_holidays['vacances_zone_b'] + df_holidays[ 'vacances_zone_c'] + df_holidays['jour_ferie'] df_holidays['date'] = df_holidays['date'].map(lambda x: str(x)) data_final_cal = pd.merge(data_final, df_holidays, how='left', left_on='release_date', right_on='date').fillna(0) data_final_cal['month'] = data_final_cal['release_date'].map(lambda x: int(x[5:7])) data_final_cal = apply_cos(data_final_cal, 'month', 'cos_month', 12) return data_final_cal
def judge_local_holiday(self, df): country = df['geoNetwork_country'] date = df['visitId'].apply(lambda x: x.date()) judge_holiday = \ np.where(country.isin( ['United States','India','Canada','Germany', 'Japan','France','Mexico','Australia', 'Spain','Netherlands','Italy','Ireland', 'Sweden','Argentina','Colombia','Belgium', 'Switzerland','Czechia','Colombia','Belgium', 'New Zealand','South Africa','South Africa']),\ np.where((country=='United States')& (date.isin(holidays.US())),1, np.where((country=='India')& (date.isin(holidays.India())),1, np.where((country=='Canada')& (date.isin(holidays.CA())),1, np.where((country=='Germany')& (date.isin(holidays.DE())),1,\ np.where((country=='Japan')& (date.isin(holidays.JP())),1, np.where((country=='France')& (date.isin(holidays.FRA())),1, np.where((country=='Mexico')& (date.isin(holidays.MX())),1, np.where((country=='Australia')& (date.isin(holidays.AU())),1,\ np.where((country=='Spain')& (date.isin(holidays.ES())),1, np.where((country=='Netherlands')& (date.isin(holidays.NL())),1, np.where((country=='Italy')& (date.isin(holidays.IT())),1, np.where((country=='Ireland')& (date.isin(holidays.IE())),1,\ np.where((country=='Sweden')& (date.isin(holidays.SE())),1, np.where((country=='Argentina')& (date.isin(holidays.AR())),1, np.where((country=='Colombia')& (date.isin(holidays.CO())),1, np.where((country=='Belgium')& (date.isin(holidays.BE())),1,\ np.where((country=='Switzerland')& (date.isin(holidays.CH())),1, np.where((country=='Czechia')& (date.isin(holidays.CZ())),1, np.where((country=='Denmark')& (date.isin(holidays.DK())),1, np.where((country=='Austria')& (date.isin(holidays.AT())),1,\ np.where((country=='Hungary')& (date.isin(holidays.HU())),1, np.where((country=='Portugal')& (date.isin(holidays.PT())),1, np.where((country=='Norway')& (date.isin(holidays.NO())),1, np.where((country=='Portugal')& (date.isin(holidays.PT())),1,\ np.where((country=='New Zealand')& (date.isin(holidays.NZ())),1, np.where((country=='South Africa')& (date.isin(holidays.ZA())),1, np.where((country=='South Africa')& (date.isin(holidays.ZA())),1,\ 0))))))))))))))))))))))))))),np.nan).astype(int) return judge_holiday
def test_read_working_days_from_contract_by_month_by_user( client: TestClient, normal_user_token_headers: dict, db: Session) -> None: r = client.get(f"{settings.API_V1_STR}/users/me", headers=normal_user_token_headers) user_id = r.json()["id"] contract = create_random_contract(db, user_id=user_id) working_days = [] next_days = +1 working_days.insert( 0, create_random_working_day(db, contract_id=contract.id, day=str(contract.start + relativedelta(days=+next_days * 1)))) working_days.insert( 1, create_random_working_day(db, contract_id=contract.id, day=str(contract.start + relativedelta(days=+next_days * 2)))) working_days.insert( 2, create_random_working_day(db, contract_id=contract.id, day=str(contract.start + relativedelta(days=+next_days * 5)))) # Month N month_date = str(contract.start) year = str(month_date).split("-")[0] month = str(month_date).split("-")[1] response = client.get( f"{settings.API_V1_STR}/contracts/{contract.id}/working_days?year={year}&month={month}", headers=normal_user_token_headers, ) assert response.status_code == 200 content = response.json() assert isinstance(content, list) holidays_fra = [ datetime.strftime(x[0], "%Y-%m-%d") for x in holidays.FRA(years=int(year)).items() if x[0] >= datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d").date() and x[0] < datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d").date() + relativedelta(months=+1) ] weekmask = contract.weekdays startDay = datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d").date() endDay = datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d").date() + relativedelta(months=+1) workingdays_list = [x.day for x in working_days] business_days_inherited_pandas = pandas.bdate_range( start=startDay, end=endDay, freq="C", weekmask=weekmask, holidays=holidays_fra + workingdays_list).format() working_days2 = working_days + [dict(day=x) for x in holidays_fra] + [ dict(day=x) for x in business_days_inherited_pandas ] assert len(content) == len(working_days2) for i in content: assert "id" in dict(i).keys() assert "day" in dict(i).keys() assert "start" in dict(i).keys() assert "end" in dict(i).keys() assert "contract_id" in dict(i).keys() assert "day_type_id" in dict(i).keys()
lookup_month = { 1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June", 7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December" } fr_holidays = holidays.FRA() df = pd.DataFrame({"date": pd.date_range('2000-01-01', '2050-12-31')}) df["year"] = df.date.dt.year df["semester"] = (df.date.dt.quarter + 1) // 2 df["quarter"] = df.date.dt.quarter df["monthofyear"] = df.date.dt.month df["name_of_month"] = df["monthofyear"].apply(lambda x: lookup_month[x]) df["week"] = df.date.dt.weekofyear df["dayofyear"] = df.date.dt.dayofyear df["dayofweek"] = df.date.dt.dayofweek + 1 df["name_of_day"] = df.date.dt.weekday_name df["holiday"] = df["date"].apply(lambda x: x in fr_holidays) print(df.head(10)) df.to_csv("lookup_date.csv", index=False)
def is_holidays(): """ Détermine si lejour courant est un jour ferié :return: """ return datetime.datetime.today() in holidays.FRA()
def read_working_days( *, db: Session = Depends(deps.get_db), id: int, year: str = Query(None, regex="^[2][0-9]{3}", min_length=4, max_length=4), month: str = Query(None, regex="^[01]?[0-9]", min_length=1, max_length=2), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Retrieve working_days by contract. """ contract = crud.contract.get(db=db, id=id) if not contract: raise HTTPException(status_code=404, detail="Contract not found") if (not crud.user.is_superuser(current_user) and (contract.user_id != current_user.id)): raise HTTPException(status_code=400, detail="Not enough permissions") if year and month: month_date = datetime.strptime(f"{year}-{month}", "%Y-%m").date() next_month_date = month_date + relativedelta(months=+1, days=-1) working_days = crud.working_day.get_multi_by_contract_by_date( db=db, contract_id=id, start=month_date, end=next_month_date) else: working_days = crud.working_day.get_multi_by_contract(db=db, contract_id=id) holidays_fra = [ datetime.strftime(x[0], "%Y-%m-%d") for x in holidays.FRA(years=int(year)).items() if x[0] >= datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d").date() and x[0] < datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d").date() + relativedelta(months=+1) ] workingdays_list = [x.day for x in working_days] weekmask = contract.weekdays startDay = datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d").date() endDay = datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d").date() + relativedelta(months=+1) business_days_inherited_pandas = pandas.bdate_range( start=startDay, end=endDay, freq="C", weekmask=weekmask, holidays=holidays_fra + workingdays_list).format() result = working_days + [ schemas.WorkingDay( day=x, day_type_id=51, contract_id=contract.id, id=0, start=datetime.strptime(f"09:00:00", "%H:%M:%S").time(), end=datetime.strptime(f"18:00:00", "%H:%M:%S").time(), ) for x in holidays_fra ] + [ schemas.WorkingDay( day=x, day_type_id=50, contract_id=contract.id, id=0, start=datetime.strptime(f"09:00:00", "%H:%M:%S").time(), end=(datetime.strptime(f"09:00:00", "%H:%M:%S") + relativedelta(hours=+int(contract.hours))).time(), ) for x in business_days_inherited_pandas ] result.sort(key=lambda x: x.day) return result