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.
Added SARIMAX keew predictions (see sarimax_test.py test_plot_keew())
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)
Added trends analysis from EnvironmentalTrends
from time_decomp.environmentaltrends import EnvironmentalTrends
# Set pandas option to display all columns
pd.set_option('display.max_columns', None)
class TestEnvironmentalTrends(unittest.TestCase):
def setUp(self):
self.decomp = EnvironmentalTrends()
self.decomp.features = ['A']
self.decomp.trend_data_params = {'year_col':'Year', 'month_col':'Month' }
self.decomp.trends_params = {'seasons_per_year': 12, 'trend_lengths': [1], 'end_years': [2025]}
def test_m_trends(self):
self.decomp.m_trends()
# output df info
print("Starting test_plot_decomposition")
print("DataFrame Info:")
print(self.decomp.t['A'].info())
print("DataFrame Head:")
print("\n%s", self.decomp.t['A'].head())
DataFrame Head:
%s Frequency SeasonsPeryear TrendLength TrendEnd TrendPeriod \
0 Monthly 12 1 2025 Jul 2024 to Jun 2025
ValueCount Minimum Median Average Maximum YearsInPeriod \
0 12.0 5.0 48.0 50.333333 96.0 1.0
SeasonsInPeriod PercentOfYears PercentOfSeasons KW-pValue Seasonality \
0 12.0 100.0 100.0 0.443263 Non-seasonal
AppliedSeasonality MK-S MK-Variance MK-pvalue IncreasingLikelihood \
0 Non-seasonal 8.0 212.666667 0.631222 68.438909
TrendDirection SenSlope LowerSlope UpperSlope
0 Likely increasing 16.5 -384.0 544.8
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file time_decomp-2.0.0.tar.gz.
File metadata
- Download URL: time_decomp-2.0.0.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11677e0ab33e9f957569130a2c6cf578b18bdbd100265f58f5831815e430b04b
|
|
| MD5 |
2703859aa07fa63661309d5dd4d1324e
|
|
| BLAKE2b-256 |
5ba56084053b0dd042181d8fd91a3c269b757ac8e6ce45fb0cf1cef28b0e55f3
|
Provenance
The following attestation bundles were made for time_decomp-2.0.0.tar.gz:
Publisher:
python-publish.yml on AcaciaMan/time-decomp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
time_decomp-2.0.0.tar.gz -
Subject digest:
11677e0ab33e9f957569130a2c6cf578b18bdbd100265f58f5831815e430b04b - Sigstore transparency entry: 173462837
- Sigstore integration time:
-
Permalink:
AcaciaMan/time-decomp@517af20525f84fc73ceb7e4fb445ccc8a478df18 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/AcaciaMan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@517af20525f84fc73ceb7e4fb445ccc8a478df18 -
Trigger Event:
release
-
Statement type:
File details
Details for the file time_decomp-2.0.0-py2.py3-none-any.whl.
File metadata
- Download URL: time_decomp-2.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a95f44f0fbaf2dd852b3a27f7ec1870a704e6c529c36c06db6ce91f371ecb8e
|
|
| MD5 |
47411da32eac87984e2564cb2e4330c8
|
|
| BLAKE2b-256 |
2b520053d76530812a02b95e00c9cf22316122249a8f1412b72be44ad5f14423
|
Provenance
The following attestation bundles were made for time_decomp-2.0.0-py2.py3-none-any.whl:
Publisher:
python-publish.yml on AcaciaMan/time-decomp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
time_decomp-2.0.0-py2.py3-none-any.whl -
Subject digest:
2a95f44f0fbaf2dd852b3a27f7ec1870a704e6c529c36c06db6ce91f371ecb8e - Sigstore transparency entry: 173462840
- Sigstore integration time:
-
Permalink:
AcaciaMan/time-decomp@517af20525f84fc73ceb7e4fb445ccc8a478df18 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/AcaciaMan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@517af20525f84fc73ceb7e4fb445ccc8a478df18 -
Trigger Event:
release
-
Statement type: