Example #1
0
    def get_holidays(self, date=None):
        """
        Return a dict with every Holidays at the date passed in parameters.
        If no parameter is passed, the function default to today's date.

        Expected argument type: datetime.date
        """

        if not date:
            date = self.local_now.date()

        holidays_today = {
            "French Holiday A Zone": False,
            "French Holiday B Zone": False,
            "French Holiday C Zone": False
        }

        holidays_today["French Public Holiday"] = True if date in holidays.France() else False

        # Get french public holidays per zone
        holidays_dict = requests.get(self.FRENCH_VACATION_URL, headers=self.HEADERS).json()
        holidays_dict = [holidays_item["fields"] for holidays_item in holidays_dict if "Zone" in holidays_item["fields"]["zones"]]
        for holidays_period in holidays_dict:
            if datetime.date.fromisoformat(holidays_period["start_date"]) <= date < datetime.date.fromisoformat(holidays_period["end_date"]):
                for zone in ("A", "B", "C"):
                    if zone in holidays_period["zones"]:
                        holidays_today[f"French Holiday {zone} Zone"] = True

        return holidays_today
Example #2
0
def daily_mail(force=False):
    if date.today() in holidays.France() and not force:
        return

    for caisse in Caisse.objects.filter(active=True):
        mrsrequests = caisse.mrsrequest_set.all().status('new').order_by(
            'creation_datetime')

        if not len(mrsrequests):
            continue

        context = dict(
            object_list=mrsrequests,
            BASE_URL=settings.BASE_URL,
            ADMIN_ROOT=crudlfap.site.views['home'].url,
        )

        Caller(
            callback='djcall.django.email_send',
            kwargs=dict(
                subject=template.loader.get_template(
                    'caisse/liquidation_daily_mail_title.txt',
                ).render(context).strip(),
                body=template.loader.get_template(
                    'caisse/liquidation_daily_mail_body.html',
                ).render(context).strip(),
                to=[caisse.liquidation_email],
                reply_to=[settings.DEFAULT_FROM_EMAIL],
                content_subtype='html'
            )
        ).spool('mail')
Example #3
0
def is_holiday(df):
    ''' This function create holiday features and add it to the DataFrame.
    if the input is a holiday day it returns 1 else it returns 0'''

    fr_holidays = holidays.France()
    df['isHoliday'] = df.timestamp.apply(lambda x: 1
                                         if x in fr_holidays else 0)
Example #4
0
 def __add_bank_holidays_columns__(self, df) :
     new_df = df.copy()
     years_list = new_df.loc[:, cf.DATE_COL].apply(lambda row : row.year).unique()
     hols = []
     for d in holidays.France(years=years_list).items() :
          hols.append(d[0])
     new_df[cf.IS_BANK_HOLIDAY_COL] = new_df[cf.DATE_COL].apply(lambda row : row in hols)
     return new_df
Example #5
0
    def transform(self, x, y=None):
        fr_holidays = holidays.France()
        x["weekday"] = x.index.dayofweek
        x["month"] = x.index.month
        x["hour"] = x.index.hour
        x["is_weekend"] = (x["weekday"] > 4) * 1
        x["is_holidays"] = (
            x.index.to_series().apply(lambda t: t in fr_holidays)) * 1

        x["is_TVtime"] = ((x.hour > 17) & (x.hour < 23)) * 1
        # X_train["is_working_hour"] = ((X_train.hour>7) & (X_train.hour<19))*1
        x["is_night"] = ((x.hour > 0) & (x.hour < 7)) * 1

        x['lag_1'] = x['consumption'].shift(1)
        x['lag_5'] = x['consumption'].shift(5)
        x['lag_10'] = x['consumption'].shift(10)
        x['lag_20'] = x['consumption'].shift(20)
        x['lag_25'] = x['consumption'].shift(25)
        x['lag_30'] = x['consumption'].shift(30)
        x['lag_35'] = x['consumption'].shift(35)
        x['lag_40'] = x['consumption'].shift(40)

        x['lag_future_1'] = x['consumption'].shift(-1)
        x['lag_future_5'] = x['consumption'].shift(-5)
        x['lag_future_10'] = x['consumption'].shift(-10)
        x['lag_future_20'] = x['consumption'].shift(-20)
        x['lag_future_25'] = x['consumption'].shift(-25)
        x['lag_future_30'] = x['consumption'].shift(-30)
        x['lag_future_35'] = x['consumption'].shift(-35)
        x['lag_future_40'] = x['consumption'].shift(-40)

        x['rolling_mean_5'] = x['consumption'].rolling(window=5).mean()
        x['rolling_mean_15'] = x['consumption'].rolling(window=15).mean()
        x['rolling_mean_-5'] = x['consumption'].rolling(
            window=5).mean().shift(-5)
        x['rolling_mean_-15'] = x['consumption'].rolling(
            window=15).mean().shift(-15)

        x['rolling_std_5'] = x['consumption'].rolling(window=5).std()
        x['rolling_std_15'] = x['consumption'].rolling(window=15).std()
        x['rolling_std_-5'] = x['consumption'].rolling(
            window=5).std().shift(-5)
        x['rolling_std_-55'] = x['consumption'].rolling(
            window=15).std().shift(-15)

        x['rolling_max_10'] = x['consumption'].rolling(window=10).max()
        #         x['rolling_max_5'] = x['consumption'].rolling(window=10).max()
        x['rolling_min_10'] = x['consumption'].rolling(window=10).min()
        x = x.ffill().bfill()

        return x
Example #6
0
    def transform(self, x, y=None):
        fr_holidays = holidays.France()
        x["weekday"] = x.index.dayofweek
        x["month"] = x.index.month
        x["hour"] = x.index.hour
        x["is_weekend"] = (x["weekday"] > 4) * 1
        x["is_holidays"] = (
            x.index.to_series().apply(lambda t: t in fr_holidays)) * 1

        x["is_breakfast"] = ((x.hour > 5) & (x.hour < 9)) * 1
        x["is_teatime"] = ((x.hour > 16) & (x.hour < 20)) * 1
        x["is_TVtime"] = ((x.hour > 17) & (x.hour < 23)) * 1
        # X_train["is_working_hour"] = ((X_train.hour>7) & (X_train.hour<19))*1
        x["is_night"] = ((x.hour > 0) & (x.hour < 7)) * 1
        return x
Example #7
0
def add_time_features(df):
    # # Time features: wwejday, month, day, hour
    # selection time - decomposition
    # Monday=0, Sunday=6
    df['selection time'] = pd.to_datetime(df['selection time'],
                                          infer_datetime_format=True)
    df['selection_weekday'] = df['selection time'].dt.dayofweek
    df['selection_month'] = df['selection time'].dt.month
    df['selection_day'] = df['selection time'].dt.day
    df['selection_hour'] = df['selection time'].dt.hour

    # Holidays
    years = df['selection time'].dt.year.unique()
    import holidays
    holidays = holidays.France(years=years)
    df['selection_is_holiday'] = df.apply(
        axis=1, func=lambda x: x['selection time'] in holidays)
    return df
Example #8
0
    def transform(self, x, y=None):
        fr_holidays = holidays.France()
#         x["weekday"] = x.index.dayofweek
        x["month"] = x.index.month
        x["hour"] = x.index.hour
#         x["is_weekend"] = (x["weekday"] > 4) * 1
        x["is_holidays"] = (x.index.to_series().apply(lambda t: t in fr_holidays)) * 1
        
#         x['month_sin'] = np.sin((x.month-1)*(2.*np.pi/12))
#         x['month_cos'] = np.cos((x.month-1)*(2.*np.pi/12))

        x["is_breakfast"] = ((x.hour > 5) & (x.hour < 9)) * 1
        x["is_teatime"] = ((x.hour > 16) & (x.hour < 20)) * 1
        
        x['lag_1'] = x['consumption'].shift(1)
        x['lag_2'] = x['consumption'].shift(2)
        x['lag_3'] = x['consumption'].shift(3)
        x['lag_4'] = x['consumption'].shift(4)
        x['lag_5'] = x['consumption'].shift(5)
        x['lag_10'] = x['consumption'].shift(10)
        x['lag_20'] = x['consumption'].shift(20)
        x['lag_future_1'] = x['consumption'].shift(-1)
        x['lag_future_2'] = x['consumption'].shift(-2)
        x['lag_future_3'] = x['consumption'].shift(-3)
        x['lag_future_4'] = x['consumption'].shift(-4)
        x['lag_future_5'] = x['consumption'].shift(-5)
        x['lag_future_10'] = x['consumption'].shift(-10)
        x['lag_future_20'] = x['consumption'].shift(-20)

        x['rolling_mean_5'] = x['consumption'].rolling(window=3).mean()
        x['rolling_mean_-5'] = x['consumption'].rolling(window=3).mean().shift(-3)

#         x['rolling_std_3'] = x['consumption'].rolling(window=3).std()
#         x['rolling_std_-3'] = x['consumption'].rolling(window=3).std().shift(-3)

#         x['rolling_max_3'] = x['consumption'].rolling(window=3).max()
#         x['rolling_min_3'] = x['consumption'].rolling(window=3).min()
    
        x = x.ffill().bfill()
        
        return x
    'DE': 81.750,
    'DK': 5.535,
    'FR': 64.610,
    'LU': 0.502,
    'NL': 16.570,
    'PL': 38.53,
    'SE': 9.341
}
holiday_op = [
    ('AT', holidays.Austria()),  # holidays for each country
    ('BE', holidays.Belgium()),  # implented this way because of bug in library
    ('CH', holidays.Switzerland()),
    ('CZ', holidays.Czech()),
    ('DE', holidays.Germany()),
    ('DK', holidays.Denmark()),
    ('FR', holidays.France()),
    ('LU', holidays.Luxembourg()),
    ('NL', holidays.Netherlands()),
    ('PL', holidays.Poland()),
    ('SE', holidays.Sweden())
]


def timedata(dt):
    # create time data extra information for influxDB (makes sorting easier)
    timestamp = datetime.fromtimestamp(dt)
    year = timestamp.year
    month = timestamp.month
    hour = timestamp.hour
    minute = timestamp.minute
    weekday = timestamp.weekday()
Example #10
0
    def transform(self, x, y=None):
        fr_holidays = holidays.France()
        x["weekday"] = x.index.dayofweek
        x["month"] = x.index.month
        x["hour"] = x.index.hour
        x["is_weekend"] = (x["weekday"] > 4) * 1
        x["is_holidays"] = (
            x.index.to_series().apply(lambda t: t in fr_holidays)) * 1

        x['weekday_sin'] = np.sin((x.weekday) * (2. * np.pi / 7))
        x['weekday_cos'] = np.cos((x.weekday) * (2. * np.pi / 7))
        x['month_sin'] = np.sin((x.month - 1) * (2. * np.pi / 12))
        x['month_cos'] = np.cos((x.month - 1) * (2. * np.pi / 12))

        x["is_TVtime"] = ((x.hour > 17) & (x.hour < 23)) * 1
        # X_train["is_working_hour"] = ((X_train.hour>7) & (X_train.hour<19))*1
        x["is_night"] = ((x.hour > 0) & (x.hour < 7)) * 1

        x['lag_1'] = x['consumption'].shift(1)
        x['lag_2'] = x['consumption'].shift(2)
        x['lag_3'] = x['consumption'].shift(3)
        x['lag_4'] = x['consumption'].shift(4)
        x['lag_5'] = x['consumption'].shift(5)
        x['lag_10'] = x['consumption'].shift(10)
        x['lag_20'] = x['consumption'].shift(20)
        x['lag_future_1'] = x['consumption'].shift(-1)
        x['lag_future_2'] = x['consumption'].shift(-2)
        x['lag_future_3'] = x['consumption'].shift(-3)
        x['lag_future_4'] = x['consumption'].shift(-4)
        x['lag_future_5'] = x['consumption'].shift(-5)
        x['lag_future_10'] = x['consumption'].shift(-10)
        x['lag_future_20'] = x['consumption'].shift(-20)

        #         x['mean_3'] = (x['consumption'].rolling(1).sum().values
        # + x['consumption'].rolling(1).sum().shift(-1).values) / 3
        #         x['mean_5'] = (x['consumption'].rolling(2).sum().values
        # + x['consumption'].rolling(2).sum().shift(-2).values) / 5
        #         x['mean_10'] = (x['consumption'].rolling(5).sum().values
        # + x['consumption'].rolling(5).sum().shift(-5).values) / 10
        #         x['mean_20'] = (x['consumption'].rolling(10).sum().values
        # + x['consumption'].rolling(10).sum().shift(-10).values) / 20
        #         x['mean_30'] = (x['consumption'].rolling(15).sum().values
        # + x['consumption'].rolling(15).sum().shift(-15).values) / 30
        #         x['mean_41'] = (x['consumption'].rolling(20).sum() + x['consumption'].rolling(20).sum().shift(-20)) / 41
        #         x['mean_61'] = (x['consumption'].rolling(30).sum() + x['consumption'].rolling(30).sum().shift(-30)) / 61

        x['rolling_mean_5'] = x['consumption'].rolling(window=5).mean()
        x['rolling_mean_15'] = x['consumption'].rolling(window=15).mean()
        x['rolling_mean_-5'] = x['consumption'].rolling(
            window=5).mean().shift(-5)
        x['rolling_mean_-15'] = x['consumption'].rolling(
            window=15).mean().shift(-15)

        x['rolling_std_3'] = x['consumption'].rolling(window=3).std()
        x['rolling_std_5'] = x['consumption'].rolling(window=5).std()
        x['rolling_std_-3'] = x['consumption'].rolling(
            window=3).std().shift(-3)
        x['rolling_std_-5'] = x['consumption'].rolling(
            window=5).std().shift(-5)

        x['rolling_max_3'] = x['consumption'].rolling(window=5).max()
        #         x['rolling_max_5'] = x['consumption'].rolling(window=10).max()
        x['rolling_min_3'] = x['consumption'].rolling(window=5).min()
        x = x.ffill().bfill()

        return x
Example #11
0
def load_call_data():

    halfhours = pd.read_csv("Appels entrants PFT par 0.5h  2017 - 2018.csv",
                            names=["date", "time", "numcalls"])

    # Merge date & time into a datetime object and set it as
    # the index for the dataframe.
    halfhours["date_time"] = pd.to_datetime(halfhours["date"] + ' ' + halfhours["time"], dayfirst=True)
    halfhours = halfhours.set_index("date_time")

    # Make the time column a dt.time object
    halfhours["date"] = pd.to_datetime(halfhours.date, dayfirst=True)
    halfhours["time"] = pd.to_datetime(halfhours.time)

    # Remove saturday afternoons
    isASaturday = halfhours.index.day_name() == "Saturday"
    after1pm = halfhours.index.hour >= 13
    halfhours[isASaturday & after1pm] = np.NaN

    # Remove bizarre Sunday which is thrown in
    halfhours["2017-04-02"] = np.NaN

    # Remove holidays and pseudo-holidays
    pseudoHolidays = ["2015-12-26", "2016-01-02", "2016-12-24",
                  "2016-12-31", "2017-07-15", "2017-12-23",
                 "2017-12-30"]

    dateAHoliday = np.array([d in holidays.France() or str(d) in pseudoHolidays for d in halfhours.index.date])
    halfhours["holiday"] = False
    halfhours.loc[dateAHoliday, "holiday"] = True
    halfhours["holiday"] = halfhours["holiday"].astype(np.bool)

    # Handle other early-closing/late-opening quirks
    halfhours.loc["2017-09-22 11:30":"2017-09-22","holiday"] = True
    halfhours.loc["2018-10-12 12:00":"2018-10-12","holiday"] = True
    halfhours.loc["2018-12-24 16:00":"2018-12-24","holiday"] = True
    halfhours.loc["2018-12-31 16:00":"2018-12-31","holiday"] = True
    halfhours.loc["2017-02-07 17:00":"2017-02-07","holiday"] = True
    halfhours.loc["2018-8-17":"2018-8-17 11:59","holiday"] = True

    mydateparser = lambda x: dt.datetime.strptime(x, "%d/%m/%y")
    daily = pd.read_csv("Appels entrants PFT par jour 2015 - 2018.csv", delimiter=";",
                        names=["date", "numcalls"], parse_dates=["date"], date_parser=mydateparser)

    idx = pd.date_range(daily.date.min(), daily.date.max())
    daily.index = pd.DatetimeIndex(daily.date)
    daily = daily.reindex(idx, fill_value=np.NaN)[["numcalls"]]

    # Remove bizarre Sunday which is thrown in
    daily.loc["2017-04-02"] = np.NaN

    pseudoHolidays = ["2015-12-26", "2016-01-02", "2016-12-24", "2016-12-31",
                      "2017-07-15", "2017-12-23", "2017-12-30"]

    dateAHoliday = np.array([d in holidays.France() or str(d) in pseudoHolidays for d in daily.index.date])
    # daily[dateAHoliday] = np.NaN
    daily["holiday"] = False
    daily.loc[dateAHoliday, "holiday"] = True
    daily["holiday"] = daily["holiday"].astype(np.bool)

    return halfhours, daily
# week that people buy the presents for the mother's day
number_of_days_after_black_friday = 3
number_of_days_before_dia_de_la_madre = 5

#Create list of dates with a 1 day step
datelist = pd.date_range(datetime(Starting_Year, 1, 1),
                         datetime(Last_Year, 1, 1)).strftime('%Y-%m-%d')

#create df from where we will write
holidays_table_df = pd.DataFrame({'ds': datelist})

#load calendars
holidays_Spain_library = holidays.Spain()
holidays_Portugal_library = holidays.Portugal()
holidays_Mexico_library = holidays.Mexico()
holidays_France_library = holidays.France()
""" Add Black Friday and Dia de la Madre """
# compute the days to add

# ----- Black Friday-----
# We predict the 4th Thursday of November which is THanksGiving
# And then we add 1 day.
# IMPORTANT! Predicting the 4th Friday Would be Different!
# 4th friday is between the 22 and 28 inclusive
# Day after 4th Thursday is between the 23 and 29 inclusive

# We consider the Weekend and cyberMonday also as
# importan (upper_window = 3)

black_friday_list = []
Example #13
0
# Poland Holidays - 2021
# for day in sorted(holidays.Poland(years=2021).items()):
#     print(day)

# USA Holidays - 2021
# for day in sorted(holidays.USA(years=2021).items()):
#     print(day)

# print('--------------------------------------')

# for day, name in sorted(holidays.USA(years=2021, state='CA').items()):
#     print(day, name)

# Russia Holidays - 2021
# for day in sorted(holidays.Russia(years=2021).items()):
#     print(day)

# Check whether a given date is a public holiday or not
usa_holidays = holidays.UnitedStates()
poland_holidays = holidays.Poland()
france_holidays = holidays.France()

print('01-01-2021' in usa_holidays)
print('01-01-2021' in poland_holidays)
print('01-01-2021' in france_holidays)

print(usa_holidays.get('01-01-2021'))
print(poland_holidays.get('01-01-2021'))
print(france_holidays.get('01-01-2021'))
Example #14
0
        """
        def __init__(self, option_strings, dest, **kwargs):
            super(SetDate, self).__init__(option_strings, dest, **kwargs)

        def __call__(self, parsobj, namespace, values, option_string=None):
            setattr(namespace, self.dest, parse(values))

    locale.setlocale(locale.LC_ALL, "")

    parser = argparse.ArgumentParser()
    parser.add_argument("--date", default=date.today(), action=SetDate)
    parser.add_argument("days", type=int)
    subparsers = parser.add_subparsers(dest="operation")
    parser_add = subparsers.add_parser("add")
    parser_add.set_defaults(func=operator.add)
    parser_sub = subparsers.add_parser("sub")
    parser_sub.set_defaults(func=operator.sub)

    arguments = parser.parse_args()
    iterator = timedelta_(datobj=arguments.date,
                          days=arguments.days,
                          kallable=arguments.func)
    iter1, iter2, iter3 = tee(iterator, 3)
    for item in islice(iter1, 1):
        print(item.strftime("%A %d/%m/%Y"))
    for item in islice(iter2, 1, None):
        print(item)
    fr_holidays = holidays.France()
    (new_date, ) = tuple(islice(iter3, 1))
    print(new_date in fr_holidays)
Example #15
0
 def setUp(self):
     self.holidays = holidays.France()
     self.prov_holidays = {
         prov: holidays.FR(prov=prov)
         for prov in holidays.FRA.PROVINCES
     }