Click here to Skip to main content
15,895,667 members

Loop for trend, seasonality and resid

Miranda Powell asked:

Open original thread
I'm trying to put together a few stats about the history of my data to better understand it. My goal is to create a loop which creates a pdf for trend. seasonality and resid for each column in my data frame. I wrote a loop to do this, but each graph includes more data than it should. For example, the first graph created has only 1 line, but then the next graph created has the line from the first graph and a new line.

I created some fake data for testing purposes.

What I have tried:

#Creating random Data
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import statsmodels.api as sm

#creating random data
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(365), freq='D')

np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
data2 = np.random.randint(1, high=200, size=len(days))
df = pd.DataFrame({'test': days, 'col1': data, 'col2': data2})
df = df.set_index('test')
print(df)


#Loop to create a resid, trend, and seasonality plot for each column in the dataframe
for i in df:
    decomposition = sm.tsa.seasonal_decompose(df[i], model = 'additive')
    decomposition.resid.plot()
    plt.savefig('{} resid.pdf'.format(i), bbox_inches='tight')

    decomposition.seasonal.plot()
    plt.savefig('{} seasonal.pdf'.format(i), bbox_inches='tight')

    decomposition.trend.plot()
    plt.savefig('{} trend.pdf'.format(i), bbox_inches='tight')
Tags: Python, Graph

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900