Construct a dataframe of holiday features. Parameters ---------- dates: pd.Series containing timestamps used for computing seasonality. holidays: pd.Dataframe containing holidays, as returned by construct_holiday_dataframe. Returns ------
(self, dates, holidays)
| 558 | return all_holidays |
| 559 | |
| 560 | def make_holiday_features(self, dates, holidays): |
| 561 | """Construct a dataframe of holiday features. |
| 562 | |
| 563 | Parameters |
| 564 | ---------- |
| 565 | dates: pd.Series containing timestamps used for computing seasonality. |
| 566 | holidays: pd.Dataframe containing holidays, as returned by |
| 567 | construct_holiday_dataframe. |
| 568 | |
| 569 | Returns |
| 570 | ------- |
| 571 | holiday_features: pd.DataFrame with a column for each holiday. |
| 572 | prior_scale_list: List of prior scales for each holiday column. |
| 573 | holiday_names: List of names of holidays |
| 574 | """ |
| 575 | # Holds columns of our future matrix. |
| 576 | expanded_holidays = defaultdict(lambda: np.zeros(dates.shape[0])) |
| 577 | prior_scales = {} |
| 578 | # Makes an index so we can perform `get_loc` below. |
| 579 | # Strip to just dates. |
| 580 | row_index = pd.DatetimeIndex(dates.dt.date) |
| 581 | |
| 582 | for row in holidays.itertuples(): |
| 583 | dt = row.ds.date() |
| 584 | try: |
| 585 | lw = int(getattr(row, 'lower_window', 0)) |
| 586 | uw = int(getattr(row, 'upper_window', 0)) |
| 587 | except ValueError: |
| 588 | lw = 0 |
| 589 | uw = 0 |
| 590 | ps = float(getattr(row, 'prior_scale', self.holidays_prior_scale)) |
| 591 | if np.isnan(ps): |
| 592 | ps = float(self.holidays_prior_scale) |
| 593 | if row.holiday in prior_scales and prior_scales[row.holiday] != ps: |
| 594 | raise ValueError( |
| 595 | 'Holiday {holiday!r} does not have consistent prior ' |
| 596 | 'scale specification.'.format(holiday=row.holiday) |
| 597 | ) |
| 598 | if ps <= 0: |
| 599 | raise ValueError('Prior scale must be > 0') |
| 600 | prior_scales[row.holiday] = ps |
| 601 | |
| 602 | for offset in range(lw, uw + 1): |
| 603 | occurrence = pd.to_datetime(dt + timedelta(days=offset)) |
| 604 | try: |
| 605 | loc = row_index.get_loc(occurrence) |
| 606 | except KeyError: |
| 607 | loc = None |
| 608 | key = '{}_delim_{}{}'.format( |
| 609 | row.holiday, |
| 610 | '+' if offset >= 0 else '-', |
| 611 | abs(offset) |
| 612 | ) |
| 613 | if loc is not None: |
| 614 | expanded_holidays[key][loc] = 1. |
| 615 | else: |
| 616 | expanded_holidays[key] # Access key to generate value |
| 617 | holiday_features = pd.DataFrame(expanded_holidays) |
no outgoing calls