A sales table has one row per sale, each with the day it happened. You want one total per calendar month, and a month in which nothing was sold must still appear, with a total of zero.
Write monthly_totals(df).
df has the columns date (text in the form YYYY-MM-DD) and amount (number, which can be negative for a refund). The rows are in no particular order and df has at least one row.
Return a new DataFrame with two columns:
month: the month as text in the form YYYY-MM, for example "2024-03"total: the sum of amount over the rows of that monthThere is one row for every calendar month from the month of the earliest date to the month of the latest date, in time order. A month in that span with no rows has a total of 0.
month must be text, not a pandas period or timestamp.df. If you want a helper column, add it to a copy (df.copy()), not to df itself.resample or pd.Grouper. Its name changed between pandas versions ("M" became "ME"), so one spelling warns on newer versions and the other fails on older ones. Grouping by dt.to_period("M"), or by year and month, behaves the same on every version.