Selected topic
Time Series Exploration
Prefer practical output? Use related tools below while reading.
python
import pandas as pd
import matplotlib.pyplot as plt# Load data into a Pandas dataframe
df = pd.read_csv('apple_stock_prices.csv')
# Visualize time series (daily closing prices)
plt.figure(figsize=(10,6))
plt.plot(df['date'], df['price'])
plt.title('Apple Stock Prices over Time')
plt.xlabel('Date')
plt.ylabel('Price ($)')
plt.show()
# Check for seasonality (e.g., daily fluctuations)
df_monthly = df.resample('M').mean()
plt.figure(figsize=(10,6))
plt.plot(df_monthly['date'], df_monthly['price'])
plt.title('Monthly Averages of Apple Stock Prices')
plt.xlabel('Date')
plt.ylabel('Price ($)')
plt.show()
# Detect trend and stationarity
from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(df['price'], model='additive')
trend = decomposition.trend
seasonality = decomposition.seasonal
# Plot the trend and seasonality components
plt.figure(figsize=(10,6))
plt.subplot(411)
plt.plot(trend, label='Trend')
plt.legend(loc='best')
plt.subplot(412)
plt.plot(seasonality, label='Seasonality')
plt.legend(loc='best')
plt.show()
# Identify outliers and anomalies (e.g., using IQR method)
Q1 = df['price'].quantile(0.25)
Q3 = df['price'].quantile(0.75)
IQR = Q3 - Q1
outliers = df[(df['price'] < Q1 - 1.5 IQR) | (df['price'] > Q3 + 1.5 IQR)]
# Examine autocorrelation (e.g., using ACF plot)
from statsmodels.graphics.tsaplots import plot_acf
plot_acf(df['price'], lags=20)
plt.show()
By following these steps and using the example above, you'll be able to gain insights into the characteristics of your time series data, including trends, seasonality, outliers, and autocorrelation. This will help inform further analysis and modeling efforts.