Search code examples
pythonstatsforecast

TypeError: expected dtype object, got 'numpy.dtype[float32] when running StatsForecast


I just followed exactly same as 'Forecast with ARIMA and ETS' (https://nixtla.github.io/statsforecast/examples/getting_started_with_auto_arima_and_ets.html). But somehow my Jupyter Notebook (Anaconda) showed the following error.

"TypeError: expected dtype object, got 'numpy.dtype[float32]'"

Why do I get the error? Can you give me solutions for this? Thanks in advance.

import numpy as np
import pandas as pd
from IPython.display import display, Markdown
import matplotlib.pyplot as plt
from statsforecast import StatsForecast
from statsforecast.models import AutoARIMA, ETS, Naive #Imports the models you will use
from statsforecast.utils import AirPassengersDF

Y_df = AirPassengersDF
Y_df.head()
   unique_id    ds          y
0  1.0          1949-01-31  112.0
1  1.0          1949-02-28  118.0
2  1.0          1949-03-31  132.0
3  1.0          1949-04-30  129.0
4  1.0          1949-05-31  121.0
Y_train_df = Y_df[Y_df.ds<='1959-12-31'] # 132 monthly observations for train
Y_test_df = Y_df[Y_df.ds>'1959-12-31'] # 12 monthly observations for test
season_length = 12 # Monthly data 
horizon = len(Y_test_df) # Predict the lenght of the test df

# Include the models you imported
models = [
    AutoARIMA(season_length=season_length),
    ETS(season_length=season_length),
    Naive()
]

# Instansiate the StatsForecast class as sf
sf = StatsForecast(
    df=Y_train_df,
    models=models,
    freq='M', 
    n_jobs=-1
)

# Forecast for the defined horizon
Y_hat_df = sf.forecast(horizon)

Y_hat_df.head()

And then, I just hit the run. But I got the following error.

TypeError                                 Traceback (most recent call last)
<ipython-input-10-a9ee1bd8ce20> in <module>
     18 
     19 # Forecast for the defined horizon
---> 20 Y_hat_df = sf.forecast(horizon)
     21 
     22 Y_hat_df.head()

~\spyder\lib\site-packages\statsforecast\core.py in forecast(self, h, df, X_df, level, fitted, sort_df)
    668         X, level = self._parse_X_level(h=h, X=X_df, level=level)
    669         if self.n_jobs == 1:
--> 670             res_fcsts = self.ga.forecast(
    671                 models=self.models,
    672                 h=h,

~\spyder\lib\site-packages\statsforecast\core.py in forecast(self, models, h, fallback_model, fitted, X, level, verbose)
    197                         )
    198                     else:
--> 199                         raise error
    200                 cols_m = [
    201                     key

~\spyder\lib\site-packages\statsforecast\core.py in forecast(self, models, h, fallback_model, fitted, X, level, verbose)
    183                     kwargs["level"] = level
    184                 try:
--> 185                     res_i = model.forecast(
    186                         h=h, y=y_train, X=X_train, X_future=X_f, fitted=fitted, **kwargs
    187                     )

~\spyder\lib\site-packages\statsforecast\models.py in forecast(self, y, h, X, X_future, level, fitted)
    306         """
    307         with np.errstate(invalid="ignore"):
--> 308             mod = auto_arima_f(
    309                 x=y,
    310                 d=self.d,

~\spyder\lib\site-packages\statsforecast\arima.py in auto_arima_f(x, d, D, max_p, max_q, max_P, max_Q, max_order, max_d, max_D, start_p, start_q, start_P, start_Q, stationary, seasonal, ic, stepwise, nmodels, trace, approximation, method, truncate, xreg, test, test_kwargs, seasonal_test, seasonal_test_kwargs, allowdrift, allowmean, blambda, biasadj, parallel, num_cores, period)
   1785         D = 0
   1786     elif D is None:
-> 1787         D = nsdiffs(
   1788             xx, period=m, test=seasonal_test, max_D=max_D, **seasonal_test_kwargs
   1789         )

~\spyder\lib\site-packages\statsforecast\arima.py in nsdiffs(x, test, alpha, period, max_D, **kwargs)
   1608     while dodiff and D < max_D:
   1609         D += 1
-> 1610         x = diff(x, period, 1)
   1611         if is_constant(x):
   1612             return D

~\spyder\lib\site-packages\statsforecast\arima.py in diff(x, lag, differences)
    583 def diff(x, lag, differences):
    584     if x.ndim == 1:
--> 585         y = diff1d(x, lag, differences)
    586         nan_mask = np.isnan(y)
    587     elif x.ndim == 2:

TypeError: expected dtype object, got 'numpy.dtype[float32]'

I am supposed to get below.

unique_id  ds           AutoARIMA   ETS         Naive   
1.0        1960-01-31   424.160156  406.651276  405.0
1.0        1960-02-29   407.081696  401.732910  405.0
1.0        1960-03-31   470.860535  456.289642  405.0
1.0        1960-04-30   460.913605  440.870514  405.0
1.0        1960-05-31   484.900879  440.333923  405.0

Solution

  • Well, somehow I found a solution for this error. I simply updated 'numba' version from 0.50.1 to 0.55.0. Then, all went thru successfully. It looks like the 'StatsForecast' needs an updated version of 0.55.0 of 'numba' and 0.38.0 of llvmlite. Thanks.

    conda install -c numba numba=0.55.0
    
    conda install -c numba llvmlite=0.38.0