TheAlgorithms/Python · error · ValueError
Cash flows list cannot be empty
Error message
Cash flows list cannot be empty
What it means
Raised by present_value() when cash_flows is empty (falsy). Summing discounts over zero flows is defined mathematically as 0, but the library treats an empty schedule as caller error — most likely a missing or misparsed input — and rejects it before the sum() generator runs.
Source
Thrown at financial/present_value.py:32
>>> present_value(0.13, [10, 20.70, -293, 297])
4.69
>>> present_value(0.07, [-109129.39, 30923.23, 15098.93, 29734,39])
-42739.63
>>> present_value(0.07, [109129.39, 30923.23, 15098.93, 29734,39])
175519.15
>>> present_value(-1, [109129.39, 30923.23, 15098.93, 29734,39])
Traceback (most recent call last):
...
ValueError: Discount rate cannot be negative
>>> present_value(0.03, [])
Traceback (most recent call last):
...
ValueError: Cash flows list cannot be empty
"""
if discount_rate < 0:
raise ValueError("Discount rate cannot be negative")
if not cash_flows:
raise ValueError("Cash flows list cannot be empty")
present_value = sum(
cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(cash_flows)
)
return round(present_value, ndigits=2)
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Check the flows list is non-empty before calling; investigate why it is empty.
- Fix the upstream filter/parsing that produced zero cash flows.
- If an empty schedule is legitimate in your domain, return 0 explicitly in your wrapper rather than calling the function.
Example fix
# before
flows = [f for f in all_flows if f.date <= cutoff] # cutoff too early -> []
pv = present_value(0.03, flows)
# after
flows = [f for f in all_flows if f.date <= cutoff]
if not flows:
raise ValueError('no cash flows in window')
pv = present_value(0.03, flows) Defensive patterns
Strategy: validation
Validate before calling
if not cash_flows:
raise ValueError('cash flow schedule is empty')
present_value(rate, cash_flows) Type guard
def is_nonempty_numeric_flows(flows: object) -> bool:
return isinstance(flows, list) and len(flows) > 0 and all(
isinstance(f, (int, float)) for f in flows
) Try / catch
try:
pv = present_value(r, flows)
except ValueError as exc:
if 'Cash flows' in str(exc):
pv = 0.0
else:
raise Prevention
- Assert non-empty inputs after date/window filters.
- Beware falsy values: present_value(0.03, None) hits this same branch, not a TypeError.
When it happens
Trigger: Calling present_value(0.03, []) or with any falsy flows value (empty tuple, None). Reached only after the discount-rate check passes.
Common situations: Parsing a CSV/date-range filter that yields no rows, a typo'd schedule variable defaulting to [], or passing None for the flows argument.
Related errors
- number_of_years must be > 0
- nominal_annual_percentage_rate must be >= 0
- Discount rate cannot be negative
- Window size must be a positive integer
- Useful years cannot be less than 1
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/b95b557da0b633de.
Report an issue: GitHub.