{
  "id": 4193953,
  "title": "pandas pct_change and cumsum: Percent Change and Running Totals",
  "url": "https://urgent.news/2026/08/29/pandas-pct-change-and-cumsum-percent-change-and-running-totals",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-29T13:00:12.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/michaelnocito/pandas-pctchange-and-cumsum-percent-change-and-running-totals-5ca1"
  },
  "original_language": "en",
  "account": "In this guide, Michael Nocito explains the differences between pandas' pct_change() and cumsum() functions, and how they handle missing values and zero denominators. He demonstrates using the data from an orders table covering January to May 2026, with no orders in April.\n\nThe first step is to create a series with one row per period. Two methods are shown: groupby with to_period() gives a row only for months with data, while resample() with MS frequency creates a row for every month, including those with no data (filling them with zero).\n\npct_change() calculates the percent change between a value and the one above it. When applied to the grouped series, the first row is NaN because there is no previous value, and the change from April to May is -14.6%. However, when pct_change() is applied to the resampled series, the change from April to May is -100%, as April's zero value is divided by zero.\n\nTo avoid this misleading result, pct_change() results can be replaced with NaN using numpy's inf and -inf values. cumsum() calculates a running total, adding up all values up to the current row. When applied to the resampled series, the cumulative total becomes inf at May due to the division by zero in pct_change().\n\nOverall, the guide emphasizes understanding how these functions handle missing data and zero denominators to avoid misinterpretation of results. Proper use of resample() and pct_change() replacement with NaN ensures accurate calculations and clearer reporting.",
  "summary": "By Michael Nocito , data analyst · Published August 8, 2026 By the end of this page you can turn transactions into a monthly series, add period-on-period change and a cumulative total, get a share-of-total column, smooth a noisy line, and run all of it separately for every group. It is about twenty-five minutes, and every number below came out of running the code. Here is what to do today, on the…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}