100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Pandas Time Series Cheat Sheet

Pandas Time Series Cheat Sheet

Covers datetime indexing, resampling, rolling windows, and shifting operations for analyzing and transforming time series data in pandas.

2 PagesIntermediateMar 5, 2026

Datetime Parsing & Indexing

Turn a date column into a queryable DatetimeIndex.

python
import pandas as pddf["date"] = pd.to_datetime(df["date"])df = df.set_index("date").sort_index()# Date range generationdates = pd.date_range(start="2024-01-01", end="2024-12-31", freq="D")# Extract componentsdf["year"] = df.index.yeardf["month"] = df.index.monthdf["day_of_week"] = df.index.dayofweek# Slice by date rangedf.loc["2024-03"]                # all of March 2024df.loc["2024-01-01":"2024-06-30"]

Resampling

Aggregate a time series into fixed-frequency bins.

python
# Downsample daily -> monthly totalsmonthly = df["sales"].resample("ME").sum()   # 'ME' = month end# Downsample to weekly averageweekly = df["sales"].resample("W").mean()# Upsample and forward-fillhourly = df["sales"].resample("h").ffill()# groupby + resample togetherdf.groupby("store").resample("ME")["sales"].sum()

Rolling Windows & Shifting

Compute moving averages, lags, and period-over-period change.

python
df["rolling_7d_avg"] = df["sales"].rolling(window=7).mean()df["rolling_7d_std"] = df["sales"].rolling(window=7).std()df["expanding_sum"] = df["sales"].expanding().sum()df["sales_lag1"] = df["sales"].shift(1)          # previous perioddf["sales_pct_change"] = df["sales"].pct_change()df["sales_ewm"] = df["sales"].ewm(span=7).mean()  # exponential weighted moving avg

Key Concepts

Core building blocks of time series work in pandas.

  • DatetimeIndex- Index type enabling label-based date slicing, resampling, and time-aware alignment
  • resample()- Groups time series data into fixed frequency bins (e.g. 'D', 'W', 'ME') and aggregates
  • asfreq()- Converts to a specified frequency without aggregation, inserting NaN for missing periods
  • rolling()- Applies a function over a sliding window (e.g. a 7-day moving average)
  • shift()- Shifts values forward/backward by n periods, used for lag features and period-over-period change
  • Timezone handling- Use tz_localize() to assign a timezone and tz_convert() to convert between timezones
  • Freq aliases- Common: 'D' day, 'W' week, 'ME' month end, 'QE' quarter end, 'YE' year end, 'h' hour
Pro Tip

When resampling irregular timestamped event data, resample() to a fixed frequency first before applying rolling() - rolling() operates on row count by default, not elapsed time, unless you pass a time-based window like rolling('7D').

Was this cheat sheet helpful?

Explore Topics

#PandasTimeSeries#PandasTimeSeriesCheatSheet#DataScience#Intermediate#DatetimeParsingIndexing#Resampling#RollingWindowsShifting#KeyConcepts#Databases#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet