Urgent.News

What's breaking now, across thousands of outlets.

Tech

pandas pct_change and cumsum: Percent Change and Running Totals

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…

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.

The 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).

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

To 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().

Overall, 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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Python PostgreSQL with asyncpg: Async Database Operations

Python PostgreSQL with asyncpg: Async Database Operations asyncpg is the fastest PostgreSQL driver for Python — pure asyncio, no thread overhead, and up to 3× faster than psycopg2 on typical…

  • asyncpg is fastest PostgreSQL driver for Python with pure asyncio implementation.
  • Installation via pip install asyncpg requires running PostgreSQL server.
  • createpool function sets up asyncpg connection pool with 2-10 connections.

The Pipeline Worked. Then the Research Outgrew It.

About a year ago, I was building a terminal-based workflow manager called Glyph.Flow. It was mostly a learning project. I wanted to understand Python better, experiment with Textual, think about…

  • Glyph.Flow started as a terminal-based workflow manager for a learning project.
  • Research pipeline proved effective for Master's thesis data processing.
  • PhD research required adaptable infrastructure for evolving questions and datasets.

More from Saturday 29 August →