Order categories by account number, not alphabetically

Within a category, rows were already ascending by account number, but
categories themselves were visited alphabetically - so the overall
account-number sequence down a section jumped around (e.g. 78000,
81000, 72400, 70000, 71060, 72600, 78000 again...) even though each
individual category block was internally ordered. Categories are now
visited in ascending order of their lowest account number (ties break
alphabetically), so the account-number sequence is monotonic across
the whole Revenue/Expenses section, not just within each category.

Verified: extracted the lead account code of every category block in
the Expenses section and confirmed the sequence is non-decreasing
end to end. Same totals as before (this only changes row order, not
values): revenue 5,338,866.02 / expense 3,743,572.26 /
balance_check 0.00.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
JasonFraser 2026-08-25 16:32:11 -04:00
parent d0ebc3f995
commit 091928eea0
1 changed files with 8 additions and 1 deletions

View File

@ -280,7 +280,14 @@ def _write_category_section(ws, start_row, df, canada_col, us_col, total_col, nu
if df.empty:
return subtotal_rows, row, 0.0, 0.0
for category in sorted(df["category"].unique()):
# Categories are ordered by their lowest account number (ascending),
# not alphabetically - so the account-number sequence a reader sees
# scrolling down the section is monotonic, not just ascending within
# each category block. Ties (categories sharing the same lowest
# account) fall back to alphabetical category order.
category_order = df.groupby("category")["account"].min().sort_values(kind="stable").index.tolist()
for category in category_order:
cat_df = df[df["category"] == category].sort_values(["account", "slob", "project_code"])
first_row = row + 1 # detail rows start after this category's own subtotal row
subtotal_row = row