def holidays(self): import holidays country_code = self.iso.split('-')[0] if self.region_type == self.STATE: state_code = self.iso.split('-')[1] return holidays.CountryHoliday(country_code, state=state_code) elif self.region_type == self.PROVINCE: prov_code = self.iso.split('-')[1] return holidays.CountryHoliday(country_code, prov=prov_code) return holidays.CountryHoliday(country_code)
def holiday_flag(DTindex, country: str = 'US'): """Create a 0/1 flag for given datetime index. Args: DTindex (panda.DatetimeIndex): DatetimeIndex of dates to create flags country (str): to pass through to python package Holidays Returns: pandas.Series() with DatetimeIndex and column 'HolidayFlag' """ if country.upper() == 'US': try: import holidays country_holidays = holidays.CountryHoliday('US') country_holidays = country_holidays[DTindex[0]:DTindex[-1]] all_days = pd.Series(np.repeat(0, len(DTindex)), index=DTindex) holi_days = pd.Series( np.repeat(1, len(country_holidays)), index=pd.DatetimeIndex(country_holidays), ) holi_days = all_days.combine(holi_days, func=max).fillna(0) holi_days.rename("HolidayFlag", inplace=True) except Exception: from pandas.tseries.holiday import USFederalHolidayCalendar # uses pandas calendar as backup in the event holidays fails holi_days = (USFederalHolidayCalendar().holidays().to_series() [DTindex[0]:DTindex[-1]]) all_days = pd.Series(np.repeat(0, len(DTindex)), index=DTindex) holi_days = pd.Series(np.repeat(1, len(holi_days)), index=holi_days) holi_days = all_days.combine(holi_days, func=max).fillna(0) holi_days.rename("HolidayFlag", inplace=True) else: import holidays country_holidays = holidays.CountryHoliday(country.upper()) country_holidays = country_holidays[DTindex[0]:DTindex[-1]] all_days = pd.Series(np.repeat(0, len(DTindex)), index=DTindex) holi_days = pd.Series( np.repeat(1, len(country_holidays)), index=pd.DatetimeIndex(country_holidays), ) holi_days = all_days.combine(holi_days, func=max).fillna(0) holi_days.rename("HolidayFlag", inplace=True) return holi_days[DTindex]
def get_holidays(tickers_df, ticker_name): country_to_code = { 'Netherlands': 'NLD', 'France': 'FRA', 'Luxembourg': 'LUX', 'Switzerland': 'CHE', 'United Kingdom': 'GBR', 'Ireland': 'IE', 'Russian Federation': 'RUS', 'Mexico': 'MEX', 'Bermuda': 'USA', 'United States': 'USA', 'Germany': 'DE', 'China': 'USA', 'Israel': 'ISR' } holidays_df = holidays.CountryHoliday( country_to_code[tickers_df.loc[ticker_name]['country']], years=YEARS) holidays_df = pd.DataFrame.from_dict(holidays_df, orient="index") holidays_df["ds"] = holidays_df.index holidays_df["holiday"] = holidays_df[0] del holidays_df[0] return holidays_df
def is_holiday(date, country_code): # Requirement: holidays python library (https://pypi.org/project/holidays/) # Install: pip install holidays try: import holidays isCountryFound = False for country in holidays.list_supported_countries(): if country_code == country: print(('Found country code "{}"'.format(country_code))) country_holidays = holidays.CountryHoliday(country_code) isCountryFound = True break if (isCountryFound == False): print(( 'Error: given country code "{}" is not found in supported country codes' .format(country_code))) print(holidays.list_supported_countries()) return False return date.date() in country_holidays except ImportError as err: print('*** Could not import "holidays" Python module ***') print( 'Please install "holidays", or Holidays are not taken into account.' ) print('To install, type pip install holidays') return False
def adjust_holiday(trade_file): country_list = list(trade_file["listing_place"].drop_duplicates()) start_date_dict = defaultdict(list) end_date_dict = defaultdict(list) for country in country_list: this_country_df = trade_file[trade_file["listing_place"] == country].copy() this_holiday = holidays.CountryHoliday(country) holiday_start_date = [start_date for start_date in list(this_country_df["trade_start_date"].drop_duplicates()) if start_date in this_holiday] holiday_end_date = [end_date for end_date in list(this_country_df["trade_end_date"].drop_duplicates()) if end_date in this_holiday] if holiday_start_date != []: for date in holiday_start_date: adjust_date = date + BDay(1) while adjust_date in this_holiday: adjust_date += BDay(1) start_date_dict[date] = [adjust_date] if holiday_end_date != []: for date in holiday_end_date: adjust_date = date - BDay(1) while adjust_date in this_holiday: adjust_date -= BDay(1) end_date_dict[date] = [adjust_date] adjust_num = 0 for adjust_dict in [start_date_dict, end_date_dict]: date_column = "trade_start_date" if adjust_num == 0 else "trade_end_date" for key, value in adjust_dict.items(): adjust_index = trade_file[trade_file[date_column] == key].index trade_file.loc[adjust_index, date_column] = value[0] adjust_num += 1 return trade_file
def transform(self, x: DataFrame, y: Series = None) -> DataFrame: x.loc[:, "measure_date"] = pd.to_datetime( pd.to_datetime(x["date_time"]).dt.date) x.loc[:, "departure_date"] = pd.to_datetime( pd.to_datetime(x["departure_date_time"]).dt.date) x.loc[:, "departure_hour"] = pd.to_datetime( x["departure_date_time"]).dt.hour x.loc[:, "departure_minute"] = pd.to_datetime( x["departure_date_time"]).dt.minute x.loc[:, "arrival_date"] = pd.to_datetime( pd.to_datetime(x["arrival_date_time"]).dt.date) x.loc[:, "arrival_hour"] = pd.to_datetime(x["arrival_date_time"]).dt.hour x.loc[:, "arrival_minute"] = pd.to_datetime( x["arrival_date_time"]).dt.minute ua_holidays = [] for date in holidays.CountryHoliday("UA", prov=None, years=[2020]).items(): ua_holidays.append(str(date[0])) x.loc[:, "is_holiday"] = [ 1 if str(val).split()[0] in ua_holidays else 0 for val in x["departure_date"] ] x.loc[:, "days_left_to_departure"] = ( x["departure_date"] - x["measure_date"]).transform(lambda x: x.dt.days) del x["departure_date_time"] del x["arrival_date_time"] return x
def get_holidays(start='2015-01-01', country='DK', frequency='D'): """ Takes in a start date and a country. Produces a dataframe with a daily date time index and columns: day_of_week - numerical day of the week identifier 0 for monday holiday_bool - boolean true or false for holiday holiday_name - name of the holiday if holiday_bool is true Returns a dataframe """ # get end date end = str(date.today() + timedelta(3)) #generate the range of daily dates dates = pd.date_range(start=start, end=end, freq=frequency) #create the holiday object country_holidays = holidays.CountryHoliday(country) #create a list for the holiday bool and name holiday_list = [] #loop through the dates for d in dates: #true if holiday in object, false otherwise holiday_bool = d in country_holidays holiday_names = country_holidays.get(d) holiday_list.append([holiday_bool, holiday_names]) #create return dataframe holidays_data = pd.DataFrame(holiday_list, index=dates, columns=['holiday', 'holiday_name']) holidays_data.holiday = holidays_data.holiday.astype('int') # add whether it is weekend holidays_data['weekend'] = 0 holidays_data.loc[(holidays_data.index.dayofweek == 5) | (holidays_data.index.dayofweek == 6), 'weekend'] = 1 return holidays_data
def next_business_day(self, date, time_span, time_unit, country_code): ONE_DAY = datetime.timedelta(days=1) HOLIDAYS = holidays.CountryHoliday(country_code) next_day = date + ONE_DAY while next_day.weekday() in holidays.WEEKEND or next_day in HOLIDAYS: next_day += ONE_DAY return next_day
def __init__(self, start_date, end_date, user_id, token): self.start_date = datetime.strptime(start_date, "%Y-%m-%d").date() self.end_date = datetime.strptime(end_date, "%Y-%m-%d").date() self.user_id = user_id self.token = token self.holidays = holidays.CountryHoliday("SE", False) print("Start date: %s, end date: %s, user ID: %s" % (self.start_date, self.end_date, self.user_id))
def holidays_timeseries(time_index, country_code: str, prov: str = None, state: str = None) -> TimeSeries: """ Creates a binary univariate TimeSeries with index `time_index` that equals 1 at every index that lies within (or equals) a selected country's holiday, and 0 otherwise. Available countries can be found `here <https://github.com/dr-prodigy/python-holidays#available-countries>`_. Parameters ---------- country_code The country ISO code prov The province state The state Returns ------- TimeSeries A new binary holiday TimeSeries instance. """ country_holidays = holidays.CountryHoliday(country_code, prov=prov, state=state) scoped_country_holidays = country_holidays[time_index[0]:time_index[-1] + pd.Timedelta(days=1)] index_series = pd.Series(time_index, index=time_index) values = index_series.apply(lambda x: x in scoped_country_holidays).astype( int) return TimeSeries.from_times_and_values(time_index, values)
def add_time_information( inpath=os.path.join("data", "interim", "data_indexed_converted.csv"), outpath=os.path.join("data", "interim", "data_indexed_converted_timed.csv"), ): """Adds columns for year, month, weekday, hour and holiday Keyword Arguments: inpath {path} -- [path for incoming data] (default: {os.path.join("data", "interim", "data_indexed_converted.csv")}) outpath {path} -- [path for where to save data] (default: {os.path.join("data", "interim", "data_indexed_converted_timed.csv")}) """ import holidays fra_holidays = holidays.CountryHoliday("FRA") data = pd.read_csv(inpath, parse_dates=["Date_Time"], index_col="Date_Time") data["year"] = data.index.year data["month"] = data.index.month_name() data["weekday"] = data.index.weekday_name data["hour"] = data.index.hour data["date"] = data.index.date data["holiday"] = data["date"].apply(lambda x: x in fra_holidays) data.drop(columns=["date"], inplace=True) data.to_csv(outpath)
def __init__(self, workweek_schedule=_DEFAULT_WORKWEEK_SCHEDULE, lunch_hour=12, offdays="US"): """Keyword arguments: workweek_schedule -- a dict that defines weekly work hours; has following format { 0: (9, 17), ... 6: () } The key is an int following datetime weekday() function where Monday is 0 and Sunday is 6. The value is a tuple with two ints: the start hour and the end hour in 24 hour format (0 to 23). lunch_hour -- int offdays -- either an ISO country code or tuple of datetime dates to be considered as holidays """ _ok, _errcode = _verify_init(workweek_schedule, lunch_hour, offdays) if not _ok: raise ValueError(_ERRCODE_MESSAGE[_errcode]) if isinstance(offdays, str): self._offdays = holidays.CountryHoliday(offdays) else: self._offdays = offdays self._workweek_schedule = workweek_schedule self._lunch_hour = lunch_hour self._workhour_lookup = _build_workhour_lookup(workweek_schedule, lunch_hour)
def get_items(country, year, subdivision_type, subdivisions): items = [] for subdivision in subdivisions: state = None prov = None if subdivision_type == "state": state = subdivision prov = None elif subdivision_type == "prov": state = None prov = subdivision for ptr in holidays.CountryHoliday(country, state=state, prov=prov, years=year, expand=False).items(): if (subdivision_type == "none"): location = country else: location = '{0}-{1}'.format(country, subdivision) items.append({ "region": location, "name": ptr[1], "date": ptr[0].isoformat(), "req_year": year }) return items
def get_default_holidays(times, country): """Creates default holidays for a specific country. Args: times: a Pandas `DatetimeIndex` that indexes time series data. country: `str`, two-letter upper-case [ISO 3166-1 alpha-2 country code]( https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) for using holiday regressors from a particular country. Returns: holidays: a Pandas `DataFrame` with default holidays relevant to the input times. The `DataFrame` should have the following columns: * `geo`: `str`, two-letter upper-case country code * `holiday`: `str`, the name of the holiday * `date`: `str`, dates in the form of `YYYY-MM-DD` """ # pylint: disable=g-import-not-at-top import pandas as pd import holidays # pylint: enable=g-import-not-at-top years = range(times.min().year, times.max().year + 1) holidays = holidays.CountryHoliday(country, years=years, expand=False) holidays = pd.DataFrame( [(country, holidays.get_list(date), date) for date in holidays], columns=['geo', 'holiday', 'date']) holidays = holidays.explode('holiday') # Ensure that only holiday dates covered by times are used. holidays = holidays[(holidays['date'] >= times.min()) & (holidays['date'] <= times.max())] holidays = holidays.reset_index(drop=True) holidays['date'] = pd.to_datetime(holidays['date']) holidays = holidays.sort_values('date') holidays['date'] = holidays['date'].dt.strftime('%Y-%m-%d') return holidays
def flag_holiday(day, country, prov=None): """Return a flag if the day given is a holiday according to country, province and years in the format required by the holidays package. day: datetime object with with the day that needs to be queried. country: ISO 3166 code for the country. prov: ISO 3166-2 code for the province/state >>> import datetime >>> day1 = datetime.datetime.strptime('2017-12-26', '%Y-%m-%d') >>> flag_holiday(day1, 'CA', 'ON') 1 >>> day2 = datetime.datetime.strptime('2019-07-04', '%Y-%m-%d') >>> flag_holiday(day2, 'US') 1 """ import holidays #Get datetimes holidays_list = list(holidays.CountryHoliday(country=country, years=day.year, prov=prov).keys()) # Format as strings holidays_dates = [day.strftime('%Y-%m-%d') for day in holidays_list] # Compare flag = day.strftime('%Y-%m-%d') in holidays_dates return int(flag)
def is_business_day(date, nation): if nation == "FR": nation = "FRA" try: return date in holidays.CountryHoliday(nation) except KeyError: return False
def holidays_list() -> list: """Return list of holidays for current and upcoming year.""" year = datetime.today().year ca_holidays = holidays.CountryHoliday("US", prov=None, state="CA", years=[year, year + 1]) return [i.isoformat() for i in ca_holidays]
def get_holiday(start_year, end_year, country = 'us'): output_date_list = [] us_holiday = holidays.CountryHoliday('US') year_len = end_year - start_year + 1 for _ in range(year_len): output_date_list.extend(us_holiday[f'{start_year}-01-01':f'{start_year}-12-31']) start_year += 1 return output_date_list
def set_holidays(df, country='US', state='CA', prov=None): """Flag holidays received on weekends.""" holiday_list = holidays.CountryHoliday(country, prov=prov, state=state) is_holiday_func = lambda dttm: 1 if dttm in holiday_list else 0 df['is_holiday'] = df['Received DtTm'].apply(is_holiday_func) return df
def date_is_holiday(array): """ is_holiday([ pd.to_datetime("2015/1/1") ] * 10) """ import holidays , numpy as np jp_holidays = holidays.CountryHoliday('JP') return np.array( [ 1 if x.astype('M8[D]').astype('O') in jp_holidays else 0 for x in array] )
def get_observed_holiday_name(year, month, day): name = holidays.CountryHoliday('US', state='TX').get( datetime.date(year, month, day)) if name and name.replace('(Observed)', '').strip() in Holiday.__OBSERVED_HOLIDAYS: return name else: return None
def working_day(dt: str | datetime, country: str = 'BR', state: str | None = None) -> bool: """ Indices if a day specified by the user is a working day. Parameters ---------- dt : str or datetime Specifies the day the user wants to know if it is a business day. country : str Indicates country to check for vacation days, by default 'BR' state: str Indicates state to check for vacation days, by default None Returns ------- boolean if true, means that the day informed by the user is a working day. if false, means that the day is not a working day. Examples -------- >>> from pymove.utils.datetime import str_to_datetime >>> independence_day = str_to_datetime('2021-09-7 12:00:01') # Holiday in Brazil >>> next_day = str_to_datetime('2021-09-8 12:00:01') # Not a Holiday in Brazil >>> print(working_day(independence_day, 'BR')) False >>> print(type(working_day(independence_day, 'BR'))) <class 'bool'> >>> print(working_day(next_day, 'BR')) True >>> print(type(working_day(next_day, 'BR'))) '<class 'bool'>' References ---------- Countries and States names available in https://pypi.org/project/holidays/ """ result = True if isinstance(dt, str): dt = str_to_datetime(dt) if isinstance(dt, datetime): dt = datetime(dt.year, dt.month, dt.day) if dt in holidays.CountryHoliday(country=country, prov=None, state=state): result = False else: dow = to_day_of_week_int(dt) # 5 == Saturday, 6 == Sunday if dow == 5 or dow == 6: result = False return result
def hdays(df,column_name): holiday_date=[] holis = holidays.CountryHoliday('IND') for i,j in holis.items(): holiday_date.append(i) list_holidays=Series([holis.get(i) for i in df[column_name]]) list_holidays=Series([i if i !=None else 'Normal' for i in list_holidays]) df['holi']=list_holidays return df
def is_holiday(self, hdate): now = datetime.now() #print(now.year) nic_holidays = holidays.CountryHoliday(country='NI', years=now.year) print(hdate) #print(nic_holidays) print("IS HOLIDAY?") print(hdate in nic_holidays) return hdate in nic_holidays
def _holidays(min_date, max_date, countries=None): dates = pd.date_range(min_date, max_date, freq="D") d = {} if countries is None: countries = ['FRA', 'AU', 'US', 'DE', 'custom'] for c in countries: if c != 'custom': h = holidays.CountryHoliday(c) col_name = "is_holiday_{}".format(c.lower()) d[col_name] = dates.map(lambda x: x in h) else: custom_countries = [ 'US', "PL", "AU", "FRA", "DE", "CZ", "SK", "ES", "PT", "CH", "NZ", "UK", "IT", "AT", "BE", "ZA" ] h_set = [holidays.CountryHoliday(c) for c in custom_countries] col_name = "is_holiday_custom" d[col_name] = dates.map(lambda x: any([x in h for h in h_set])) return pd.DataFrame(d, index=dates)
def holidays_timeseries( time_index: pd.DatetimeIndex, country_code: str, prov: str = None, state: str = None, column_name: Optional[str] = "holidays", until: Optional[Union[int, str, pd.Timestamp]] = None, add_length: int = 0, dtype: np.dtype = np.float64, ) -> TimeSeries: """ Creates a binary univariate TimeSeries with index `time_index` that equals 1 at every index that lies within (or equals) a selected country's holiday, and 0 otherwise. Available countries can be found `here <https://github.com/dr-prodigy/python-holidays#available-countries>`_. Parameters ---------- time_index The time index over which to generate the holidays country_code The country ISO code prov The province state The state until Extend the time_index up until timestamp for datetime indexed series and int for range indexed series, should match or exceed forecasting window. add_length Extend the time_index by add_length, should match or exceed forecasting window. Set only one of until and add_length. column_name Optionally, the name of the value column for the returned TimeSeries dtype The desired NumPy dtype (np.float32 or np.float64) for the resulting series Returns ------- TimeSeries A new binary holiday TimeSeries instance. """ time_index = _extend_time_index_until(time_index, until, add_length) scope = range(time_index[0].year, (time_index[-1] + pd.Timedelta(days=1)).year) country_holidays = holidays.CountryHoliday(country_code, prov=prov, state=state, years=scope) index_series = pd.Series(time_index, index=time_index) values = index_series.apply(lambda x: x in country_holidays).astype(dtype) return TimeSeries.from_times_and_values(time_index, values, columns=pd.Index([column_name]))
def ip_localisation(ip, user): response = DbIpCity.get("92.184.112.171", api_key='free') get_holidays = holidays.CountryHoliday(response.country) today = datetime.datetime.today() is_holiday = today.strftime("%Y-%m-%d") in get_holidays Login.objects.create(user=user, location=response.country, is_holiday=is_holiday)
def get_is_holiday_from_series(series: pd.Series, country: str = "UnitedKingdom") -> pd.Series: """Return 1 if day is a public holiday. By default, uses UK holidays, but can specify a country by string name in `country` arg. See `holidays.list_supported_countries()` for list of supported countries. """ years = series.dt.year.unique() holiday_dates = holidays.CountryHoliday(country, years=years) return series.dt.date.isin(holiday_dates).astype(int)
def __init__(self, hass, config): self.config = config self.__name = config.get(CONF_NAME) self.__frequency = config.get(CONF_FREQUENCY) self.__collection_days = config.get(CONF_COLLECTION_DAYS) first_month = config.get(CONF_FIRST_MONTH) if first_month in MONTH_OPTIONS: self.__first_month = MONTH_OPTIONS.index(first_month) + 1 else: self.__first_month = 1 last_month = config.get(CONF_LAST_MONTH) if last_month in MONTH_OPTIONS: self.__last_month = MONTH_OPTIONS.index(last_month) + 1 else: self.__last_month = 12 self._weekday_order_numbers = config.get(CONF_WEEKDAY_ORDER_NUMBER) self._week_order_numbers = config.get(CONF_WEEK_ORDER_NUMBER) self.__monthly_force_week_numbers = bool( self._week_order_numbers is not None and len(self._week_order_numbers) != 0 ) self.__include_dates = to_dates(config.get(CONF_INCLUDE_DATES, [])) self.__exclude_dates = to_dates(config.get(CONF_EXCLUDE_DATES, [])) country_holidays = config.get(CONF_MOVE_COUNTRY_HOLIDAYS) self.__holidays = [] if country_holidays is not None and country_holidays != "": today = dt_util.now().date() this_year = today.year years = [this_year, this_year + 1] try: for date, name in holidays.CountryHoliday( country_holidays, years=years ).items(): if date >= today: self.__holidays.append(date) except KeyError: _LOGGER.error("Invalid country code (%s)", country_holidays) _LOGGER.debug("(%s) Found these holidays %s", self.__name, self.__holidays) self.__period = config.get(CONF_PERIOD) self.__first_week = config.get(CONF_FIRST_WEEK) self.__first_date = to_date(config.get(CONF_FIRST_DATE)) self.__next_date = None self.__today = None self.__days = 0 self.__date = config.get(CONF_DATE) self.__entities = config.get(CONF_ENTITIES) self.__verbose_state = config.get(CONF_VERBOSE_STATE) self.__state = "" if bool(self.__verbose_state) else 2 self.__icon_normal = config.get(CONF_ICON_NORMAL) self.__icon_today = config.get(CONF_ICON_TODAY) self.__icon_tomorrow = config.get(CONF_ICON_TOMORROW) self.__date_format = config.get(CONF_DATE_FORMAT, DEFAULT_DATE_FORMAT) self.__verbose_format = config.get(CONF_VERBOSE_FORMAT, DEFAULT_VERBOSE_FORMAT) self.__icon = self.__icon_normal
def is_work_day(date_time: datetime) -> bool: """ Determine the if the date_time is a business day in BC. Assumes Sat, Sun and all BC stat holidays are non-working days. Requires 3rd party library: holidays==0.10.4 or better """ day_of_week = date_time.strftime("%a") if not (day_of_week == "Sun" or day_of_week == "Sat"): if date_time not in holidays.CountryHoliday('CA', prov='BC'): return True return False