Why does pandas line up labels before it subtracts?

Subtract a benchmark return from each stock on the same date, multiply returns by portfolio weights, and keep only the dates two tables share, with pandas label alignment.

After this lesson, you can subtract a benchmark return from each stock’s return on the same date and multiply stock returns by portfolio weights.

How does pandas match a benchmark to each date?

returns.sub(benchmark, axis="index") matches each benchmark return to the row with the same date label and subtracts it from every stock in that row, whatever the order of the benchmark. AAA’s excess return on 2 March is 3.0 − 1.0 = 2.0 percentage points, and on 3 March, where the benchmark has no return, both stocks get NaN.

The returns are made-up daily returns in percent of AAA and BBB on three days, one column per stock, and the benchmark lists 4 March before 2 March and leaves out 3 March. axis="index" tells pandas that the benchmark’s labels are dates, the row labels. A plain returns - benchmark matches the benchmark’s labels with the columns, the default, so it finds no ticker named after a date and gives a table of NaN with four columns, the two dates and the two tickers.

The following box subtracts the benchmark both ways.

import pandas as pd

returns = pd.DataFrame({"AAA": [3.0, 4.0, 6.0], "BBB": [1.0, 2.0, -2.0]}, index=["2026-03-02", "2026-03-03", "2026-03-04"])
benchmark = pd.Series([2.0, 1.0], index=["2026-03-04", "2026-03-02"])
print(returns.sub(benchmark, axis="index"))
print(returns - benchmark)
            AAA  BBB
2026-03-02  2.0  0.0
2026-03-03  NaN  NaN
2026-03-04  4.0 -4.0
            2026-03-02  2026-03-04  AAA  BBB
2026-03-02         NaN         NaN  NaN  NaN
2026-03-03         NaN         NaN  NaN  NaN
2026-03-04         NaN         NaN  NaN  NaN

How do I multiply returns by weights?

returns.mul(weights, axis="columns") matches each weight to the column with the same ticker and multiplies every day’s return by it, which gives each stock’s contribution to the portfolio return. On 2 March, AAA contributes 3.0 × 0.75 = 2.25 percentage points and BBB 0.25, a portfolio return of 2.50%.

The weights are fractions of the portfolio’s value at the start of each day, listed with BBB first. .sum(axis=1) adds the contributions across each row, as in the lesson on axes.

The following box computes the contributions and the portfolio returns.

weights = pd.Series({"BBB": 0.25, "AAA": 0.75})
contributions = returns.mul(weights, axis="columns")
print(contributions.assign(portfolio=contributions.sum(axis=1)))
             AAA   BBB  portfolio
2026-03-02  2.25  0.25        2.5
2026-03-03  3.00  0.50        3.5
2026-03-04  4.50 -0.50        4.0

How do I keep only the dates both tables have?

returns.index.intersection(benchmark.index) returns the labels present in both indexes, here 2 and 4 March, and .loc with those labels keeps only the dates with both a stock return and a benchmark return. .union() returns the labels of either index.

fill_value=0 in .sub() would treat the missing benchmark return as a return of zero, which is an assumption about the data.

The following box finds the shared dates and computes the excess returns on them.

shared_dates = returns.index.intersection(benchmark.index)
print(shared_dates.tolist())
print(returns.loc[shared_dates].sub(benchmark, axis="index"))
['2026-03-02', '2026-03-04']
            AAA  BBB
2026-03-02  2.0  0.0
2026-03-04  4.0 -4.0

Your turn

Print BBB’s excess return on the shared dates. You should see 0.0 on 2 March and −4.0 on 4 March.

.sub() on two Series matches their date labels.

print(returns.loc[shared_dates, "BBB"].sub(benchmark))
2026-03-02    0.0
2026-03-04   -4.0
dtype: float64