Lesson 12 · Data analytics zero to hero
Pandas Time Series Analysis Tutorial | Data Analytics #12
Video twelve of the 30-part series, and the last stop in the pandas block: dates as an index, resampling, rolling windows, and percent change. We're using a…
- CourseData analytics zero to hero
- Lesson12 of 30
- Video12 min
- FormatJupyter notebook · 10 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- aapl_ohlcv.csv24.3 KB
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 12: Time Series with Pandas#
- Video twelve of the 30-part series, and the last stop in the pandas block: dates as an index, resampling, rolling windows, and percent change.
- We're using a real dataset: about a year of actual AAPL daily stock price and volume history.
- Let's jump straight in.
Before You Start#
- Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
- Place aapl_ohlcv.csv in the same folder as this notebook.
Part 1: Dates as the Index#
import pandas as pd
df = pd.read_csv('aapl_ohlcv.csv', parse_dates=['Date'])
df = df.set_index('Date').sort_index()
print(df.head(3))
print(df.index)
print(df.loc['2025-08'].shape)
print(df.loc['2025-08':'2025-09'].head(3))
Part 2: Resampling#
weekly_close = df['Close'].resample('W').mean()
print(weekly_close.head(5))
monthly_summary = df['Close'].resample('ME').agg(['first', 'last', 'min', 'max'])
print(monthly_summary.head(5))
monthly_volume = df['Volume'].resample('ME').sum()
print(monthly_volume.head(3))
Part 3: Rolling Windows#
df['MA20'] = df['Close'].rolling(window=20).mean()
print(df[['Close', 'MA20']].tail(5))
df['MA5'] = df['Close'].rolling(window=5).mean()
df['Above20MA'] = df['MA5'] > df['MA20']
print(df[['MA5', 'MA20', 'Above20MA']].tail(5))
Part 4: pct_change, shift, and diff#
df['DailyReturn'] = df['Close'].pct_change(fill_method=None)
print(df['DailyReturn'].describe())
df['PriceYesterday'] = df['Close'].shift(1)
df['PriceChange'] = df['Close'].diff()
print(df[['Close', 'PriceYesterday', 'PriceChange']].tail(3))
cumulative_return = (1 + df['DailyReturn']).cumprod() - 1
print(cumulative_return.tail(3))
last_valid = cumulative_return.dropna().iloc[-1]
print(f"Total return over the period: {last_valid:.2%}")
Wrap-Up: What You Learned#
- Turning a date column into a real DatetimeIndex with parse_dates and set_index, and slicing it with partial date strings.
- resample, for bucketing a time series into weekly or monthly summaries.
- rolling, for moving averages and other smoothed statistics.
- pct_change, diff, and shift, for comparing each row to an earlier point in time.
- Computing a compounded cumulative return from daily percent changes.
- All of it on a real year of AAPL trading data. This wraps up the pandas block. Video thirteen starts a visualization block with Matplotlib, turning tables like these into real charts. Subscribe so it lands automatically see you there.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



