# import libraries
import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
import matplotlib.pyplot as plt
Python Code
# read data
= pd.read_csv("../posts/2024-10-02-ts-fundamentals-whats-a-time-series/example_ts_data.csv")
data_raw
= (
data_raw # select columns
"Country", "Product", "Date", "Revenue"]]
data_raw[[# change data types
.assign(= pd.to_datetime(data_raw["Date"]),
Date = pd.to_numeric(data_raw["Revenue"])
Revenue
)
)
# print the first few rows
print(data_raw.head())
# filter on specific series
= data_raw[(data_raw["Country"] == "United States") & (data_raw["Product"] == "Ice Cream")]
us_ic_raw
"Date", inplace=True)
us_ic_raw.set_index(
print(us_ic_raw.head())
# plot the series
=(10, 6))
plt.figure(figsize"Revenue"], label = "Ice Cream Revenue", color = "blue")
plt.plot(us_ic_raw.index, us_ic_raw["US Ice Cream Revenue")
plt.title("Date")
plt.xlabel("Revenue")
plt.ylabel(True)
plt.grid(
# save the plot
# plt.savefig("chart1", dpi = 300, bbox_inches = "tight")
# plot the autocorrelation
=(10, 6))
plt.figure(figsize"Revenue"], lags=24, alpha=0.05)
plot_acf(us_ic_raw["Autocorrelation of US Ice Cream Revenue")
plt.title(
# save the plot
# plt.savefig("chart2", dpi = 300, bbox_inches = "tight")
# plot the partial autocorrelation
=(10, 6))
plt.figure(figsize"Revenue"], lags=24, alpha=0.05)
plot_pacf(us_ic_raw["Partial Autocorrelation of US Ice Cream Revenue")
plt.title(
# save the plot
# plt.savefig("chart3", dpi = 300, bbox_inches = "tight")