'time series decomposition error in python

I have the following dataframe

data = pd.DataFrame({
'date': [1988, 1988, 2000, 2005],
'value': [11558522, 12323552, 13770958, 18412280]
}) 

i then make the date column into a datetime dtype

data['date'] = pd.to_datetime(data['date'],format = '%Y')

when i print the dtypes i get

print(data.dtypes)

>>> Register No.
>>> date    datetime64[ns]
>>> Sum              int64
>>> dtype: object

I then use the following code to make a time series decomposition

from pylab import rcParams
from statsmodels.tsa.seasonal import seasonal_decompose 
rcParams ['figure.figsize'] = 18,8
decomposition = seasonal_decompose(data, model='additive', freq=30)
fig = decomposition.plot()

But i get the following error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-e3a60d7302da> in <module>
      3 
      4 rcParams ['figure.figsize'] = 18,8
----> 5 decomposition = seasonal_decompose(data, model='additive', freq=30)
      6 
      7 fig = decomposition.plot()

~/opt/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/seasonal.py in seasonal_decompose(x, model, filt, freq, two_sided, extrapolate_trend)
    113     nobs = len(x)
    114 
--> 115     if not np.all(np.isfinite(x)):
    116         raise ValueError("This function does not handle missing values")
    117     if model.startswith('m'):

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

i do not have any missing values in my dataframe and im not sure why am getting this error



Solution 1:[1]

You need to set date as index in data frame:

data = data.set_index('date')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 milos.ai