Time series analysis in Python – statistics, plots, and forecasting
Before diving into time series analysis, it’s crucial to have data to work with. In this section, we’ll walk through the process of creating mock time series data, saving it to an Excel file, and then reading it back into pandas. This will serve as our foundation for the upcoming time series analysis.
As always, we’ll start by loading the relevant libraries:
import pandas as pd import numpy as np import matplotlib.pyplot as plt
Then, we must create the sample data and save it to Excel so that it can be used in the rest of this chapter:
# Create a date range date_rng = pd.date_range(start='2022-01-01', end='2023-12-31', Â Â Â Â freq='D') # Create a trend component trend = 0.05 * np.arange(len(date_rng)) # Create a seasonal component (cyclicality) seasonal = 2.5 * np.sin(2 * np.pi * np.arange(len(date_rng)) / 365) # Add some random noise...