My CFO Needed to Know Who Owed Us Money: I Built a Python Accounts Receivable Pipeline
My CFO Needed to Know Who Owed Us Money: I Built a Python Accounts Receivable Pipeline
Our CFO had a straightforward question.
Who owes us money, how much do they owe, and how late is it?
The accounting system could export our accounts receivable data.
But the export wasn't quite ready for analysis.
Customer names weren't consistent. Dates used different formats. Dollar amounts contained currency symbols and commas. Payment statuses varied. There were even duplicate records.
The information was there.
It just needed some work.
So we'll give the dirty CSV to our business analyst and use Python and Pandas to turn it into something the CFO can actually use.
Our pipeline looks like this:
accounting system
↓
dirty CSV
↓
Python + Pandas
↓
clean + calculate + classify
↓
├── CSV → Excel → CFO
└── JSON → software
The job isn't complicated.
Clean the records.
Calculate what's still owed.
Determine how late the invoices are.
Then put the results somewhere useful.
The Dirty Data
Our business analyst receives a file called:
accounts_receivable.csv
A few rows look like this:
invoice_number,customer,invoice_date,due_date,invoice_amount,amount_paid,status
1047,Acme Manufacturing,10/01/2026,10/31/2026,"$10,000.00","$4,000.00",OPEN
1048, NORTH STAR TOOLS ,2026-10-08,2026-11-07,"$7,250.00",$0,open
1049,Green Valley Supply,10-15-2026,11-14-2026,"$3,800.00","$3,800.00",PAID
1050,ACME MANUFACTURING,09/10/2026,10/10/2026,"$12,450.00","$2,450.00",Open
1051,Blue Ridge Parts,10/25/2026,11/24/2026," $5,600.00 ","$1,000.00",OPEN
1051,Blue Ridge Parts,10/25/2026,11/24/2026," $5,600.00 ","$1,000.00",OPEN
1052,Central Office Supply,11/02/2026,,"$2,900.00",$0,OPEN
It's not terrible data.
That's part of the problem.
It looks reasonable enough until we try to work with it.
Acme Manufacturing
and
ACME MANUFACTURING
should represent the same customer.
OPEN,
open, and
Open
mean the same thing.
Dates use several formats.
Currency fields contain characters we can't use directly in calculations.
Invoice 1051 appears twice.
Invoice 1052 is missing its
due_date, so it cannot be used in our aging analysis and will be removed with the
other incomplete records.
Our analyst doesn't need more data.
The analyst needs trustworthy data.
Follow the Data
Consider invoice 1050:
1050,ACME MANUFACTURING,09/10/2026,10/10/2026,"$12,450.00","$2,450.00",Open
The invoice was originally for $12,450.
The customer has paid $2,450.
So the amount we're actually waiting for is:
$12,450 - $2,450 = $10,000
That's the balance our CFO cares about.
We can also compare the due date with our analysis date to determine how late the invoice is.
For this example, we'll use November 30, 2026 as our fixed analysis date.
Now we have a useful little pipeline:
raw invoice
↓
clean invoice
↓
outstanding balance
↓
days overdue
↓
aging category
We're no longer just storing an invoice.
We're describing its business significance.
Cleaning and Classifying the Data
Before we calculate anything, we'll clean the records.
We'll standardize customer names and payment statuses.
We'll remove dollar signs and commas from currency fields and convert those fields to numbers.
We'll turn the dates into proper datetime values.
We'll remove incomplete and duplicate records.
Then we'll calculate the balance and classify each unpaid invoice into one of these aging categories:
- Current
- 1–30 days overdue
- 31–60 days overdue
- 61–90 days overdue
- 90+ days overdue
A paid invoice will simply be marked
Paid.
Those categories give our CFO something much more useful than a giant list of open invoices.
The Python Program
Here's the complete Python and Pandas program:
import pandas as pd
INPUT_FILE = "accounts_receivable.csv"
CSV_OUTPUT = "accounts_receivable_report.csv"
JSON_OUTPUT = "accounts_receivable_report.json"
ANALYSIS_DATE = pd.Timestamp("2026-11-30")
# INPUT
df = pd.read_csv(INPUT_FILE)
# Clean column names
df.columns = (
df.columns
.str.strip()
.str.lower()
)
# Clean customer names
df["customer"] = (
df["customer"]
.str.strip()
.str.title()
)
# Clean currency fields
for column in ["invoice_amount", "amount_paid"]:
df[column] = (
df[column]
.astype(str)
.str.replace("$", "", regex=False)
.str.replace(",", "", regex=False)
.str.strip()
)
df[column] = pd.to_numeric(
df[column],
errors="coerce"
)
# Convert dates
df["invoice_date"] = pd.to_datetime(
df["invoice_date"],
errors="coerce",
format="mixed"
)
df["due_date"] = pd.to_datetime(
df["due_date"],
errors="coerce",
format="mixed"
)
# Standardize payment status
df["status"] = (
df["status"]
.str.strip()
.str.lower()
)
# Remove incomplete records
df = df.dropna(
subset=[
"invoice_number",
"customer",
"invoice_date",
"due_date",
"invoice_amount",
"amount_paid"
]
)
# Remove duplicates
df = df.drop_duplicates()
# Calculate outstanding balance
df["balance"] = (
df["invoice_amount"] - df["amount_paid"]
).clip(lower=0)
# Calculate days overdue
df["days_overdue"] = (
ANALYSIS_DATE - df["due_date"]
).dt.days
df["days_overdue"] = (
df["days_overdue"]
.clip(lower=0)
.astype(int)
)
# Set payment status from the calculated balance
df.loc[df["balance"] == 0, "status"] = "paid"
df.loc[df["balance"] > 0, "status"] = "open"
# Paid invoices are not overdue
df.loc[df["balance"] == 0, "days_overdue"] = 0
def aging_category(row):
if row["balance"] == 0:
return "Paid"
days = row["days_overdue"]
if days == 0:
return "Current"
elif days <= 30:
return "1-30 days overdue"
elif days <= 60:
return "31-60 days overdue"
elif days <= 90:
return "61-90 days overdue"
else:
return "90+ days overdue"
df["aging_category"] = df.apply(
aging_category,
axis=1
)
# Put older outstanding invoices first
df = df.sort_values(
by=["days_overdue", "balance"],
ascending=[False, False]
)
df = df.reset_index(drop=True)
# OUTPUT
df.to_csv(
CSV_OUTPUT,
index=False,
date_format="%Y-%m-%d"
)
df.to_json(
JSON_OUTPUT,
orient="records",
date_format="iso",
indent=2
)
print(df)
print(f"\nCreated {CSV_OUTPUT}")
print(f"Created {JSON_OUTPUT}")
The code follows the same path as our data:
dirty records
↓
clean
↓
calculate
↓
classify
↓
CSV + JSON
There's nothing mysterious going on.
Every operation exists because of something our CFO needs to know.
The Data Coming Out
Now our cleaned CSV looks more like this:
invoice_number,customer,invoice_date,due_date,invoice_amount,amount_paid,status,balance,days_overdue,aging_category
1050,Acme Manufacturing,2026-09-10,2026-10-10,12450.0,2450.0,open,10000.0,51,31-60 days overdue
1047,Acme Manufacturing,2026-10-01,2026-10-31,10000.0,4000.0,open,6000.0,30,1-30 days overdue
1048,North Star Tools,2026-10-08,2026-11-07,7250.0,0.0,open,7250.0,23,1-30 days overdue
1051,Blue Ridge Parts,2026-10-25,2026-11-24,5600.0,1000.0,open,4600.0,6,1-30 days overdue
1049,Green Valley Supply,2026-10-15,2026-11-14,3800.0,3800.0,paid,0.0,0,Paid
Look at invoice 1050 again.
Our original record was:
1050,ACME MANUFACTURING,09/10/2026,10/10/2026,"$12,450.00","$2,450.00",Open
Now it's:
1050,Acme Manufacturing,2026-09-10,2026-10-10,12450.0,2450.0,open,10000.0,51,31-60 days overdue
Same invoice.
But now our CFO can immediately see that $10,000 remains outstanding and that the invoice falls into the 31–60 day aging category.
We haven't created new facts. We've made the existing facts easier to use.
One Dataset, Two Audiences
Our program creates two files.
The first is:
accounts_receivable_report.csv
That's for the CFO.
It can be opened in Excel, filtered by aging category, sorted by outstanding balance, or used to examine a particular customer.
The second is:
accounts_receivable_report.json
That's for software.
A couple of records might look like this:
[
{
"invoice_number": 1050,
"customer": "Acme Manufacturing",
"invoice_date": "2026-09-10T00:00:00.000",
"due_date": "2026-10-10T00:00:00.000",
"invoice_amount": 12450.0,
"amount_paid": 2450.0,
"status": "open",
"balance": 10000.0,
"days_overdue": 51,
"aging_category": "31-60 days overdue"
},
{
"invoice_number": 1047,
"customer": "Acme Manufacturing",
"invoice_date": "2026-10-01T00:00:00.000",
"due_date": "2026-10-31T00:00:00.000",
"invoice_amount": 10000.0,
"amount_paid": 4000.0,
"status": "open",
"balance": 6000.0,
"days_overdue": 30,
"aging_category": "1-30 days overdue"
}
]
The data is the same.
The audience is different.
cleaned accounts receivable data
↓
├── CSV → Excel → CFO
│
└── JSON → applications, APIs and pipelines
CSV gives people something familiar to work with.
JSON gives software structured data it can pass into another application, API, automation process, or another stage of a data pipeline.
Pandas can give us both.
Now It's the CFO's Data
We started with a simple question.
Who owes us money, how much do they owe, and how late is it?
The answer was already somewhere inside our accounting export.
It was just buried in inconsistent data.
Python and Pandas cleaned the records, calculated the outstanding balances, determined how late the invoices were, and assigned aging categories.
Then we sent the same information in two directions:
accounting system
↓
dirty CSV
↓
Python + Pandas
↓
clean + calculate + classify
↓
├── CSV → Excel → CFO
└── JSON → software
Maybe the CFO decides an overdue account needs attention.
Maybe the accounts receivable manager follows up with a customer.
Maybe the business decides to look more closely at its payment terms.
Those decisions happen downstream.
Our program doesn't make them.
That's an important boundary.
Python doesn't decide which customer deserves a phone call.
Pandas doesn't decide how the company should handle an overdue account.
They do something more basic.
They turn messy records into evidence that somebody can use.
The code is the pipe.
The business decision still belongs to the person at the other end.
