Skip to content
New Workbook
Sign up
Time Series Decomposition

Time Series Decomposition

Decompose your time series data and plot it to uncover seasonality, trends and noise.

  • Seasonality tells you whether the data displays a clear periodic pattern.
  • Trend answers whether the data follow a consistent upwards or downward slope.
  • Noise highlights outlier points or missing values inconsistent with the rest of the data.
# Load packages
import statsmodels.api as sm
import pandas as pd
import matplotlib.pyplot as plt
# Upload your data as CSV and load as the first data frame
df = pd.read_csv(
    "co2.csv",
    parse_dates=["datestamp"],  # Tell pandas which column(s) to parse as dates
    index_col="datestamp",
)  # Use a date column as your index
# Convert numeric columns
df["co2"] = pd.to_numeric(
    df["co2"],  # Tell pandas which column(s) to make numeric
    errors="coerce",  # If ‘coerce’, then invalid parsing will be set as NaN
)

# Fill missing values
df["co2"] = df["co2"].fillna(method="ffill")  # Choose method to fill missing values
df.head()
# Plot settings
%config InlineBackend.figure_format='retina'
plt.style.use("ggplot")
plt.rcParams["figure.figsize"] = 12, 9  # Figure size (width,height)

# Perform time series decompositon
decomposition = sm.tsa.seasonal_decompose(df)

# Plot decomposition
fig = decomposition.plot()

# Specify axis labels
plt.xlabel("Date", fontsize=12)  # Text and size of xlabel
plt.suptitle(
    "CO2 Time-Series Decomposition",  # Text of title
    y=1.05,  # Position of title
    size=15,  # Size of title
)
plt.show()
r
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Create a table using pandas DataFrame
data = {'Month': ['Jan-23', 'Feb-23', 'Mar-23', 'Apr-23', 'May-23', 'Jun-23', 'Jul-23', 'Aug-23', 'Sep-23', 'Oct-23', 'Nov-23', 'Dec-23'],
        'Historical': [-.0003, .0573, .0275, .0588, .1465, .0974, .0808, 0, 0, 0, 0, 0],
        'Stable Rand': [0,0,0,0,0,0,.0808,.0839, .0870, .0902, .0933, .0965],
       'Rand Strenghtens': [0,0,0,0,0,0, .0808,.0634, .0589, .0544, .0488, .0417],
       'Rand Weakems': [0,0,0,0,0,0, .0808,.1044, .1155, .1269, .1395, .1540]}
df = pd.DataFrame(data)

df
import numpy as np
# Replace 0 with None in df
df = df.replace(0, np.nan)
df
# Multiply all numerical columns by 100
df[['Historical', 'Stable Rand', 'Rand Strenghtens', 'Rand Weakems']] *= 100

# Display the updated dataframe
df
# Melt numerical columns into 1 column with numbers and 1 column with labels
melted_df = df.melt(id_vars='Month', var_name='Scenario', value_name='Returns in %')

melted_df
# Create a new column called "Scenario Flag" where the value is 1 if the scenario is "Historical" and 0 otherwise
melted_df['Actual'] = melted_df['Scenario'].apply(lambda x: 0 if x == 'Historical' else 1)

melted_df
palette = sns.color_palette("mako_r", 6)
plt.figure(figsize=(12, 5))  # Set the figure size

ax = sns.lineplot(
    data=melted_df, x="Month", y="Returns in %",
    hue="Scenario", style="Actual",
    palette=palette
)

plt.axvline(6, 0,16, c = 'grey', linestyle=':')

# Gets the axes object out after plotting

# Turns off grid on the left Axis.
ax.grid(False)
# Line chart using seaborn
sns.set(style="darkgrid")  # Set the style of the plot

# Plot the line chart
plt.figure(figsize=(12, 3))  # Set the figure size
sns.lineplot(data=df, x="Month", y="Historical", label="Historical")  # Plot Historical data
sns.lineplot(data=df, x="Month", y="Stable Rand", label="Rand remains at current exchange rate")  # Plot Stable Rand data
sns.lineplot(data=df, x="Month", y="Rand Strenghtens", label="Rand Strengthens by 5%")  # Plot Rand Strenghtens data
sns.lineplot(data=df, x="Month", y="Rand Weakems", label="Rand Weakens  by 5%")  # Plot Rand Weakems data

# Set the title and labels
plt.title("Website graph for returns variance", fontsize=15)  # Set the title and font size
plt.xlabel("Month", fontsize=12)  # Set the x-axis label and font size
plt.ylabel("Returns in %", fontsize=12)  # Set the y-axis label and font size

# Show the legend
plt.legend(fontsize=12)  # Show the legend with font size

# Show the plot
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from datetime import datetime


Spread_df = pd.read_csv('BTC_Forex_2022-07-25-2023-07-26.csv')
Spread_df['Date_time'] =Spread_df['Date'] + ' ' + Spread_df['Time']
Spread_df.dtypes
# Convert the "Date_time" column to datetime format if it is not already
Spread_df["Date_time"] = pd.to_datetime(Spread_df["Date_time"])

# Create a new column "First_Day_of_Month" with the first day of the month
Spread_df["First_Day_of_Month"] = Spread_df["Date_time"].dt.to_period("M").dt.start_time

# Display the updated dataframe
Spread_df