Sattawat
Sattawat

Reputation: 149

TypeError: seasonal_decompose() got an unexpected keyword argument 'period'

I'm trying to digest time series data with datetime index. I'm going to use period in my project, but it gives me an error saying there is no argument named period. I couldn't find anything for that. But I see that the interval is mentioned on the statsmodel website, does anyone know how I can overcome this situation?

 from statsmodels.tsa.seasonal import seasonal_decompose
# Multiplicative Decomposition 
decomp_mul = seasonal_decompose(df['meantemp'], model='multiplicative', extrapolate_trend='freq', period=365)
decomp_mul.plot()
plt.show()

TypeError Traceback (most recent call last) in () 2 3 # Multiplicative Decomposition ----> 4 decomp_mul = seasonal_decompose(df['meantemp'], model='multiplicative', extrapolate_trend='freq', period=365) 5 decomp_mul.plot() 6 plt.show()

TypeError: seasonal_decompose() got an unexpected keyword argument 'period'

i use google colab

Upvotes: 3

Views: 14700

Answers (2)

Francisco
Francisco

Reputation: 1

Updating your Q: I checked this today: Note: The frequency parameter of statsmodels’ seasonal_decompose() method has been deprecated and replaced with the period parameter in TDS webpage. And... The date should be in datetime format and need to be set as index using .set_index(), e.g., df.set_index('Date', inplace=True). In your case Date is t (as per your dataset)

Upvotes: 0

user19218643
user19218643

Reputation: 1

#Prueba con este código que me funcionó en colab #freq=12 es el número de periodos en un año para este caso

La info la obtuve en el libro Mastering Python for Finance de James Ma Weiming (segunda edición, pag 202) Te dejo lo que dice junto con el código

Seasonal decomposing

Decomposing involves modeling both the trend and seasonality, and then removing them. We can use the statsmodel.tsa.seasonal module to model a nonstationary time series dataset using moving averages and remove its trend and seasonal components.

By reusing our df_log variable containing the logarithm of our dataset from the previous section, we get the following:

from statsmodels.tsa.seasonal import seasonal_decompose 
decompose_result = seasonal_decompose(df_log.dropna(), freq=12) 
df_trend = decompose_result.trend 
df_season = decompose_result.seasonal 
df_residual = decompose_result.resid 

The seasonal_decompose() method of statsmodels.tsa.seasonal requires a parameter, freq, which is an integer value specifying the number of periods per seasonal cycle.

Since we are using monthly data, we expect 12 periods in a seasonal year. The method returns an object with three attributes, mainly the trend and seasonal components, as well as the final pandas series data with its trend and seasonal components removed.

More information on the seasonal_decompose() method of the statsmodels.t sa.seasonal module can be found here

Let's visualize the different plots by running the following Python code:

plt.rcParams['figure.figsize'] = (12, 8) 
fig = decompose_result.plot()

Upvotes: 0

Related Questions