def test_all_holidays_present(self): at_2015 = holidays.AT(years=[2015]) all_holidays = [ "Neujahr", "Heilige Drei Könige", "Ostermontag", "Staatsfeiertag", "Christi Himmelfahrt", "Pfingstmontag", "Fronleichnam", "Mariä Himmelfahrt", "Nationalfeiertag", "Allerheiligen", "Mariä Empfängnis", "Christtag", "Stefanitag", ] for holiday in all_holidays: self.assertIn(holiday, at_2015.values())
def load(country, region, observed, expand, years): # Erases existing holiday cache and makes a new one... global dates if country == "US": dates = holidays.US(state=region, observed=observed, expand=expand, years=years) elif country == "CA": dates = holidays.CA(prov=region, observed=observed, expand=expand, years=years) elif country == "MX": dates = holidays.MX(observed=observed, expand=expand, years=years) elif country == "NZ": dates = holidays.NZ(prov=region, observed=observed, expand=expand, years=years) elif country == "AU": dates = holidays.AU(prov=region, observed=observed, expand=expand, years=years) elif country == "AT": dates = holidays.AT(prov=region, observed=observed, expand=expand, years=years) elif country == "DE": dates = holidays.DE(prov=region, observed=observed, expand=expand, years=years) else: print "UNKNOWN COUNTRY ", country
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 ts_fit(self, suppress=False): """Fit Prophet to the time series data. Parameters: ---------- suppress: bool Suppress or not some of the output messages """ if self.hyper_params is not None: self._gs.set_forecaster(self) self._gs.set_hyper_params(self.hyper_params) # a very important command here to avoid endless loop self.hyper_params = None self._prophet_logger.info("***** Starting grid search *****") self._gs = self._gs.grid_search(suppress=suppress, show_plot=False) # self.best_model = self._gs.best_model self.__dict__.update(self.best_model['forecaster'].__dict__) self._prophet_logger.info("***** Finished grid search *****") else: self._prepare_fit() self._model = None self.ts_split() ts_df = self._train_dt.copy() ts_test_df = self._test_dt # sanity check if 'on_weekend' in ts_df.columns: ts_df.drop(['on_weekend', 'off_weekend'], inplace=True, axis=1) # ts_test_df.drop(['on_weekend', 'off_weekend'], inplace=True, axis=1) # Fit self._prophet_logger.info("Trying to fit the Prophet model....") try: if not suppress: self._prophet_logger.info("...via using parameters\n") print_attributes(self) # diagnose on? if self._diagnose: try: assert self._step is not None and self._horizon is not None except (KeyError, AssertionError): self._prophet_logger.warning("You want to diagnose the Prophet model. Please provide parameters " "'step' and 'horizon' within object initialization!") sys.exit("STOP") ts_df = ts_df.reset_index() ts_df.columns = self._ts_df_cols if ts_test_df is not None and not ts_test_df.empty: ts_test_df = ts_test_df.reset_index() ts_test_df.columns = self._ts_df_cols # weekly_s = self._weekly_seasonality if self._weekend_seasonality: # force to False weekly_s = False # if not self._consider_holidays: self._model = Prophet(interval_width=self._prophet_interval_width, yearly_seasonality=self._yearly_seasonality, weekly_seasonality=weekly_s, daily_seasonality=self._daily_seasonality, changepoint_range=self._changepoint_range, changepoint_prior_scale=self._changepoint_prior_scale) else: try: assert self._country in ['AT', 'DE', 'US'] except AssertionError: self._prophet_logger.exception("Assrtion exception occurred. Right now, Austria (AT), " "Germany(DE) and USA (US) supported.") sys.exit("STOP") else: holi = None if self._country == 'AT': holi = holidays.AT(state=None, years=list(np.unique(np.asarray(self.ts_df.index.year)))) elif self._country == 'DE': holi = holidays.DE(state=None, years=list(np.unique(np.asarray(self.ts_df.index.year)))) elif self._country == 'US': holi = holidays.US(state=None, years=list(np.unique(np.asarray(self.ts_df.index.year)))) # holi_dict = dict() for date, name in sorted(holi.items()): holi_dict[date] = name df_holi = pd.DataFrame.from_dict(data=holi_dict, orient='index').reset_index() df_holi.columns = ['ds', 'holiday'] df_holi['lower_window'] = 0 df_holi['upper_window'] = 0 self._model = Prophet(interval_width=self._prophet_interval_width, yearly_seasonality=self._yearly_seasonality, weekly_seasonality=weekly_s, daily_seasonality=self._daily_seasonality, changepoint_range=self._changepoint_range, changepoint_prior_scale=self._changepoint_prior_scale, holidays=df_holi) if self._monthly_seasonality: self._model.add_seasonality(name='monthly', period=30.5, fourier_order=20) if not suppress: self._prophet_logger.info("Added monthly seasonality.") if self._quarterly_seasonality: self._model.add_seasonality(name='quarterly', period=91.5, fourier_order=20) if not suppress: self._prophet_logger.info("Added quarterly seasonality.") if self._weekend_seasonality: ts_df['on_weekend'] = ts_df['ds'].apply(self.we_season) ts_df['off_weekend'] = ~ts_df['ds'].apply(self.we_season) self._train_dt = ts_df.copy() self._train_dt.set_index('ds', inplace=True) # if ts_test_df is not None and not ts_test_df.empty: ts_test_df['on_weekend'] = ts_test_df['ds'].apply(self.we_season) ts_test_df['off_weekend'] = ~ts_test_df['ds'].apply(self.we_season) self._test_dt = ts_test_df.copy() self._test_dt.set_index('ds', inplace=True) # and add self._model.add_seasonality(name='weekend_on_season', period=7, fourier_order=5, condition_name='on_weekend') self._model.add_seasonality(name='weekend_off_season', period=7, fourier_order=5, condition_name='off_weekend') if not suppress: self._prophet_logger.info("Added week-end seasonality.") # tic start = time() self.model_fit = self._model.fit(ts_df) # toc if not suppress: self._prophet_logger.info("Time elapsed: {} sec.".format(time() - start)) except (Exception, ValueError): self._prophet_logger.exception("Prophet error...") return -1 else: self._prophet_logger.info("Model successfully fitted to the data!") # Fitted values self._prophet_logger.info("Computing fitted values and residuals...") # in-sample predict try: self.fittedvalues = self._model.predict(ts_df.drop('y', axis=1)) except (Exception, ValueError): self._prophet_logger.exception("Prophet predict error...") # Residuals try: # use fittedvalues to fill in the model dictionary self.residuals = pd.Series(np.asarray(ts_df.y) - np.asarray(self.fittedvalues['yhat']), index=self._train_dt.index) except (KeyError, AttributeError): self._prophet_logger.exception("Model was not fitted or ts has other structure...") # self.lower_conf_int = pd.Series(np.asarray(self.fittedvalues['yhat_lower']), index=self._train_dt.index) self.upper_conf_int = pd.Series(np.asarray(self.fittedvalues['yhat_upper']), index=self._train_dt.index) self._prophet_logger.info("Done.") return self
settings["country_last_updated"] = now settings["country_last"] = country_last settings.flush() return country_last country_holidays = { "CA": holidays.CA(), "CO": holidays.CO(), "MX": holidays.MX(), "US": holidays.US(), "NZ": holidays.NZ(), "AU": holidays.AU(), "DE": holidays.DE(), "AT": holidays.AT(), "DK": holidays.DK(), "UK": holidays.UK(), "IE": holidays.IE(), "ES": holidays.ES(), "CZ": holidays.CZ(), "SK": holidays.SK(), "PL": holidays.PL(), "PT": holidays.PT(), "NL": holidays.NL(), "NO": holidays.NO(), "IT": holidays.IT(), "SE": holidays.SE(), "JP": holidays.JP(), "BE": holidays.BE(), "ZA": holidays.ZA(),
def setUp(self): self.holidays = holidays.AT()