to unlocking Pandas' time series capabilities is the DatetimeIndex. When your DataFrame's index is composed of datetime objects, you can perform powerful time-based selections and operations.
Python
import pandas as pd
# Create some sample time series data
dates = pd.date_range(start='2025-01-01', periods=100, freq='D')
data = np.random.randint(50, 100, size=100)
ts = pd.Series(data, index=dates)
print("Time Series Head:")
print(ts.head())
# If your dates are in a column, set it as the index
# df.set_index('date_column', inplace=True)
Time-based Indexing and Slicing
With a DatetimeIndex, you can select data using date strings in a very intuitive way.
Python
# Select data for a specific day
print("\nData for Jan 5, 2025:")
print(ts['2025-01-05'])
# Select all data for a specific month
print("\nData for January 2025:")
print(ts['2025-01'].head()) # .head() to keep it short
# Slice a date range
print("\nData from Jan 10 to Jan 15:")
print(ts['2025-01-10':'2025-01-15'])
Resampling: Changing Frequencies
Resampling is the process of converting a time series from one frequency to another.
- Downsampling: Aggregating data to a lower frequency (e.g., from daily to monthly). You must specify an aggregation function.
- Upsampling: Converting data to a higher frequency (e.g., from daily to hourly). You must specify how to fill or interpolate the new points.
Python
# Downsample from daily to monthly, taking the mean
monthly_mean = ts.resample('M').mean() # 'M' is for month-end frequency
print("\nMonthly Mean:")
print(monthly_mean.head())
# Downsample from daily to weekly, taking the sum
weekly_sum = ts.resample('W').sum() # 'W' is for weekly frequency
print("\nWeekly Sum:")
print(weekly_sum.head())
Rolling Windows: Moving Calculations
Rolling window calculations are essential for tasks like identifying trends. A rolling window of a certain size moves over the data, and a function (like mean) is calculated on the data within that window at each step. This is often used to calculate moving averages, which smooth out short-term fluctuations.
Python
# Calculate the 7-day rolling mean (7-day moving average)
rolling_avg = ts.rolling(window=7).mean()
print("\n7-Day Rolling Average:")
print(rolling_avg.head(10)) # First 6 values will be NaN
# You can plot this to see the smoothing effect
import matplotlib.pyplot as plt
ts.plot(style='-', label='Original')
rolling_avg.plot(style='--', label='Rolling Mean')
plt.legend()
plt.show()