How do I winsorize or truncate extreme returns in pandas?

Cap extreme returns at the 1st and 99th percentiles with clip and quantile, drop them instead with between, compare the mean, standard deviation and number of rows, and set the bounds within each date with groupby transform.

After this lesson, you can limit the effect of extreme returns on a monthly mean, either by capping their values or by removing their rows.

How do I cap extreme returns?

returns.clip(lower, upper) replaces every return below the 1st percentile with the 1st percentile and every return above the 99th with the 99th, which is called winsorizing, so all stocks stay and the extremes shrink. In the made-up month, S088’s return of −88% becomes −16.06% and S017’s return of 295% becomes 20.17%, and four returns change in all.

The month holds 200 made-up returns, S000 to S199, drawn from a normal distribution with a mean of 0.01 and a standard deviation of 0.08, with three draws replaced by 2.95, −0.88 and 1.40. .quantile([0.01, 0.99]) finds the two percentiles, interpolating between neighbouring returns. One ordinary draw, S026’s, also lies below the lower bound. A missing return compares unequal to itself, so real data add .notna() to the test for changed returns.

The following box builds the month, caps it and lists the changed returns.

import numpy as np
import pandas as pd

rng = np.random.default_rng(7)
tickers = [f"S{number:03d}" for number in range(200)]
stock_return = pd.Series(rng.normal(0.01, 0.08, 200), index=tickers)
stock_return.loc[["S017", "S088", "S150"]] = [2.95, -0.88, 1.40]
lower, upper = stock_return.quantile([0.01, 0.99])
winsorized = stock_return.clip(lower, upper)
changed = winsorized.ne(stock_return)
print(round(lower, 4), round(upper, 4), changed.sum())
print(pd.DataFrame({"raw": stock_return, "winsorized": winsorized})[changed].round(4))
-0.1606 0.2017 4
         raw  winsorized
S017  2.9500      0.2017
S026 -0.1913     -0.1606
S088 -0.8800     -0.1606
S150  1.4000      0.2017

What changes if I drop the extreme rows?

returns[returns.between(lower, upper)] keeps only the returns inside the bounds, which is called truncating or trimming, so the month loses four stocks. The three extremes push the raw mean to 1.66% and the standard deviation to 24.90%, while winsorizing gives about 0.07% and 7.29% on 200 stocks and truncating 0.02% and 6.88% on 196.

.between includes both bounds. An extreme return can be a data error, to be fixed from the source, or a real event such as a takeover, so a signal or regression is checked with other percentiles, and a portfolio’s realised performance uses the verified returns as recorded.

The following box compares the three versions.

truncated = stock_return[stock_return.between(lower, upper)]
versions = {"raw": stock_return, "winsorized": winsorized, "truncated": truncated}
print(pd.DataFrame({name: values.agg(["size", "mean", "std"]) for name, values in versions.items()}).T.round(4))
             size    mean     std
raw         200.0  0.0166  0.2490
winsorized  200.0  0.0007  0.0729
truncated   196.0  0.0002  0.0688

How do I set separate bounds for each month?

panel.groupby("date")["stock_return"].transform(winsorize) caps each month’s returns at that month’s own percentiles, so a calm month gets narrower bounds, and four returns change in each of the three months. One pair of bounds from all 600 returns changes 5, 0 and 7 returns, because February’s smaller returns never reach bounds set mostly by the other months.

The panel holds the same 200 stocks over three month-ends, with standard deviations of 8% in January, 4% in February and 8% in March. winsorize takes the percentiles as defaulted arguments. Pooled bounds for January use returns from February and March, which were not known at January’s end.

The following box winsorizes each month and compares the changed counts with pooled bounds.

def winsorize(returns, lower_quantile=0.01, upper_quantile=0.99):
    return returns.clip(returns.quantile(lower_quantile), returns.quantile(upper_quantile))

panel = pd.DataFrame({"date": np.repeat(pd.date_range("2026-01-31", periods=3, freq="ME"), 200), "ticker": np.tile(tickers, 3)})
panel["stock_return"] = rng.normal(0.01, np.repeat([0.08, 0.04, 0.08], 200))
panel["per_date"] = panel.groupby("date")["stock_return"].transform(winsorize)
panel["pooled"] = winsorize(panel["stock_return"])
print(panel.groupby("date")["stock_return"].quantile([0.01, 0.99]).unstack().round(4))
print(pd.DataFrame({column: panel[column].ne(panel["stock_return"]).groupby(panel["date"]).sum() for column in ["pooled", "per_date"]}))
              0.01    0.99
date                      
2026-01-31 -0.1664  0.1712
2026-02-28 -0.0732  0.0847
2026-03-31 -0.1704  0.1891
            pooled  per_date
date                        
2026-01-31       5         4
2026-02-28       0         4
2026-03-31       7         4

Your turn

Winsorize the one-month returns at the 5th and 95th percentiles and print the number of changed returns and the mean in percent. You should see 20 and −0.0004%.

The percentiles are arguments of winsorize.

capped = winsorize(stock_return, 0.05, 0.95)
print(capped.ne(stock_return).sum(), f"{capped.mean():.4%}")
20 -0.0004%