Skip to main content

rust-lang.org License pypi versions

PyXIRR

Rust-powered collection of financial functions for Python.

Features:

  • correct
  • blazingly fast
  • works with iterators
  • works with unordered input
  • no external dependencies

Installation

pip install pyxirr

Benchmarks

Rust implementation has been tested against existing xirr package (uses scipy.optimize under the hood) and the implementation from the Stack Overflow (pure python).

bench

PyXIRR is ~10-20x faster in XIRR calculation. More than 10x faster than numpy-financial.

Powered by github-action-benchmark and plotly.js.

Live benchmarks are hosted on Github Pages.

Examples

from datetime import date
from pyxirr import xirr

dates = [date(2020, 1, 1), date(2021, 1, 1), date(2022, 1, 1)]
amounts = [-1000, 1000, 1000]

# feed columnar data
xirr(dates, amounts)
# feed iterators
xirr(iter(dates), (x for x in amounts))
# feed an iterable of tuples
xirr(zip(dates, amounts))
# feed a dictionary
xirr(dict(zip(dates, amounts)))

Numpy and Pandas support

import numpy as np
import pandas as pd

# feed numpy array
xirr(np.array([dates, amounts]))
xirr(np.array(dates), np.array(amounts))
# feed DataFrame (columns names doesn't matter; ordering matters)
xirr(pd.DataFrame({"a": dates, "b": amounts}))

API reference

Let's define type annotations:

# `None` if the calculation fails to converge or result is NAN.
# could return `inf` or `-inf`
FloatOrNone = Optional[float]

DateLike = Union[datetime.date, datetime.datetime, numpy.datetime64, pandas.Timestamp]
Rate = float  # rate as decimal, not percentage, normally between [-1, 1]
Guess = Optional[Rate]
Amount = Union[int, float, Decimal]
Payment = Tuple[DateLike, Amount]

DateLikeArray = Iterable[DateLike]
AmountArray = Iterable[Amount]
CashFlowTable = Iterable[Payment]
CashFlowDict = Dict[DateLike, Amount]

Exceptions

  • InvalidPaymentsError. Occurs if either:
    • the amounts and dates arrays (AmountArray, DateLikeArray) are of different lengths
    • the given arrays do not contain at least one negative and at least one positive value

XIRR

# raises: InvalidPaymentsError
def xirr(
    dates: Union[CashFlowTable, CashFlowDict, DateLikeArray],
    amounts: Optional[AmountArray] = None,
    guess: Guess = None,
) -> FloatOrNone

XNPV

# raises: InvalidPaymentsError
def xnpv(
    rate: Rate,
    dates: Union[CashFlowTable, CashFlowDict, DateLikeArray],
    amounts: Optional[AmountArray] = None,
) -> FloatOrNone

IRR

Compute the Internal Rate of Return (IRR)

# raises: InvalidPaymentsError
def irr(amounts: AmountArray, guess: Guess = None) -> FloatOrNone

NPV

Compute the Net Present Value.

# raises: InvalidPaymentsError
def npv(rate: Rate, amounts: AmountArray) -> FloatOrNone

FV

Compute the future value.

def fv(
    rate: Rate, # Rate of interest per period
    nper: int, # Number of compounding periods
    pmt: Amount, # Payment
    pv: Amount, # Present value
    pmt_at_begining: bool = False  # When payments are due
) -> FloatOrNone

PV

Compute the present value.

def pv(
    rate: Rate, # Rate of interest per period
    nper: int, # Number of compounding periods
    pmt: Amount, # Payment
    fv: Amount = 0, # Future value
    pmt_at_begining: bool = False  # When payments are due
) -> FloatOrNone

MIRR

Modified internal rate of return.

def mirr(
    values: AmountArray, # Cash flows. Must contain at least one positive and one negative value or nan is returned.
    finance_rate: Rate, # Interest rate paid on the cash flows
    reinvest_rate: Rate, # Interest rate received on the cash flows upon reinvestment
) -> FloatOrNone

Roadmap

  • NumPy support
  • XIRR
  • XNPV
  • NPV
  • IRR
  • FV
  • PV
  • MIRR
  • Improve docs, add more tests
  • other functions from numpy-financial

Development

Running tests with pyo3 is a bit tricky. In short, you need to compile your tests without extension-module feature to avoid linking errors. See the following issues for the details: #341, #771.

If you are using pyenv, make sure you have the shared library installed (check for ${PYENV_ROOT}/versions/<version>/lib/libpython3.so file).

$ PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install <version>

Install dev-requirements

$ pip install -r dev-requirements.txt

Building

$ maturin develop

Testing

$ LD_LIBRARY_PATH=${PYENV_ROOT}/versions/3.8.6/lib cargo test --no-default-features

Building and distribution

This library uses maturin to build and distribute python wheels.

$ docker run --rm -v $(pwd):/io konstin2/maturin build --release --manylinux 2010 --strip
$ maturin upload target/wheels/pyxirr-${version}*

Release files for pyxirr 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyxirr 0.5.1
File Size Uploaded
pyxirr-0.5.1.tar.gz 192.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pyxirr 0.5.1
File
pyxirr-0.5.1-cp39-none-win_amd64.whl CPython 3.9 none Windows x86-64 Details
pyxirr-0.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.5+ x86-64 Details
pyxirr-0.5.1-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl CPython 3.9 CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64), macOS 10.9+ x86-64, macOS 11.0+ ARM64 Details
pyxirr-0.5.1-cp38-none-win_amd64.whl CPython 3.8 none Windows x86-64 Details
pyxirr-0.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.5+ x86-64 Details
pyxirr-0.5.1-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl CPython 3.8 CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64), macOS 10.9+ x86-64, macOS 11.0+ ARM64 Details
pyxirr-0.5.1-cp37-none-win_amd64.whl CPython 3.7 none Windows x86-64 Details
pyxirr-0.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64 Details
pyxirr-0.5.1-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64, macOS 11.0+ ARM64, macOS 10.9+ universal2 (ARM64, x86-64) Details
pyxirr-0.5.1-cp36-none-win_amd64.whl CPython 3.6 none Windows x86-64 Details
pyxirr-0.5.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64 Details
pyxirr-0.5.1-cp36-cp36m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.9+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64, macOS 10.9+ x86-64 Details

Total release size: 2.8 MB

Release files / pyxirr-0.5.1.tar.gz

Download URL pyxirr-0.5.1.tar.gz
Size 192.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e51e834fe16c2e0e6eca8560cd1c6be15bffbe3b95183db3a4ba085f2761ea32
BLAKE2b-256 checksum
How to use checksums
6d4c84f9a6d11a19c1bfaaf1b085a483fdc60743f085f6bb83d687021cbd06e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp39-none-win_amd64.whl

Download URL pyxirr-0.5.1-cp39-none-win_amd64.whl
Size 122.8 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
11520bcbbf089fc1b5a04f3e5418f15f8b0189d36d7f4db1e59329b30968c700
BLAKE2b-256 checksum
How to use checksums
84599a532535c50ce38a1538c6bf5b85f6dfe0bbf85fd51f1ec0d3f5f16d40e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl

Download URL pyxirr-0.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Size 180.1 kB
Tags CPython 3.9 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
0c5c409c94ad01eb7435acb430b7fcb37b6a93e18ee5d0559137aa476eb66f5c
BLAKE2b-256 checksum
How to use checksums
32312681741ff1b130b9cc57f3e45824697ab69c5f0f1ded3d5b514cdd91feac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl

Download URL pyxirr-0.5.1-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Size 339.2 kB
Tags CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
63206699c30a26bb72dd49ead8e9b413f0c19753bfa7577a0e84033a152554c0
BLAKE2b-256 checksum
How to use checksums
57d5e9224020a7f60e5308ddd009af05f17661b3ae24f163656759ec39919e40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp38-none-win_amd64.whl

Download URL pyxirr-0.5.1-cp38-none-win_amd64.whl
Size 122.8 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
cb1b5252655bd2f0708dc09f72d66fd1c7a190751ce55bdd5271b60bb8ef36a6
BLAKE2b-256 checksum
How to use checksums
616fece8583d591e7131b19d5b444b2b86b3c5b9a74f9726f33ad115c39aeffe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl

Download URL pyxirr-0.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Size 180.1 kB
Tags CPython 3.8 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
586a3cbd16e395c6ed07d538eab49daf35383f368f41e8364b75dfc3a30d9ed6
BLAKE2b-256 checksum
How to use checksums
7c2c9f00166c4533161e933b714361ca555caa4ad4367fa08aa1604c5be8ba69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl

Download URL pyxirr-0.5.1-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Size 339.2 kB
Tags CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
cfa084102c1914565164f9e4176e2f1813249abb880fd3726b26bfbec19efad2
BLAKE2b-256 checksum
How to use checksums
078098579b51060f970de59be857a0bff47c0d297ee63804636ea177f33b873b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp37-none-win_amd64.whl

Download URL pyxirr-0.5.1-cp37-none-win_amd64.whl
Size 122.8 kB
Tags CPython 3.7 Windows x86-64
SHA-256 checksum
How to use checksums
2ff17863065519a9c5948c38add5969621f749fe644ccf1c8f37fd690276e3fd
BLAKE2b-256 checksum
How to use checksums
f385017ce24251e1bd5876f75453e65a66987c00cd972b15920e3fc06586796c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl

Download URL pyxirr-0.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Size 180.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
b2ae044f7a43c50fbcfd31a73a66b0616f3604bb74efe6841016f66c98efa5bf
BLAKE2b-256 checksum
How to use checksums
642043f9dd5a26798ec2b6805739094a7bc33ea91d269068e0b4168c3a6c61dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl

Download URL pyxirr-0.5.1-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Size 339.2 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
80c092a0b25338696a619c41e7cfd40d2f6f40b8917cf268df66dd92ea2558c9
BLAKE2b-256 checksum
How to use checksums
e1eeca631da311fd6a4c1873da781677e3c2465895bc2a491a7e83b13b633980
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp36-none-win_amd64.whl

Download URL pyxirr-0.5.1-cp36-none-win_amd64.whl
Size 122.8 kB
Tags CPython 3.6 Windows x86-64
SHA-256 checksum
How to use checksums
682c8b806f21e5a96d069508ec62bbdfa6751c883420026d3dc817f64a2d3001
BLAKE2b-256 checksum
How to use checksums
60ed2a5f6f99d058448d77e29ed38c99c3a026a5e980e08b4715180dde51fcd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl

Download URL pyxirr-0.5.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Size 180.1 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
aef7d03f55b36fcb129b4a34d85570277b1e001521e9efed3b6c83658feb3432
BLAKE2b-256 checksum
How to use checksums
8e77dd9559cc22f03a35c60ab89f19c86866c35c566b9307cc992259ade3e74d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6

Release files / pyxirr-0.5.1-cp36-cp36m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl

Download URL pyxirr-0.5.1-cp36-cp36m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Size 339.4 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
eb85f763105541380f7084700881b8d67425f3d958694b3415d9dba7be3937d8
BLAKE2b-256 checksum
How to use checksums
6b21feda5dcaa338bfe97f2209dee9db451cbe1f5905327881b706db533cfc22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.6
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page