Skip to main content

Time series decomposition plot trend and seasonality together

Project description

time-decomp

Time series decomposition plot trend and seasonality

Plot trend and seasonality together in one chart as described at Business Days Time Series Weekly Trend and Seasonality.

Example usage from ./Python/tests/keew_decomp_test.py

# create test
# test_decomposition.py
import unittest
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from time_decomp.decomposition import DecompositionSingleton

class TestKeewDecomposition(unittest.TestCase):
    def setUp(self):
        self.decomp = DecompositionSingleton()

        n = 2000

        df = pd.DataFrame({'A': np.random.randint(0,100, size=(n,)), 'B': np.random.randint(0,100, size=(n,))})

        lsDays = [pd.Timestamp(2021, 1, 1)]*n
        for i in range(n):
            # construct time, where iYear-iMonth-i
            lsDays[i] = pd.Timestamp( np.random.randint(2021,2025), np.random.randint(1,13) , (i+1) % 28 + 1)    

        df['Day'] = lsDays

        df['Year'] = df['Day'].dt.year
        df['Month'] = df['Day'].dt.month
        df['KeewMonth'] = df['Day'].apply(self.decomp.get_month_keew)
        df['Keew']=(df['Month']-1)*4+df['KeewMonth']

        self.decomp.df = df.groupby(['Year', 'Keew']).last().reset_index()
        
        self.decomp.features = ['A', 'B']
        self.decomp.decompose_params = {'model': 'additive', 'period':48, 'extrapolate_trend':'freq'}        

    def test_plot_decomposition(self):

        # output df info
        print("Starting test_plot_decomposition")
        print("DataFrame Info:")
        print(self.decomp.df.info())
        print("DataFrame Head:")
        print("\n%s", self.decomp.df.head())

    
        self.decomp.m_decompose()
        self.decomp.plot_decomposition('A', 'Year', range(2021,2025), 'Keew', 'A keew', chart_elements=[self.decomp.ChartElement.TREND, self.decomp.ChartElement.SEASONAL])
        plt.show()

Added PLOTLY to run from IPython Notebook

from plotly.offline import iplot

decomp.m_decompose()
fig = decomp.plot_decomposition_plotly('A', 'Year', range(2021,2025), 'Keew', 'A keew', chart_elements=[ decomp.ChartElement.OBSERVED, decomp.ChartElement.SEASONAL])
iplot(fig)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

time_decomp-1.2.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

time_decomp-1.2.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file time_decomp-1.2.1.tar.gz.

File metadata

  • Download URL: time_decomp-1.2.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for time_decomp-1.2.1.tar.gz
Algorithm Hash digest
SHA256 3491821c0dd6a63b4c682148e935b9f2d97e27e5d5bc4e84ce002978097f8d8f
MD5 cb81919dc1d3c58fa25434d596c3a074
BLAKE2b-256 d9141f00ccec33229ceabea4d2ccd8e13066af6a8d005f9f002e44a8750ca21e

See more details on using hashes here.

File details

Details for the file time_decomp-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: time_decomp-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for time_decomp-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5367a681f6967f817d8ea3bd0edc47cd1117f4344f3dd95c591e527a4ebf929
MD5 bb4fcb5e6d835435af8733412ab55e93
BLAKE2b-256 df42dd997b6254e90c2121bb4faee0292b086670168e251f8fcf6fcb11cb3402

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page