PyXIRR Financial Functions
PyXIRR is a Rust-powered Python library offering a fast and precise collection of financial functions, including XIRR, XNPV, IRR, FV, NPV, and more. It is actively maintained with frequent minor releases, currently at version 0.10.8.
Warnings
- gotcha XIRR calculation requires both positive and negative cash flows. If a series of cash flows contains only positive or only negative values, the `xirr` function will raise an exception.
- gotcha The Multiple IRR problem can occur when cash flow signs change more than once. PyXIRR attempts to find a solution around a default guess (0.1, similar to Excel) and, if unsuccessful, tries other attempts, ultimately selecting the lowest IRR to be conservative.
- gotcha The `npv` function in PyXIRR defaults to starting from zero (NumPy compatible). Excel's `NPV` function, however, typically starts summation from the first period.
- gotcha While PyXIRR provides high performance, it might have a 'certain conversion cost' compared to direct NumPy operations, especially when passing large amounts of data between Python and the Rust core.
Install
-
pip install pyxirr
Imports
- xirr
from pyxirr import xirr
- DayCount
from pyxirr import DayCount
- All functions (e.g., fv, npv)
import pyxirr pyxirr.fv(...)
Quickstart
from datetime import date
from pyxirr import xirr
dates = [
date(2020, 1, 1),
date(2021, 1, 1),
date(2022, 1, 1)
]
amounts = [-1000, 750, 500]
# Calculate XIRR with columnar data
result = xirr(dates, amounts)
print(f"Calculated XIRR: {result:.4f}")
# XIRR also accepts iterables of tuples or pandas DataFrames
# from pyxirr import xnpv
# npv_result = xnpv(0.05, dates, amounts)
# print(f"Calculated XNPV: {npv_result:.2f}")