Time Series#
Financial datasets are inherently time-based—prices, returns, volumes, FX rates,
and macroeconomic indicators are all indexed by dates. Pandas provides first-class
support for time series through the DatetimeIndex, which enables rich operations
such as shifting, lagging, resampling, rolling statistics, and automatic alignment a
cross datasets.
First, we’ll import the pandas library and then load ten years of closing stock prices for seven stocks: Amazon, American Express, Goldman Sachs, Home Depot, Microsoft, and Walmart.
1# imports and data load
2import pandas as pd
3
4from finance import AdjustedCloseMatrix
5matrix = AdjustedCloseMatrix(symbols=["AAPL", "AMZN", "AXP", "GS", "HD", "MSFT", "WMT"], lookback_days=10*252) # ten years of trading days
6matrix.load_from_csv("../StockData/stocks")
7prices = matrix.to_dataframe()
8prices
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2015-11-12 | 26.090711 | 33.2800 | 62.355658 | 157.355860 | 97.763661 | 46.461019 | 15.722738 |
| 2015-11-13 | 25.328643 | 32.1175 | 61.679950 | 155.413094 | 94.755184 | 46.042765 | 15.576416 |
| 2015-11-16 | 25.742369 | 32.3905 | 62.078444 | 156.857924 | 95.418470 | 46.848775 | 15.976732 |
| 2015-11-17 | 25.633019 | 32.1650 | 61.601984 | 155.560026 | 99.635076 | 46.469733 | 16.542695 |
| 2015-11-18 | 26.444690 | 33.1770 | 62.502927 | 158.082356 | 99.919341 | 47.241743 | 16.821535 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2025-11-13 | 272.950000 | 237.5800 | 364.730000 | 805.500000 | 368.070000 | 503.290000 | 102.540000 |
| 2025-11-14 | 272.410000 | 234.6900 | 357.180000 | 790.910000 | 362.360000 | 510.180000 | 102.480000 |
| 2025-11-17 | 267.460000 | 232.8700 | 341.250000 | 775.560000 | 358.030000 | 507.490000 | 102.950000 |
| 2025-11-18 | 267.440000 | 222.5500 | 340.660000 | 776.300000 | 336.480000 | 493.790000 | 101.390000 |
| 2025-11-19 | 268.560000 | 222.6900 | 344.640000 | 785.990000 | 334.500000 | 487.120000 | 100.610000 |
2520 rows × 7 columns
DatetimeIndex Basics#
Once a DataFrame is indexed by dates:
(If necessary, convert an existing column to the index and sort)
prices = prices.set_index("Date").sort_index()
Confirm that the index type is DatetimeIndex:
1print(type(prices.index))
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
With the index as a DatetimeIndex, Pandas allows for -
date-based slicing
arithmetic with time offsets
resampling (daily → monthly)
shifting/lagging
alignment with other time-indexed data
Example: selecting a specific date or range:
1prices.loc["2025-01-15"] # data for a single day
AAPL 236.816526
AMZN 223.350000
AXP 309.649197
GS 596.811822
HD 398.693511
MSFT 423.985596
WMT 90.657199
Name: 2025-01-15 00:00:00, dtype: float64
1prices.loc["2025-01"] # data for a single month
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2025-01-02 | 242.770042 | 220.22 | 295.196186 | 566.327062 | 381.384622 | 416.297742 | 89.327216 |
| 2025-01-03 | 242.282212 | 224.19 | 300.488213 | 571.409497 | 382.091508 | 421.041735 | 90.101385 |
| 2025-01-06 | 243.914949 | 227.61 | 299.298474 | 574.620492 | 382.278047 | 425.517199 | 90.746526 |
| 2025-01-07 | 241.137305 | 222.11 | 299.437277 | 571.399647 | 377.231667 | 420.067078 | 90.131161 |
| 2025-01-08 | 241.625135 | 222.13 | 300.260179 | 571.301150 | 380.147572 | 422.245137 | 91.113761 |
| 2025-01-10 | 235.801043 | 218.94 | 290.791846 | 551.582090 | 382.788576 | 416.665725 | 92.304790 |
| 2025-01-13 | 233.361894 | 218.46 | 294.480035 | 554.487746 | 382.091508 | 414.915321 | 90.845779 |
| 2025-01-14 | 232.246854 | 217.76 | 297.791473 | 562.938772 | 385.625938 | 413.403609 | 90.111311 |
| 2025-01-15 | 236.816526 | 223.35 | 309.649197 | 596.811822 | 398.693511 | 423.985596 | 90.657199 |
| 2025-01-16 | 227.249086 | 220.66 | 308.816380 | 603.775546 | 401.874498 | 422.265028 | 90.617498 |
| 2025-01-17 | 228.961469 | 225.94 | 309.887144 | 616.530882 | 401.923587 | 426.690765 | 91.252714 |
| 2025-01-21 | 221.653976 | 230.71 | 315.231052 | 625.198600 | 410.808751 | 426.163655 | 92.384192 |
| 2025-01-22 | 222.838706 | 235.01 | 319.494280 | 623.218814 | 402.630473 | 443.767148 | 92.533071 |
| 2025-01-23 | 222.669459 | 235.42 | 323.083324 | 629.887048 | 406.842335 | 444.274367 | 93.108735 |
| 2025-01-24 | 221.793356 | 234.85 | 318.592062 | 627.326131 | 406.950332 | 441.638816 | 94.051633 |
| 2025-01-27 | 228.842000 | 235.42 | 313.714136 | 623.760547 | 417.131453 | 432.190613 | 96.671898 |
| 2025-01-28 | 237.204799 | 238.15 | 314.328834 | 628.212602 | 411.201465 | 444.761695 | 96.562721 |
| 2025-01-29 | 238.299927 | 237.07 | 312.554141 | 627.798916 | 405.890003 | 439.918248 | 96.771151 |
| 2025-01-30 | 236.537766 | 234.64 | 316.222500 | 635.993850 | 406.950332 | 412.727317 | 97.912554 |
| 2025-01-31 | 234.954808 | 237.68 | 314.735328 | 630.773519 | 404.476231 | 412.796935 | 97.426217 |
1prices.loc["2025-01-25":"2025-02-04"] # a date range
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2025-01-27 | 228.842000 | 235.42 | 313.714136 | 623.760547 | 417.131453 | 432.190613 | 96.671898 |
| 2025-01-28 | 237.204799 | 238.15 | 314.328834 | 628.212602 | 411.201465 | 444.761695 | 96.562721 |
| 2025-01-29 | 238.299927 | 237.07 | 312.554141 | 627.798916 | 405.890003 | 439.918248 | 96.771151 |
| 2025-01-30 | 236.537766 | 234.64 | 316.222500 | 635.993850 | 406.950332 | 412.727317 | 97.912554 |
| 2025-01-31 | 234.954808 | 237.68 | 314.735328 | 630.773519 | 404.476231 | 412.796935 | 97.426217 |
| 2025-02-03 | 227.000194 | 237.42 | 313.823195 | 622.864226 | 401.314880 | 408.679508 | 98.795901 |
| 2025-02-04 | 231.768980 | 242.06 | 313.129181 | 624.647018 | 403.995156 | 410.121602 | 100.016706 |
This syntax is extremely convenient for financial analysis because it mirrors natural language ideas of “all data for this month” or “from this date to that date.”
Resampling (Daily → Weekly → Monthly → Annual)#
Resampling aggregates data over new time frequencies. This is useful when computing monthly returns, quarterly averages, annual volatility, etc.
Example: convert daily closing prices to end-of-month prices:
1monthly_prices = prices.resample("ME").last()
2monthly_prices.head()
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2015-11-30 | 26.672409 | 33.2400 | 62.061119 | 155.641655 | 105.715200 | 47.680385 | 16.244529 |
| 2015-12-31 | 23.732356 | 33.7945 | 60.250570 | 147.622858 | 104.886433 | 48.671715 | 17.065803 |
| 2016-01-31 | 21.946680 | 29.3500 | 46.555231 | 132.330627 | 99.739266 | 48.329575 | 18.474498 |
| 2016-02-29 | 21.917479 | 27.6260 | 48.365228 | 123.009492 | 98.438595 | 44.950733 | 18.468930 |
| 2016-03-31 | 24.705617 | 29.6820 | 53.429741 | 129.138167 | 106.398493 | 48.793809 | 19.208664 |
End-of-week:
1weekly_prices = prices.resample("W-SAT").last()
2weekly_prices.head()
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2015-11-14 | 25.328643 | 32.1175 | 61.679950 | 155.413094 | 94.755184 | 46.042765 | 15.576416 |
| 2015-11-21 | 26.897873 | 33.4225 | 62.736826 | 156.294685 | 102.817271 | 47.540019 | 16.584107 |
| 2015-11-28 | 26.561932 | 33.6630 | 62.243040 | 155.478397 | 106.394279 | 47.311925 | 16.534413 |
| 2015-12-05 | 26.836998 | 33.6320 | 61.593321 | 155.617082 | 106.694684 | 49.048948 | 16.609230 |
| 2015-12-12 | 25.518033 | 32.0075 | 59.652828 | 144.616833 | 103.450937 | 47.425972 | 16.525711 |
Common financial resampling rules:
"B"— business daily"W"— weekly (ends on Sunday by default)"ME"— month end"QE"— quarter end"YE"— year end"30T"— 30-minute intervals"1H"— hourly
The full list of rules for date offsets.
Resampling can use any aggregation function:
(any built-in method available via GroupBy)
Including: sum, mean, std, max, min, median, first, last
1monthly_mean = prices.resample("ME").mean()
2monthly_mean.head()
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2015-11-30 | 26.342949 | 33.149417 | 62.250981 | 155.924635 | 101.586111 | 47.095926 | 16.380958 |
| 2015-12-31 | 25.190392 | 33.463114 | 60.593148 | 149.654922 | 105.098045 | 48.597944 | 16.693999 |
| 2016-01-31 | 22.192199 | 30.053079 | 53.004211 | 131.658550 | 97.931016 | 45.972682 | 17.600621 |
| 2016-02-29 | 21.686177 | 26.531000 | 46.959870 | 122.496748 | 95.776581 | 45.235083 | 18.443317 |
| 2016-03-31 | 23.635079 | 28.618705 | 51.866562 | 126.736429 | 102.844910 | 47.163012 | 18.908814 |
1monthly_volatility = prices.resample("ME").std()
2monthly_volatility.head()
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2015-11-30 | 0.517832 | 0.615681 | 0.412930 | 1.324640 | 4.226872 | 0.528006 | 0.409419 |
| 2015-12-31 | 1.031216 | 0.563803 | 0.854390 | 3.656376 | 0.996106 | 0.588716 | 0.275202 |
| 2016-01-31 | 0.654378 | 1.140541 | 4.135118 | 6.081216 | 3.178501 | 1.268536 | 0.367964 |
| 2016-02-29 | 0.284430 | 1.358542 | 1.097620 | 3.579853 | 3.859374 | 1.246868 | 0.254715 |
| 2016-03-31 | 0.632360 | 0.654660 | 1.091353 | 1.760522 | 2.279452 | 0.998205 | 0.238747 |
Shifting and Lagging#
Shifting is an important tools in time-series finance. It moves data forward or backward in time without changing the index, allowing you to align today’s prices with yesterday’s values (or vice versa). This is essential for computing returns, building lagged signals, and preparing data for prediction.
Shift Prices Forward#
Shift daily prices down by 1 day:
1last5 = prices.tail(5)
2print(last5)
3prev_prices = last5.shift(1)
4print(prev_prices)
AAPL AMZN AXP GS HD MSFT WMT
2025-11-13 272.95 237.58 364.73 805.50 368.07 503.29 102.54
2025-11-14 272.41 234.69 357.18 790.91 362.36 510.18 102.48
2025-11-17 267.46 232.87 341.25 775.56 358.03 507.49 102.95
2025-11-18 267.44 222.55 340.66 776.30 336.48 493.79 101.39
2025-11-19 268.56 222.69 344.64 785.99 334.50 487.12 100.61
AAPL AMZN AXP GS HD MSFT WMT
2025-11-13 NaN NaN NaN NaN NaN NaN NaN
2025-11-14 272.95 237.58 364.73 805.50 368.07 503.29 102.54
2025-11-17 272.41 234.69 357.18 790.91 362.36 510.18 102.48
2025-11-18 267.46 232.87 341.25 775.56 358.03 507.49 102.95
2025-11-19 267.44 222.55 340.66 776.30 336.48 493.79 101.39
Every value moves down by 1 row (to the next day).
The top row becomes NaN because there is no previous day.
Useful for calculations involving lagged values. (e.g., returns)
Compute simple returns manually:
1returns = (prices - prices.shift(1)) / prices.shift(1)
2returns.tail()
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2025-11-13 | -0.001901 | -0.027109 | -0.021568 | -0.039894 | -0.008245 | -0.015358 | -0.008701 |
| 2025-11-14 | -0.001978 | -0.012164 | -0.020700 | -0.018113 | -0.015513 | 0.013690 | -0.000585 |
| 2025-11-17 | -0.018171 | -0.007755 | -0.044599 | -0.019408 | -0.011949 | -0.005273 | 0.004586 |
| 2025-11-18 | -0.000075 | -0.044317 | -0.001729 | 0.000954 | -0.060190 | -0.026996 | -0.015153 |
| 2025-11-19 | 0.004188 | 0.000629 | 0.011683 | 0.012482 | -0.005884 | -0.013508 | -0.007693 |
This works column by column:
numerator: today’s price minus yesterday’s price
denominator: yesterday’s price (again via shift)
Identical to:
1returns = prices.pct_change()
2returns.tail()
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2025-11-13 | -0.001901 | -0.027109 | -0.021568 | -0.039894 | -0.008245 | -0.015358 | -0.008701 |
| 2025-11-14 | -0.001978 | -0.012164 | -0.020700 | -0.018113 | -0.015513 | 0.013690 | -0.000585 |
| 2025-11-17 | -0.018171 | -0.007755 | -0.044599 | -0.019408 | -0.011949 | -0.005273 | 0.004586 |
| 2025-11-18 | -0.000075 | -0.044317 | -0.001729 | 0.000954 | -0.060190 | -0.026996 | -0.015153 |
| 2025-11-19 | 0.004188 | 0.000629 | 0.011683 | 0.012482 | -0.005884 | -0.013508 | -0.007693 |
Comparing Current vs. Historical Prices#
You might want to see how the current price compares to the price 5 days ago, 10 days ago, or even 252 days ago (approx. one trading year).
1apple_df = prices[["AAPL"]].copy()
2apple_df["returns"] = apple_df["AAPL"].pct_change()
3apple_df["5_Day_Lag_Price"] = apple_df["AAPL"].shift(5)
4apple_df.tail(7)
| AAPL | returns | 5_Day_Lag_Price | |
|---|---|---|---|
| 2025-11-11 | 275.25 | 0.021601 | 269.779663 |
| 2025-11-12 | 273.47 | -0.006467 | 269.879566 |
| 2025-11-13 | 272.95 | -0.001901 | 269.509923 |
| 2025-11-14 | 272.41 | -0.001978 | 268.211176 |
| 2025-11-17 | 267.46 | -0.018171 | 269.430000 |
| 2025-11-18 | 267.44 | -0.000075 | 275.250000 |
| 2025-11-19 | 268.56 | 0.004188 | 273.470000 |
Lagging#
In financial modeling, a stock’s price from previous days (lags) is often used as a feature to predict its future price or volatility. For example, you might use the 1-day, 3-day, and 5-day lagged prices as inputs to a machine learning model.
Rolling Windows#
Rolling calculations capture time-varying behavior, such as:
20-day moving average
60-day rolling volatility
rolling correlations
rolling sums / cumulative flows
20-day moving average:
1roll_mean = prices.rolling(window=20).mean()
2roll_mean.tail() # Switch to head()? Why no data?
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2025-11-13 | 267.094899 | 234.4325 | 360.7030 | 784.6170 | 379.9945 | 516.6500 | 103.8060 |
| 2025-11-14 | 268.113060 | 235.5150 | 361.2310 | 786.6240 | 378.5175 | 516.4800 | 103.5435 |
| 2025-11-17 | 268.386701 | 236.3345 | 360.8195 | 787.2360 | 376.9745 | 516.0150 | 103.3385 |
| 2025-11-18 | 268.632868 | 236.3605 | 360.0915 | 788.1020 | 374.2535 | 514.8215 | 103.0970 |
| 2025-11-19 | 269.150826 | 236.5975 | 359.7235 | 790.1715 | 371.5300 | 513.1505 | 102.7705 |
20-day rolling volatility:
1roll_volatility = prices.rolling(window=20).std()
2roll_volatility.tail()
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2025-11-13 | 5.808184 | 13.297420 | 6.883075 | 22.331834 | 7.777495 | 12.392228 | 2.204463 |
| 2025-11-14 | 4.713007 | 12.308742 | 6.107212 | 20.886821 | 8.191469 | 12.459697 | 2.017240 |
| 2025-11-17 | 4.492326 | 11.493318 | 7.131954 | 20.340227 | 9.001272 | 12.620026 | 1.842939 |
| 2025-11-18 | 4.283787 | 11.459794 | 8.369374 | 19.421864 | 12.220008 | 13.550669 | 1.760072 |
| 2025-11-19 | 3.512970 | 11.102450 | 8.889526 | 16.532818 | 14.604721 | 14.810414 | 1.565525 |
Rolling operations automatically align their output with the same index, with
NaN for the first window-1 rows.
Expanding Windows#
Expanding windows compute statistics using all data up to each date. Useful for long-run averages or incremental calculations.
1expanding_mean = prices.expanding().mean()
2expanding_mean.head()
| AAPL | AMZN | AXP | GS | HD | MSFT | WMT | |
|---|---|---|---|---|---|---|---|
| 2015-11-12 | 26.090711 | 33.28000 | 62.355658 | 157.355860 | 97.763661 | 46.461019 | 15.722738 |
| 2015-11-13 | 25.709677 | 32.69875 | 62.017804 | 156.384477 | 96.259422 | 46.251892 | 15.649577 |
| 2015-11-16 | 25.720574 | 32.59600 | 62.038018 | 156.542293 | 95.979105 | 46.450853 | 15.758629 |
| 2015-11-17 | 25.698686 | 32.48825 | 61.929009 | 156.296726 | 96.893098 | 46.455573 | 15.954645 |
| 2015-11-18 | 25.847887 | 32.62600 | 62.043793 | 156.653852 | 97.498346 | 46.612807 | 16.128023 |
The Maximum Drawdown (MDD) is the largest peak-to-trough decline during a specific period. To calculate the MDD at any point in time, you must track:
The Historical Peak (the highest price seen up to that date).
The Drawdown (the percentage drop from that historical peak).
The expanding() function is perfect for finding the Historical Peak.
1apple_df = prices[["AAPL"]].copy()
2apple_df['Historical_Peak'] = apple_df['AAPL'].expanding(min_periods=1).max()
3apple_df['Drawdown'] = apple_df['AAPL'] / apple_df['Historical_Peak'] - 1
4apple_df['Max_Drawdown'] = apple_df['Drawdown'].expanding(min_periods=1).min()
5apple_df.head(10)
| AAPL | Historical_Peak | Drawdown | Max_Drawdown | |
|---|---|---|---|---|
| 2015-11-12 | 26.090711 | 26.090711 | 0.000000 | 0.000000 |
| 2015-11-13 | 25.328643 | 26.090711 | -0.029208 | -0.029208 |
| 2015-11-16 | 25.742369 | 26.090711 | -0.013351 | -0.029208 |
| 2015-11-17 | 25.633019 | 26.090711 | -0.017542 | -0.029208 |
| 2015-11-18 | 26.444690 | 26.444690 | 0.000000 | -0.029208 |
| 2015-11-19 | 26.780632 | 26.780632 | 0.000000 | -0.029208 |
| 2015-11-20 | 26.897873 | 26.897873 | 0.000000 | -0.029208 |
| 2015-11-23 | 26.548404 | 26.897873 | -0.012992 | -0.029208 |
| 2015-11-24 | 26.803178 | 26.897873 | -0.003521 | -0.029208 |
| 2015-11-25 | 26.611534 | 26.897873 | -0.010645 | -0.029208 |
Summary#
Once your DataFrame has a proper DatetimeIndex:
slicing works by date
resampling uses date rules
rolling windows use time-aware windows
shifts preserve calendar structure
merges automatically align by date
plotting time series becomes trivial (
df.plot())
This is why setting the date index correctly is important.