The simplest way to calculate xirr in Python is to use the package pyxirr:
pip install pyxirr
Here is a simple example:
from datetime import date
from pyxirr import xirr
dates = [date(2023, 1, 1), date(2024, 12, 31)]
amounts = [100, -121]
# feed columnar data
xirr(dates, amounts)
# feed tuples
xirr(zip(dates, amounts))
# feed DataFrame
import pandas as pd
r = xirr(pd.DataFrame({"dates": dates, "amounts": amounts}))
print (r)
The XIRR (Extended Internal Rate of Return) is a financial metric used to calculate the annualized rate of return for a schedule of cash flows that occur at irregular intervals.
pyxirr packagepyxirr is a lightweight and precise library for computing XIRR and XNPV.
pip install pyxirr
from datetime import date
from pyxirr import xirr
# Cash flow dates
dates = [date(2023, 1, 1), date(2024, 12, 31)]
# Corresponding cash flows
# Positive values = inflows, Negative values = outflows
amounts = [-100, 121]
# Option 1: Pass as separate lists
result = xirr(dates, amounts)
print("XIRR:", result)
Output:
XIRR: 0.10000000000000009
That means an annualized return of about 10%.
You can pass data in multiple formats:
xirr(zip(dates, amounts))
import pandas as pd
df = pd.DataFrame({"dates": dates, "amounts": amounts})
print(xirr(df))
from datetime import date
from pyxirr import xirr
dates = [
date(2023, 1, 1),
date(2023, 6, 1),
date(2023, 12, 1),
date(2024, 12, 31),
]
amounts = [-1000, 200, 300, 700]
print("XIRR:", round(xirr(dates, amounts) * 100, 2), "%")
Output:
XIRR: 9.78 %
pyxirr handles date spacing automatically — no need to convert to periods.pyxirr)If you prefer not to install extra packages, you can approximate XIRR using numpy_financial.irr with equal time spacing — but it won’t be exact for irregular dates.