Nixtla/hierarchicalforecast

TypeError: can only concatenate str (not "pandas._libs.tslibs.offsets.MonthEnd") to str

Closed this issue · 5 comments

Getting the above error in running the example from the Readme.MD it self:

# compute base forecast no coherent
from statsforecast.core import StatsForecast
from statsforecast.models import AutoARIMA, Naive

#obtain hierarchical reconciliation methods and evaluation
from hierarchicalforecast.core import HierarchicalReconciliation
from hierarchicalforecast.methods import BottomUp, TopDown, MiddleOut


#obtain hierarchical datasets
from datasetsforecast.hierarchical import HierarchicalData

# Load TourismSmall dataset
Y_df, S, tags = HierarchicalData.load('./data', 'TourismSmall')
Y_df['ds'] = pd.to_datetime(Y_df['ds'])


# Compute base level predictions 
sf = StatsForecast(df=Y_df, 
                   models=[AutoARIMA(season_length=12), Naive()], 
                   freq='M', n_jobs=-1)

When calling forecast, I get the error.

forecasts_df = sf.forecast(h=12)

TypeError                                 Traceback (most recent call last)
Cell In [8], line 1
----> 1 forecasts_df = sf.forecast(h=12)

File ~/opt/anaconda3/envs/fcst/lib/python3.9/site-packages/statsforecast/core.py:514, in StatsForecast.forecast(self, h, df, X_df, level, fitted, sort_df)
    512 fcsts = res_fcsts['forecasts']
    513 cols = res_fcsts['cols']
--> 514 fcsts_df = self._make_future_df(h=h)
    515 fcsts_df[cols] = fcsts
    516 return fcsts_df

File ~/opt/anaconda3/envs/fcst/lib/python3.9/site-packages/statsforecast/core.py:388, in StatsForecast._make_future_df(self, h)
    386     last_date_f = lambda x: pd.date_range(x + self.freq, periods=h, freq=self.freq)
    387 if len(np.unique(self.last_dates)) == 1:
--> 388     dates = np.tile(last_date_f(self.last_dates[0]), len(self.ga))
    389 else:
    390     dates = np.hstack([
    391         last_date_f(last_date)
    392         for last_date in self.last_dates            
    393     ])

File ~/opt/anaconda3/envs/fcst/lib/python3.9/site-packages/statsforecast/core.py:386, in StatsForecast._make_future_df.<locals>.<lambda>(x)
    384     last_date_f = lambda x: np.arange(x + 1, x + 1 + h, dtype=self.last_dates.dtype)
    385 else:
--> 386     last_date_f = lambda x: pd.date_range(x + self.freq, periods=h, freq=self.freq)
    387 if len(np.unique(self.last_dates)) == 1:
    388     dates = np.tile(last_date_f(self.last_dates[0]), len(self.ga))

TypeError: can only concatenate str (not "pandas._libs.tslibs.offsets.MonthEnd") to str

I have same problem
Managed to solve?

hey @pankaj-kvhld and @rafaepires! I think the problem is related to the version of statsforecast you're using. Could you try updating the version using pip install -U statsforecast? I tested the code using colab and it works as expected. Here you can reproduce it.

Same issue any updates here?

Hey @anonymouspapi and @rafaepires,

This error is related to the dtype of the 'ds' column.
Can you try converting it with pd.to_datetime?

Alternatively, updating the StatsForecast pip install -U statsforecast may help

Closing due to lack of response and because it's a datasetsforecast/statsforecast issue.