Pandas#
In the previous sections, we explored financial data analysis using NumPy—working directly with price matrices, return matrices, and portfolio vectors. NumPy is fast, expressive, and ideal for implementing the core mathematical operations that underpin quantitative finance. But real-world financial datasets are not just numbers.
Pandas: Introduction#
Financial data almost always includes:
Dates (trading days, timestamps)
Company identifiers (tickers, names, CIKs)
Categories (sectors, industries, countries)
Metadata (market cap, currency, headquarters location)
Working with raw numeric arrays quickly becomes cumbersome when you need to align data by dates, merge multiple sources, or keep track of which symbol corresponds to which row.
This is where pandas becomes essential.
Why Pandas?
Pandas is a high-level data analysis library built on top of NumPy that provides:
Labelled rows and columns
DataFrames have row labels (e.g., dates)
and column labels (e.g., tickers)
This makes financial time series far easier to work with.
Built-in time series functionality
Resampling (daily → monthly)
Shifting and lagging
Rolling windows (
rolling)Expanding windows (
expanding)Automated alignment using DatetimeIndex
Most operations “just work” once your index is a set of dates.
Easy data import and export. Pandas can load:
CSV
Excel
Parquet
SQL tables
Web API results
This makes it a natural hub for working with multiple financial datasets (prices, fundamentals, macro, and text-based metadata).
Built-in descriptive and statistical tools. DataFrames provide intuitive methods:
.describe().corr(),.cov().pct_change().rolling().mean(),.rolling().std()
These tools replicate much of what we manually coded in NumPy—only now they operate automatically across labelled columns.
Grouped and categorical analysis. Financial datasets often involve groups:
sectors
industries
asset classes
countries
Pandas makes this easy through
groupby,agg, andpivot_table, which we will use extensively in later sections.
NumPy vs Pandas: When to Use Each#
Task |
Best Tool |
Why |
|---|---|---|
Vectorized math, linear algebra |
NumPy |
Fast, minimal overhead |
Portfolio optimization, risk modeling |
NumPy + Pandas |
Numeric ops + labelled Series |
Time series manipulation |
Pandas |
Date indexes and resampling |
Merging multiple datasets |
Pandas |
Joins, alignment, handling missing data |
Exploratory data analysis |
Pandas |
Descriptive stats, convenient slicing |
Creating clean visuals |
Pandas + seaborn |
DataFrames integrate smoothly with plotting libraries |
A common pattern is: Load with pandas → compute with pandas → optimize with NumPy → present with pandas/seaborn.
Real Financial Data Requires Labels#
To illustrate why pandas is so crucial, consider two datasets we’ll use later:
Daily stock prices (columns = tickers, index = dates)
company_overviews.csv (metadata such as industry, market cap, location)
NumPy could hold these, but pandas lets us:
set dates as the index automatically,
merge metadata with prices on ticker symbols,
group companies by industry and compute average performance,
join price data with currency or macroeconomic series,
handle missing data cleanly when companies IPO or delist.
These are all bread-and-butter operations in financial analytics.
Recall from the previous sections that we had to use a separate lists to track the stock symbols (rows) and data (columns). With Pandas, they’re part of the same data structure.