If pct_change() gives you odd numbers, the cause is usually one of six things: rows from different symbols are stacked, the default fill behaviour changed between pandas versions, a zero base turns into inf, periods counts rows instead of time, data is not sorted, or a percent of a percent hides a tiny base. Grouping by the key, passing fill_method=None, and masking zero bases fix most of it. Verified on pandas 3.0.6 with simulated data; the pad-fill default was deprecated in pandas 2.1.
Below, each pitfall gets a minimal reproduction and a fix. All data is simulated (random walks and Poisson counts), so you can rerun every snippet.
1. Stacked symbols leak into each other
A long-format frame (one row per symbol per day) is the most common shape for market or news-count data. Calling pct_change() on the column ignores where one symbol ends and the next begins.
df["flat"] = df["count"].pct_change() df["grouped"] = df.groupby("code")["count"].pct_change() In my run with three simulated symbols of 30 days each, the first row of the second symbol showed +1150% and the first row of the third showed -97.9% in the ungrouped column. Those are not moves; they are the ratio between two unrelated series. The grouped version correctly returns NaN on both boundary rows.
Fix: always groupby(key) before pct_change, and sort by key and date first.
2. The fill default depends on your pandas version
s = pd.Series([10, np.nan, 12, 0, 5.0]) s.pct_change() On pandas 3.0.6 this returns [nan, nan, nan, -1.0, inf]: the missing value stays missing and so does the change after it. On pandas 2.x the old default fill_method="pad" forward-filled the gap first, so row 2 came out as 0.2 (12 vs the padded 10), and from 2.1 onward you also got a FutureWarning. Same code, two answers.
Fix: be explicit. Pass fill_method=None, and if you really want padding, call .ffill() yourself so the choice is visible in the code.
3. Zero bases become infinity
Counts (mentions per hour, trades per minute, articles per day) hit zero often. Anything divided by a zero base is inf, and 0 → 0 is NaN.
On 60 simulated hourly counts with a Poisson mean of 1.2, raw pct_change() produced 14 inf values and 10 NaN. One inf is enough to wreck a mean, a z-score or a chart axis.
prev = s.shift() safe = s.diff() / prev.where(prev > 0) After masking, the same series had 0 inf and 24 NaN, which you can then handle on purpose: drop, cap, or switch to a difference instead of a ratio. For low counts, log1p differences or plain differences are often the better metric.
4. periods counts rows, not days
t = pd.Series([1., 2, 4], index=pd.to_datetime(["2026-10-09", "2026-10-12", "2026-10-13"])) t.pct_change() # [nan, 1.0, 1.0] t.pct_change(freq="D") # [nan, nan, 1.0] Friday to Monday counts as "one period" by row. With freq="D" pandas looks up the value exactly one calendar day earlier, finds none for Monday, and returns NaN. Neither is wrong, but you need to know which one you asked for. For trading data, row-based change on a clean trading calendar is usually what you want; calendar-based change is right for things that happen on weekends too, like news.
5. Unsorted input gives you backward changes
pct_change compares each row with the previous row in the current order. After a merge or concat, rows can come back out of date order and every change is computed against the wrong neighbour. There is no error.
Fix: df.sort_values(["code", "date"]) immediately before the call, and assert it:
assert df.groupby("code")["date"].apply(lambda d: d.is_monotonic_increasing).all() 6. A percent of a tiny base looks like news
Going from 1 to 4 mentions is +300%. Going from 400 to 460 is +15%. Sorting by percent change puts the first one on top, even though it is almost certainly noise. This matters when you rank many series, for example when you build a cross-stock heat comparison and want the names that are genuinely busier than usual.
Two practical guards:
- require a minimum base (
prev >= 20, or whatever fits your volume), and - rank on a change relative to the series' own history (a rolling z-score or percentile) rather than the raw ratio.
If you want to see what a baseline-relative ranking looks like on real A-share news data, the stock heat comparison tool puts several symbols side by side against their own history.
Checklist
- Sort by key and time.
-
groupby(key)beforepct_change. - Pass
fill_method=Noneexplicitly. - Mask zero bases; decide what to do with the resulting
NaN. - Choose
periods(rows) orfreq(calendar) on purpose. - Put a minimum base on anything you rank by percent change.
Lava (熔岩, https://lava.ai-dolphin.com) — A-share stock heat & sentiment monitoring by AI-Dolphin.
中文简介: 本文整理 pandas pct_change() 的 6 个常见坑:多股票堆叠未分组导致跨标的串值、pandas 2.x 与 3.0 默认填充行为不同、零基数产生 inf、periods 按行而非按日、未排序、小基数百分比失真,并给出可复现的修复写法(模拟数据,pandas 3.0.6 验证)。

