Python Code

# import libraries
import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
import matplotlib.pyplot as plt
# read data
data_raw = pd.read_csv("../posts/2024-10-02-ts-fundamentals-whats-a-time-series/example_ts_data.csv")

data_raw = (
    # select columns
    data_raw[["Country", "Product", "Date", "Revenue"]]
    # change data types
    .assign(
        Date = pd.to_datetime(data_raw["Date"]), 
        Revenue = pd.to_numeric(data_raw["Revenue"])
    )
)

# print the first few rows
print(data_raw.head())
# filter on specific series
us_ic_raw = data_raw[(data_raw["Country"] == "United States") & (data_raw["Product"] == "Ice Cream")]

us_ic_raw.set_index("Date", inplace=True)

print(us_ic_raw.head())
# plot the series
plt.figure(figsize=(10, 6))
plt.plot(us_ic_raw.index, us_ic_raw["Revenue"], label = "Ice Cream Revenue", color = "blue")
plt.title("US Ice Cream Revenue")
plt.xlabel("Date")
plt.ylabel("Revenue")
plt.grid(True)

# save the plot
# plt.savefig("chart1", dpi = 300, bbox_inches = "tight")
# plot the autocorrelation
plt.figure(figsize=(10, 6))
plot_acf(us_ic_raw["Revenue"], lags=24, alpha=0.05)
plt.title("Autocorrelation of US Ice Cream Revenue")

# save the plot
# plt.savefig("chart2", dpi = 300, bbox_inches = "tight")
# plot the partial autocorrelation
plt.figure(figsize=(10, 6))
plot_pacf(us_ic_raw["Revenue"], lags=24, alpha=0.05)
plt.title("Partial Autocorrelation of US Ice Cream Revenue")

# save the plot
# plt.savefig("chart3", dpi = 300, bbox_inches = "tight")