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.
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.
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%.