Skip to content

[BUG] Naive model with last strategy is sensitive to trailing NaN values #918

@Flix6x

Description

@Flix6x

Describe the bug

The naive model with "last" strategy really picks the last value, even if that's a NaN value. Can we make this more robust, similar to how the "mean" strategy actually does a nanmean under the hood? I could offer a PR to make it pick the last non-NaN value, taking into account the requested seasonal periodicity.

To Reproduce

import numpy as np
import pandas as pd
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.naive import NaiveForecaster

data = pd.Series(
    index=pd.date_range(
        "2021-06-01 00:00", periods=12, freq="1h", closed="left"
    ),
    data=[1, 2, 3, 1, 2, 3, np.nan, 2, 3, 1, 2, 3]
)
train_data = data[:7]
test_data = data[7:]

print(train_data)
# 2021-06-01 00:00:00    1.0
# 2021-06-01 01:00:00    2.0
# 2021-06-01 02:00:00    3.0
# 2021-06-01 03:00:00    1.0
# 2021-06-01 04:00:00    2.0
# 2021-06-01 05:00:00    3.0
# 2021-06-01 06:00:00    NaN
# Freq: H, dtype: float64

fh = ForecastingHorizon(test_data.index, is_relative=False)
model = NaiveForecaster(strategy="last", sp=3)
model.fit(train_data)
forecast_data = model.predict(fh)

print(forecast_data)
# 2021-06-01 07:00:00    2.0
# 2021-06-01 08:00:00    3.0
# 2021-06-01 09:00:00    NaN  # Let's make this a 1
# 2021-06-01 10:00:00    2.0
# 2021-06-01 11:00:00    3.0
# dtype: float64

Expected behavior

Additional context

Versions

Details

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmodule:forecastingforecasting module: forecasting, incl probabilistic and hierarchical forecasting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions