Search Knowledge

© 2026 LIBREUNI PROJECT

Python for Scientific Computing / Pandas Deep Dive

Pandas: Time Series Analysis

Why Time Series in Pandas?

Pandas was originally developed in a financial context (at AQR Capital Management), which explains why it has world-class support for time series. Temporal data often requires specialized operations like resampling (converting frequency), time zone conversion, and rolling windows.

The DatetimeIndex

The key to time series in Pandas is having a DatetimeIndex.

python
1 
2import pandas as pd
3import numpy as np
4 
5# Create a range of dates
6dates = pd.date_range('2023-01-01', periods=6, freq='D')
7print("Dates Index:\n", dates)
8 
9# Create a Series with the date index
10ts = pd.Series(np.random.randn(6), index=dates)
11print("\nTime Series:\n", ts)
12 

Resampling: Upsampling and Downsampling

Resampling is the process of changing the frequency of your time series observations.

  • Downsampling: Aggregating data (e.g., daily to monthly).
  • Upsampling: Increasing frequency (e.g., monthly to daily), often requiring interpolation.
python
1 
2import pandas as pd
3import numpy as np
4 
5# Hourly data for 3 days
6rng = pd.date_range('1/1/2023', periods=72, freq='H')
7ts = pd.Series(np.random.randn(len(rng)), index=rng)
8 
9# Downsample to Daily frequency and get the mean
10daily_summary = ts.resample('D').mean()
11print("Daily Mean:\n", daily_summary)
12 

Moving Windows (Rolling Operations)

Rolling operations allow you to calculate statistics over a sliding window of time. This is common for smoothing noisy data or calculating moving averages in finance.

python
1 
2import pandas as pd
3import numpy as np
4 
5# Create 100 days of data
6ts = pd.Series(np.random.randn(100), index=pd.date_range('1/1/2023', periods=100))
7 
8# Calculate 7-day rolling mean
9rolling_mean = ts.rolling(window=7).mean()
10 
11print("Original Data (first 10):\n", ts.head(10))
12print("\n7-day Moving Average (first 10):\n", rolling_mean.head(10))
13 

Handling Time Zones

Global data often requires reconciling different time zones.

python
1 
2import pandas as pd
3 
4ts = pd.Series([1, 2, 3], index=pd.date_range('2023-01-01', periods=3, freq='D'))
5 
6# Localize to UTC
7ts_utc = ts.tz_localize('UTC')
8print("UTC Series:\n", ts_utc)
9 
10# Convert to US Eastern time
11ts_eastern = ts_utc.tz_convert('US/Eastern')
12print("\nEastern Time Series:\n", ts_eastern)
13 

Shifting and Lagging

In time series modeling, you often want to shift data forward or backward in time (e.g., to calculate percentage changes).

python
1 
2import pandas as pd
3 
4ts = pd.Series([10, 20, 30, 40], index=pd.date_range('2023-01-01', periods=4))
5 
6# Shift forward by 1
7shifted = ts.shift(1)
8print("Shifted (Lag 1):\n", shifted)
9 
10# Calculate percentage change
11pct_change = (ts - ts.shift(1)) / ts.shift(1)
12print("\nPercent Change:\n", pct_change)
13 

Period Containers

While Timestamp represents a point in time, Period represents a duration (a day, a month, a year).

python
1 
2import pandas as pd
3 
4p = pd.Period('2023-01', freq='M')
5print(f"Period: {p}")
6print(f"Next month: {p + 1}")
7 
8# Converting from Timestamp to Period
9ts = pd.Timestamp('2023-01-15')
10period = ts.to_period('M')
11print(f"Timestamp to Monthly Period: {period}")
12 

In the next section, we will integrate these data manipulation skills with Scikit-Learn to build predictive models.