Skip to main content

Rust-powered collection of financial functions.

Project description

rust-lang.org License pypi versions

PyXIRR

Rust-powered collection of financial functions.

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 than other solutions!

Powered by github-action-benchmark.

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 pv(
    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}*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyxirr-0.5.0.tar.gz (191.8 kB view details)

Uploaded Source

Built Distributions

pyxirr-0.5.0-cp39-none-win_amd64.whl (122.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

pyxirr-0.5.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (180.1 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

pyxirr-0.5.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (339.3 kB view details)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

pyxirr-0.5.0-cp38-none-win_amd64.whl (122.8 kB view details)

Uploaded CPython 3.8 Windows x86-64

pyxirr-0.5.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (180.1 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

pyxirr-0.5.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (339.5 kB view details)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

pyxirr-0.5.0-cp37-none-win_amd64.whl (122.7 kB view details)

Uploaded CPython 3.7 Windows x86-64

pyxirr-0.5.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (180.1 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.5+ x86-64

pyxirr-0.5.0-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (339.5 kB view details)

Uploaded CPython 3.7m macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

pyxirr-0.5.0-cp36-none-win_amd64.whl (122.7 kB view details)

Uploaded CPython 3.6 Windows x86-64

pyxirr-0.5.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (180.1 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.5+ x86-64

pyxirr-0.5.0-cp36-cp36m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (339.5 kB view details)

Uploaded CPython 3.6m macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

File details

Details for the file pyxirr-0.5.0.tar.gz.

File metadata

  • Download URL: pyxirr-0.5.0.tar.gz
  • Upload date:
  • Size: 191.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.10.6

File hashes

Hashes for pyxirr-0.5.0.tar.gz
Algorithm Hash digest
SHA256 d33a66e1c058820e2765877257413cfc5e44ac2bc3a36a4a8b00049549454778
MD5 46b54d59cf0568ba61b0b0279ef500c9
BLAKE2b-256 0c87fa03df9e7acbdab28971e92652944a3375c80b8cae534f973052485d83d3

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp39-none-win_amd64.whl.

File metadata

  • Download URL: pyxirr-0.5.0-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 122.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.10.6

File hashes

Hashes for pyxirr-0.5.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 f015d269f8df7bb9edca632a671b75e38b625bcc19a914d9d2076558faed01b5
MD5 6a457f9cbc67d7965d70d83a4b4381ed
BLAKE2b-256 e6d48bb66d7dff8688a2cefa35d5eb21c6f2321e9c4b47c77ee7ce2a704a8306

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pyxirr-0.5.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dfb429a86612573efa15bb0f5589fef405059e1e65743cb42935d6a73d2eb2e0
MD5 11176b379c860815039780428226bd66
BLAKE2b-256 a9f83fde100d7ec81403c0b800d3521782fadbb6f305acc1aea2f5384619231c

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pyxirr-0.5.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 0b325d67e7d61ccbade2f23ba4ca491872abc3645b9c3522bcbd102d01c2998c
MD5 8466527912dbd93619cc2a5048214726
BLAKE2b-256 ab1eeb5f649ab0274a620e1b72b80c67717d63f619e2bd4db52d055ddcb88082

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp38-none-win_amd64.whl.

File metadata

  • Download URL: pyxirr-0.5.0-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 122.8 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.10.6

File hashes

Hashes for pyxirr-0.5.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 2b59e1e6f475e173aca6e0ee5f1503219ccaeda0bbd0172e9446341fbf352cc7
MD5 9eb42cef2bd632bffcf379498f654e50
BLAKE2b-256 a9b6c9610ad4dc465e7c654f7da299a4fbb5bd5261d743ff5b80bc82446cdd50

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pyxirr-0.5.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 39cbef9e75af15f3fdceb3bbe60f81fe5df7563774cc718f4d8b496ab02f6811
MD5 a9cec195558a9a1800aec8ea99aafaa9
BLAKE2b-256 ca28cfe21719b05603c9b3c5d8d7bb0b95ec30e4a4b2b05f7306eec9aeb0db18

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pyxirr-0.5.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 68a874d452e84127ecb1baed4aaaabc1ec7f911e8ada60401425fdf85c874fde
MD5 3020e4537c28d7011f5197430b002969
BLAKE2b-256 6e5ff5ed4943dbafbe3273223cb98bec96fe3641b57c1f80c210ada621f51bcd

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp37-none-win_amd64.whl.

File metadata

  • Download URL: pyxirr-0.5.0-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 122.7 kB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.10.6

File hashes

Hashes for pyxirr-0.5.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 419e6eba5a2cfca48ee4eefa1d51e741dc6ba2dde0f35f60a4ed0162e216fc94
MD5 718204769d54d1450fae5636272aa177
BLAKE2b-256 14fcc094687fb76833b69554dce6421cb558b90a64ec498894367bf350a9527f

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pyxirr-0.5.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4eb366e29416f693740b429c8e55bac453f613f1e6550bd5cc47e9acc144d2b5
MD5 f795997706b17311aeade4445fb1b27d
BLAKE2b-256 53ccf4677eebfffe79623b201beaf957e8517786cfb305334e47ba7ea7985704

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pyxirr-0.5.0-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 bfc597247cca32c0834b2f46656ef2012ffbabe482c93258c74c236b7549dcec
MD5 f193b315d91c9be84a8d768438d9ecae
BLAKE2b-256 ab811dbe5fa27850b92f283b0f15446dc90f046d234b44863ab889efc3e75410

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp36-none-win_amd64.whl.

File metadata

  • Download URL: pyxirr-0.5.0-cp36-none-win_amd64.whl
  • Upload date:
  • Size: 122.7 kB
  • Tags: CPython 3.6, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.10.6

File hashes

Hashes for pyxirr-0.5.0-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 7f79221b66581cd9c00896ed212247a2ad1f17ea264dbef9a86d18c3c9b7d79b
MD5 d90fcd88508acda9c843ec09eb1f48fd
BLAKE2b-256 7dde6538a0e05040743564e59a18470760b9af778745d0d60c5de62bf28202b8

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pyxirr-0.5.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 04d9901b184d67a96cd747c6a2880fe1ddf51e0380b4ddaf09b2c9eb489403e2
MD5 e2f591fcc4b98e9f28fc2b06e7566e14
BLAKE2b-256 fd202eed805236506eee6f97c96992ce8d8301c2dc3c712661d98b3d176a7bb5

See more details on using hashes here.

File details

Details for the file pyxirr-0.5.0-cp36-cp36m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pyxirr-0.5.0-cp36-cp36m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 3f67792391f5840830a20dc9ddebba224c6c7501c9a25de1bc38f51a51c49b9f
MD5 b06a216bec28e94f725f3b494de12182
BLAKE2b-256 d268b7ae1ae8fc50645d302fc3651ac82da3a9226b4d669924dec2221dadf8ac

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page