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).

Implementation Sample size Execution time
pyxirr (Rust) 100 45.89 us
xirr (scipy) 100 790.76 us
pure Python 100 14.37 ms
pyxirr (Rust) 1000 404.03 us
xirr (scipy) 1000 3.47 ms
pure Python 1000 35.97 ms
pyxirr (Rust) 10000 3.58 ms
xirr (scipy) 10000 28.04 ms
pure Python 10000 24.23 s

PyXIRR is ~10-20x faster than other solutions!

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:

DateLike = Union[datetime.date, datetime.datetime, numpy.datetime64, pandas.Timestamp]
Amount = Union[int, float, Decimal]
Payment = Tuple[DateLike, Amount]

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

XIRR

def xirr(
    dates: Union[CashFlowTable, CashFlowDict, DateLikeArray]
    amounts: Optional[AmountArray] = None
    guess: Optional[float] = None,
)

XNPV

def xnpv(
    rate: float,
    dates: Union[CashFlowTable, CashFlowDict, DateLikeArray]
    amounts: Optional[AmountArray] = None
)

Roadmap

  • NumPy support
  • XIRR
  • XNPV
  • FV
  • PV
  • NPV
  • IRR
  • MIRR

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

$ make test

Building and distribution

This library uses maturin to build and distribute python wheels.

$ make release
$ make publish version=<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.3.1.tar.gz (161.2 kB view details)

Uploaded Source

Built Distributions

pyxirr-0.3.1-cp39-cp39-manylinux2010_x86_64.whl (180.8 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

pyxirr-0.3.1-cp38-cp38-manylinux2010_x86_64.whl (180.9 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

pyxirr-0.3.1-cp37-cp37m-manylinux2010_x86_64.whl (180.7 kB view details)

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

pyxirr-0.3.1-cp36-cp36m-manylinux2010_x86_64.whl (180.7 kB view details)

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

File details

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

File metadata

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

File hashes

Hashes for pyxirr-0.3.1.tar.gz
Algorithm Hash digest
SHA256 4a4f92e598be29f1994719cad00e779c25a00114acedccfe934fc70de8042e98
MD5 edb9da603e52d3523bd72eac73faf4a9
BLAKE2b-256 1ac840e92dac20c5def55b425c409031992ce1ec10e5718c290bdeb81b1fd7f3

See more details on using hashes here.

Provenance

File details

Details for the file pyxirr-0.3.1-cp39-cp39-manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pyxirr-0.3.1-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 6662def403abb221b2011e067e4c817adcb352c85aa4590bab2ffeae4c564f59
MD5 14b25ae21d9c58417bafa7e5efe72e74
BLAKE2b-256 3bf542551551bb32926de6afca760e345d3f75d3123b15b876c700361e5aaac7

See more details on using hashes here.

Provenance

File details

Details for the file pyxirr-0.3.1-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pyxirr-0.3.1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 343d18fccf9d09886b3e50620728adf49a72c047aa62dfea8cdc3c4ddfef3435
MD5 1d781cc87829d4d8c702156a1155a5a0
BLAKE2b-256 b8dce28731229baae0615ee533aba227aaa44848ba284bc96940e16cb5341d68

See more details on using hashes here.

Provenance

File details

Details for the file pyxirr-0.3.1-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pyxirr-0.3.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 2dd2003043728ece25d26fef499d57b43235cd04fa2845c16862c0a8a924adef
MD5 f2d0006f17662192c5751d4b8f5a967a
BLAKE2b-256 ed9f35c45c394aa9b947bf7994d455b1d9a7b2d81d665a55a39e20a14cddfce6

See more details on using hashes here.

Provenance

File details

Details for the file pyxirr-0.3.1-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pyxirr-0.3.1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 aa373b5c3eb62e37cb0ceccb1a7baaa719278fd1fe6f489af04a60413f3fb559
MD5 fb21c66b7019272f878a433c502b1266
BLAKE2b-256 6587bb3c3a385bf9d71afd4229ea6b7d5dd21cff8a8b4bac4f3bdfe9eab48e62

See more details on using hashes here.

Provenance

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