eap_pl

eap_pl provides the shared Polars calculations used in Empirical Asset Pricing: A Python Reader’s Companion. The function reference lists the expected columns, return units, and output for each helper.

Installation

From the book checkout, uv sync installs the local package in the book environment. To install just the package into another environment, run this command from the checkout with that environment’s Python.

python -m pip install ./packages/eap_pl

Example

Compute cross-sectional correlations within each month. Pearson uses the 0.5th and 99.5th percentile cutoffs, and Spearman uses ranks of the original values.

from datetime import date

import polars as pl

from eap_pl import period_correlations

stocks = pl.DataFrame({
    "month": [date(2012, 1, 1)] * 4 + [date(2012, 2, 1)] * 4,
    "beta": [0.6, 0.8, 1.1, 1.4, 0.5, 0.9, 1.0, 1.3],
    "size": [20.0, 55.0, 40.0, 120.0, 100.0, 50.0, 80.0, 30.0],
})
period_correlations(stocks, ["beta", "size"], "month").sort("month")

Importing eap_pl registers .eap on Polars expressions and DataFrames. For example, pl.col("beta").eap.winsorize() clips an expression at its quantile cutoffs, and .eap.show_table() displays a DataFrame with Great Tables.

eap_pl.load_parquet() reads the local Parquet repository configured in DATA_DIR, and eap_pl.ptime() times a block of code. See Preliminaries in the book for the required WRDS tables.