def test_seasonality_smoke(index, forecast_index): s = Seasonality(12) s.in_sample(index) steps = 83 if forecast_index is None else len(forecast_index) warn = None if ( is_int_index(index) and np.any(np.diff(index) != 1) or ( type(index) is pd.Index and max(index) > 2 ** 63 and forecast_index is None ) ): warn = UserWarning with pytest_warns(warn): s.out_of_sample(steps, index, forecast_index) assert isinstance(s.period, int) str(s) hash(s) if isinstance(index, (pd.DatetimeIndex, pd.PeriodIndex)) and index.freq: s = Seasonality.from_index(index) s.in_sample(index) s.out_of_sample(steps, index, forecast_index) Seasonality.from_index(list(index))
def test_seasonality(index): s = Seasonality(period=12) exog = s.in_sample(index) assert s.is_dummy assert exog.shape == (index.shape[0], 12) pd.testing.assert_index_equal(exog.index, index) assert np.all(exog.sum(1) == 1.0) assert list(exog.columns) == [f"s({i},12)" for i in range(1, 13)] expected = np.zeros((index.shape[0], 12)) for i in range(12): expected[i::12, i] = 1.0 np.testing.assert_equal(expected, np.asarray(exog)) warn = None if type(index) is NumericIndex and np.any(np.diff(index) != 1): warn = UserWarning with pytest.warns(warn): fcast = s.out_of_sample(steps=12, index=index) assert fcast.iloc[0, len(index) % 12] == 1.0 assert np.all(fcast.sum(1) == 1) s = Seasonality(period=7, initial_period=3) exog = s.in_sample(index) assert exog.iloc[0, 2] == 1.0 assert exog.iloc[0].sum() == 1.0 assert s.initial_period == 3 with pytest.raises(ValueError, match="initial_period must be in"): Seasonality(period=12, initial_period=-3) with pytest.raises(ValueError, match="period must be >= 2"): Seasonality(period=1)