Python – Use Python to decompose time series only in trends and residuals

Use Python to decompose time series only in trends and residuals… here is a solution to the problem.

Use Python to decompose time series only in trends and residuals

I just want to break down the time series for trends and residuals (no seasonality). So far, I know I can use statsmodels to break down time series, but that includes seasonality. Is there a way to break it down without seasonality?

I looked at the documentation ( seasonal_decompose allows different types of seasonality (“addition”, “multiplication”}), but I don’t see a keyword parameter that excludes seasonality.

Below is a toy model of my question. Time series with trends but no seasonality. If we were to remove the seasonality factor, I think we would be more appropriate. Time series decomposition

import numpy as np
import pandas as pd
import statsmodels.api as sm
from statsmodels.tsa.arima_model import ARMA
from matplotlib import pylab as plt

#defining the trend function
def trend(t, amp=1):
    return amp*(1 + t)

n_time_steps = 100
#initializing the time series
time_series = np.zeros(n_time_steps)
time_series[0] = trend(0, amplitud)

alpha = 0.1
#making the time series
for t in range(1,n_time_steps):
    time_series[t] = (1 - alpha)*time_series[t - 1] + alpha*trend(t, amp=amplitud) + alpha*np.random.normal(0,25)

#passing the time series to a pandas format
dates = sm.tsa.datetools.dates_from_range('2000m1', length=len(time_series))
time_series_pd= pd. Series(time_series, index=dates)
#decomposing the time series
res = sm.tsa.seasonal_decompose(time_series_pd)


I don’t think the function seasonal_decompose can be used without the Seasonal component.

Have you ever thought about using another function, such as > statsmodels.tsa.tsatools.detrend ? This can do what you want with polynomial fitting.

Related Problems and Solutions