def is_day_off(date, weekday): cal = France() splitted = ((date.split(" "))[0]).split("-") #print(splitted) y = int(splitted[0]) m = int(splitted[1]) d = int(splitted[2]) if (cal.is_working_day(dt.date(y,m,d)) and (weekday != 6)): return 0 else: return 1
def est_ouvr(_dt) : # Imports from app.models import TDateFermeture from workalendar.europe import France output = False # Initialisation d'un calendrier français cal = France() # Vérification de l'ouvrabilité if cal.is_working_day(_dt) == True and TDateFermeture.objects.filter(pk = _dt).count() == 0 : output = True return output
def _get_feries(self) -> List[datetime]: """Renvoie la liste des jours fériés de l'année x et de l'année x +1 :return: liste des jours fériés :rtype: List[datetime] """ days = [] for year in (self.year, self.year + 1): days += [_[0] for _ in France().holidays(year)] self.feries = days return days
def get_nb_half_days_by_period_per_month(date_from, date_to): day_list = pd.date_range(date_from, date_to).tolist() cal = France() holidays = cal.holidays(date_from.year) holidays_date = [h[0] for h in holidays] md_dict = {} for i in range(1, 13): md_dict[i] = { "monday_am": 0, "monday_pm": 0, "tuesday_am": 0, "tuesday_pm": 0, "wednesday_am": 0, "wednesday_pm": 0, "thursday_am": 0, "thursday_pm": 0, "friday_am": 0, "friday_pm": 0, } # for each day of the period for day in day_list: if not day.date() in holidays_date: if day.dayofweek == 0: md_dict[day.month]['monday_am'] += 1 md_dict[day.month]['monday_pm'] += 1 if day.dayofweek == 1: md_dict[day.month]['tuesday_am'] += 1 md_dict[day.month]['tuesday_pm'] += 1 if day.dayofweek == 2: md_dict[day.month]['wednesday_am'] += 1 md_dict[day.month]['wednesday_pm'] += 1 if day.dayofweek == 3: md_dict[day.month]['thursday_am'] += 1 md_dict[day.month]['thursday_pm'] += 1 if day.dayofweek == 4: md_dict[day.month]['friday_am'] += 1 md_dict[day.month]['friday_pm'] += 1 return md_dict
def fill_not_working_days(self): """ Create columns WEEK_END and DAY_OFF from DATE """ # days-off cal = France() self.df["DAY_OFF"] = np.vectorize(cal.is_holiday)( self.df.index.to_pydatetime()).astype(int) # week-end self.df["WEEK_END"] = np.in1d(self.df.index.dayofweek, [5, 6]).astype(int) # not working day self.df["NOT_WORKING_DAY"] = self.df["DAY_OFF"] | self.df["WEEK_END"] # daytime self.df["DAYTIME"] = self.df.index.map(self.__is_daytime)
def clean_electricity(self): import datetime #Get holiday dates in France using the library called workalendar from workalendar.europe import France cal = France() #Convert dates to DateTime format self.df['Date'] = pd.to_datetime(self.df['Date'], format='%d/%m/%Y') #Create the DayTypes mentioned in the article (except DayType 8) #/!\ We chose an arbitrary order to create the day types, since they are not separated sets #Add normal weekdays (DayType 1) self.df['Day_type'] = self.df['Date'].apply(lambda u: (u.date(), 1)) #Add Mondays (DayType 0) self.df['Day_type'] = self.df['Day_type'].apply( lambda x: (x[0], 0) if x[0].weekday() == 0 else x) #Add Fridays (DayType 2) self.df['Day_type'] = self.df['Day_type'].apply( lambda x: (x[0], 2) if x[0].weekday() == 4 else x) #Add Saturdays (DayType 3) self.df['Day_type'] = self.df['Day_type'].apply( lambda x: (x[0], 3) if x[0].weekday() == 5 else x) #Add Sundays (DayType 4) self.df['Day_type'] = self.df['Day_type'].apply( lambda x: (x[0], 4) if x[0].weekday() == 6 else x) #Add Bank holidays (DayType 6) self.df['Day_type'] = self.df['Day_type'].apply( lambda x: (x[0], 6) if cal.is_holiday(x[0]) == True else x) #Add Before Bank holidays (DayType 5) self.df['Day_type'] = self.df['Day_type'].apply( lambda x: (x[0], 5) if cal.is_holiday(x[0] + datetime.timedelta( days=1)) == True else x) #Add After Bank holidays (DayType 7) self.df['Day_type'] = self.df['Day_type'].apply( lambda x: (x[0], 7) if cal.is_holiday(x[0] - datetime.timedelta( days=1)) == True else x) self.df['Day_type'] = self.df['Day_type'].apply(lambda x: x[1]) self.df['Date'] = self.df['Date'].apply(lambda u: u.date())
def __init__(self, _date): self.date = _date # if required, _calendar.is_working_day can deal with extra working days/holidays self.is_working_day = France().is_working_day(_date) self.weekday = _date.weekday() # normal working day if no holiday or if weekend day # tip: holiday during weekend are considered as normal weekend working day if self.weekday in [5, 6] or self.is_working_day: self.profile = copy.deepcopy(Week.SLOTS[self.weekday]) # unusual working day if holiday else: self.profile = copy.deepcopy(Week.SLOTS[Week.KEY_SPECIAL_HOLIDAY]) if not self.profile: raise UserWarning( "daily planning's profile has not been successfully resolved.")
def get_cal_from_country(bmk_country): """ Function returning a calendar based on the 'benchmark_country' of the csv file # Python package to manage holidays per country # >> See : https://github.com/peopledoc/workalendar from 'benchmark_country' column (to be parsed) in the Derivation Script Warning : Tuples may appear like [USA, Japon] or [USA, China] instead of China [Germany, China] instead of Russia [USA, China] instead of Moyen-Orient [USA, China] instead of Brasil Currently missing : China, Russia @TODO : ADD HONG-KONG !!! (for 'HSI_Index') NOTE : 5 avril 2018 : Ching Ming Festival (jour férié Hong-Kong !) :param bmk_country: benchmark country (type: string) :return: - cal: calendar related to the country (type: workalendar type ?) """ cal = [] if ',' in bmk_country: # '[A, B]' => ['A', 'B'] print "[WARNING] Tuple for the 'benchmark_country : {}, returning the first one..".format( bmk_country) bmk_country = bmk_country.replace('[', '').replace(']', '').split(',') bmk_country = bmk_country[0] # TO BE DEFINED ! if bmk_country == 'USA': cal = UnitedStates() elif bmk_country == 'Germany': cal = Germany() elif bmk_country == 'Japan': cal = Japan() elif bmk_country == 'France': cal = France() elif bmk_country == 'UK': cal = UnitedKingdom() elif bmk_country == 'Grèce': cal = Greece() elif bmk_country == 'Italie': cal = Italy() elif bmk_country == 'Espagne': cal = Spain() elif bmk_country == 'Brasil': cal = Brazil() return cal
] name_encoder = dict(zip(names, [list(x) for x in np.eye(len(names))[:, 1:]])) weekdays = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ] # features features = ( ['YEAR', 'MONTH', 'DAY', 'WEEKEND', 'HOLIDAY', 'TIMESLOT', 'DAYSLOT'] + weekdays[:-1] + names[1:] + ['CALLS']) # French calendar object calendar = France() # first and last dates possibly considered first = datetime.strptime('2011-01-01 00:00:00', '%Y-%m-%d %H:%M:%S') last = datetime.strptime('2013-12-31 23:30:00', '%Y-%m-%d %H:%M:%S') def parse_date(datestring): """ Parse date string and return relevant features. """ # datetime object encoding data information date = datetime.strptime(datestring[:-4], '%Y-%m-%d %H:%M:%S') # create date-related predictor weekday = [0] * 6
import daiquiri import numpy as np import pandas as pd from dateutil import parser from workalendar.europe import France from sklearn.cluster import KMeans import xgboost as xgb from jitenshea import config daiquiri.setup(logging.INFO) logger = daiquiri.getLogger("stats") # French Calendar cal = France() SEED = 2018 np.random.seed(SEED) CLUSTER_ACT_PATH_CSV = 'jitenshea/data/cluster_activite.csv' ################################### ### CLUSTER ACTIVITE ################################### def preprocess_data_for_clustering(df): """Prepare data in order to apply a clustering algorithm Parameters
#! /usr/bin/env python # -*- coding: utf-8 -*- """Use weekalendar to generate holidays.""" from collections import OrderedDict from workalendar.europe import France holidays = [] for year in range(1990, 2020): holidays += France().get_calendar_holidays(year) header = """#! /usr/bin/env python # -*- coding: utf-8 -*- from datetime import datetime holidays = [""" footer = """ ] """ with open("../assets/holidays.py", "w") as text_file: text_file.write(header) for holiday_date, holiday_name in OrderedDict(holidays).iteritems(): text_file.write(""" datetime.strptime("{}", "%Y-%m-%d").date(), # {}""".format( holiday_date, holiday_name)) text_file.write(footer)
def is_holiday_fr(date_of_interest): cal = France() result = cal.is_holiday(date_of_interest) # this will return a boolean True/False value # cast to integer result = int(result) return result
import pandas as pd from workalendar.europe import France CAL = France() map_jour_fr = { 'JOHV': 'Jour Ouvré Hors Vacances Scolaires', 'SAHV': 'Samedi Hors Vacances Scolaires', 'JOVS': 'Jour Ouvré en période de Vacances Scolaires', 'SAVS': 'Samedi en période de Vacances Scolaires', 'DIJFP': 'Dimanche, Jour Férié et ponts' # TODO:pont } def is_dimanchelike(day): """Dimanche, Jour Férié """ return not CAL.is_working_day(day) def read_vacances(path): mapping = { 'Académies': 'academie', 'Date de début': 'begin', 'Date de fin': 'end', 'Description': 'description' } df = (pd.read_csv(path).rename(columns=mapping)) for d in ['begin', 'end']: df[d] = pd.to_datetime(df[d])
def __init__(self, school_year): """Creates the class :param school_year: Starting year for the school year :type school_year: int """ # Initialize school calendar school_calendar = {} # Get holidays from National Education Ministry with open("Calendrier_Scolaire_Zone_B.ics", "r") as cal: edu_calendar = icsCalendar(cal.read()) # Initialize days off ICS calendar days_off_school = icsCalendar() # Define begin/end of the school year we are in for event in edu_calendar.events: if ("Rentrée scolaire des élèves" in event.name and event.begin.year == school_year): school_begin = event.begin continue if "Vacances d'été" in event.name and event.begin.year == school_year + 1: school_end = event.begin days_off_school.events.add(event) continue # Add events between begin and end of school year # First add school holidays, starting the day after the beginning for event in edu_calendar.events: if event.begin > school_begin and event.begin < school_end: # Remove 1s to end so that it does not overrides next day event._end_time = event._end_time.shift(seconds=-1) days_off_school.events.add(event) # Then take holidays from workalendar calendar = France() for year in [school_year, school_year + 1]: for day_off in calendar.holidays(year): begin = day_off[0] event = Event(day_off[1], begin) event.make_all_day() if event.begin > school_begin and event.begin < school_end: # Remove 1s to end so that it does not overrides next day event.end = event.end.shift(seconds=-1) days_off_school.events.add(event) # Create timeline object timeline = days_off_school.timeline def get_last_tue(date): while date.weekday() != 1: date = date.shift(days=-1) return date def set_end(date): return date.replace(hour=16, minute=30, second=0, microsecond=0) # Now create dict of all days of school year and status # ordered by week number current = school_begin while int(current.strftime("%w")) > 1: current = current.shift(days=-1) while current < school_end: day = current.strftime("%d") week_number = current.isocalendar()[1] if week_number not in school_calendar.keys(): school_calendar[week_number] = [] current_week = school_calendar[week_number] # Limite cantine limit = set_end( get_last_tue( current.shift( weeks=-1) if current.weekday() >= 1 else current)) past_cantine = arrow.now() > limit past_garderie = current < arrow.now() month = current.strftime("%B").capitalize() data = { "day": day, "month": month, "date": current.strftime("%Y%m%d") } # If one of these is past, then save immediately data["bookable_cantine"] = not past_cantine data["bookable_garderie"] = not past_garderie if not data["bookable_cantine"] and not data["bookable_garderie"]: current_week.append(data) current = current.shift(days=+1) continue if current.isoweekday() in NON_WORKING_DAYS or current.isoweekday( ) in [ 6, 7, ]: data["bookable_cantine"] = False data["bookable_garderie"] = False data["bookable"] = False current_week.append(data) elif len([i for i in timeline.at(current)]) > 0: data["bookable_cantine"] = False data["bookable_garderie"] = False data["bookable"] = False current_week.append(data) elif current < school_begin: data["bookable_cantine"] = False data["bookable_garderie"] = False data["bookable"] = False current_week.append(data) else: data["bookable"] = True current_week.append(data) current = current.shift(days=+1) # Create set of periods i = 1 periods = { i: { "begin": school_begin.format("YYYYMMDD"), "begin_pretty": school_begin.format("DD/MM/YYYY"), } } for event in sorted(edu_calendar.events): if event.begin < school_begin: continue if event.begin > school_end: continue if "Vacances" in event.name: periods[i]["end"] = event.begin.format("YYYYMMDD") periods[i]["end_pretty"] = event.begin.format("DD/MM/YYYY") i += 1 periods[i] = { "begin": event.end.format("YYYYMMDD"), "begin_pretty": event.end.format("DD/MM/YYYY"), } periods.pop(i) self.periods = periods self.calendar = school_calendar
# from fbprophet import make_holidays as mhdays import json from capacity_planning.forecast.utilities.holidays import events_and_hols as evh from capacity_planning.utilities import sys_utils as s_ut FILE_PATH = os.path.dirname(os.path.abspath(__file__)) country_hols = dict() from workalendar.usa import UnitedStates country_hols['UnitedStates'] = UnitedStates() from workalendar.europe import Russia country_hols['Russia'] = Russia() from workalendar.europe import France country_hols['France'] = France() from workalendar.europe import Belgium country_hols['Belgium'] = Belgium() from workalendar.europe import Spain country_hols['Spain'] = Spain() from workalendar.europe import Germany country_hols['Germany'] = Germany() from workalendar.europe import Austria country_hols['Austria'] = Austria() from workalendar.europe import Italy country_hols['Italy'] = Italy() from workalendar.europe import Portugal country_hols['Portugal'] = Portugal() from workalendar.europe import UnitedKingdom country_hols['UnitedKingdom'] = UnitedKingdom() from workalendar.europe import Ireland