Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not fail if holidays are missing in workalendar #77

Open
wants to merge 1 commit into
base: python-update
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/hcrystalball/feature_extraction/_holiday_transformer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import pandas as pd
from sklearn.base import BaseEstimator
from sklearn.base import TransformerMixin
Expand Down Expand Up @@ -67,7 +69,7 @@ def get_feature_names(self):
@unified_country_code.setter
def unified_country_code(self, value):
if value is not None and value not in list(registry.region_registry.keys()):
raise ValueError("Unknown `country_code`. For list of valid codes please look at workalendar.")
logging.warning("Unknown `country_code`. For list of valid codes please look at workalendar.")
self._unified_country_code = value

def fit(self, X, y=None):
Expand Down Expand Up @@ -141,14 +143,21 @@ def transform(self, X, y=None):
self.unified_country_code = X[self.country_code_column].unique()[0]

years = X.index.year.unique().tolist() + [max(X.index.year)]
cal = registry.region_registry[self.unified_country_code]()
holidays = (
pd.concat(
[pd.DataFrame(data=cal.holidays(year), columns=["date", self._col_name]) for year in years]
try:
cal = registry.region_registry[self.unified_country_code]()
holidays = (
pd.concat(
[
pd.DataFrame(data=cal.holidays(year), columns=["date", self._col_name])
for year in years
]
)
# one day could have multiple public holidays
.drop_duplicates(subset="date").set_index("date")
)
# one day could have multiple public holidays
.drop_duplicates(subset="date").set_index("date")
)
except KeyError:
logging.warning("HolidayTransformer: No holidays found for %s", self.unified_country_code)
holidays = pd.DataFrame(columns=["date", self._col_name]).set_index("date")

df = (
pd.merge(X, holidays, left_index=True, right_index=True, how="left")
Expand Down