Rust-powered collection of financial functions.
Project description
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:
# `None` if the calculation fails to converge or result is not finite (`inf` or `nan`)
FiniteOrNone = Optional[float]
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]
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
- the amounts and dates arrays (
XIRR
# raises: InvalidPaymentsError
def xirr(
dates: Union[CashFlowTable, CashFlowDict, DateLikeArray],
amounts: Optional[AmountArray] = None,
guess: Optional[float] = None,
) -> FiniteOrNone
XNPV
# raises: InvalidPaymentsError
def xnpv(
rate: float,
dates: Union[CashFlowTable, CashFlowDict, DateLikeArray],
amounts: Optional[AmountArray] = None,
) -> FiniteOrNone
IRR
# raises: InvalidPaymentsError
def irr(amounts: AmountArray, guess: Optional[float] = None) -> FiniteOrNone
NPV
# raises: InvalidPaymentsError
def npv(rate: float, amounts: AmountArray) -> FiniteOrNone
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
$ 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pyxirr-0.4.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 736d256e7a329524f71e075cdbbda47c4f5d0413673fc12292c198f02f80c211 |
|
MD5 | 4b45398602e9d685a3d1b528bb073cb8 |
|
BLAKE2b-256 | 7aa36d5a11a6a1f40bc5eac282e9d6c7714b0fc45336131998e99ade54d4ccce |
Hashes for pyxirr-0.4.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a724b433b9371740e7bee8fde065d7931a7b5d7efe703f8bdcd2517bf7d56506 |
|
MD5 | da6459661ad91ca1770e1a9778e7702e |
|
BLAKE2b-256 | f83de356c44552ca4f4b89cdab64faeeeb832f0917a729d92abea450c18dedd3 |
Hashes for pyxirr-0.4.0-cp39-cp39-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2779f76f4b3705c3b4c3454c9ab4fa870236bd8896d8482e3e25ff21bdfaae0 |
|
MD5 | 6a802bf628afccd8cde948445e8a0ce8 |
|
BLAKE2b-256 | 2995fafa2e46a619a4863c4cb04ba06ba88062d7bb4f4ebc2afe5bb1d36b2ca1 |
Hashes for pyxirr-0.4.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27c52f2df10495c4075bab150190b426f54b5fc642a6a5ee0a560f97fb4cd13e |
|
MD5 | 9a90cc23c93bae10c99f9d0abc985c33 |
|
BLAKE2b-256 | cf641ca7d7870567e11164faae95f75f75407a2adb78c4adfd714e739ac5632c |
Hashes for pyxirr-0.4.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6007d00f3f0b580c2a418e8729a59b0d630154fcb46e3e9469f1f5e979f2045 |
|
MD5 | 9ca76dee66a96d86f0aabd1a31d90af7 |
|
BLAKE2b-256 | 37e5c661c9036bdc0918b855b0f4c83dbaa2ef17304b83eeab8fe70e6773c472 |
Hashes for pyxirr-0.4.0-cp38-cp38-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18580f3c1c8bebca24dbbe368889626750c0dafc2c3cd74c9f19fb1ff66f2424 |
|
MD5 | 01f6a7ad78d2ddfcf2527c3cf0b6d392 |
|
BLAKE2b-256 | 0a49c84bd4aafe55c4880478f640f85810ec0a1b6064f5bf398d285a38a089ee |
Hashes for pyxirr-0.4.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85bccdbf5a8eb7d305cc6cb54d6be39c64450e93bb2bb4ebdf3f4994fafcce34 |
|
MD5 | 6257e7b7f7be226eb7d4204f1fc9fe28 |
|
BLAKE2b-256 | 6b0158de2c255f7391b6d92a3d4024350147352ec916d3d1907145b143458891 |
Hashes for pyxirr-0.4.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b1d50eeded6d362c224298f3983d737cb0e92bb2cad62de4f265f18480dc6a2 |
|
MD5 | d76ef181cc764a50151a4063249e98f2 |
|
BLAKE2b-256 | 458a112b88c705f84ee356425be94e64bda566fba29b1f78f4c37f5ff9b2062a |
Hashes for pyxirr-0.4.0-cp37-cp37m-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fba7ba7bd2ff6d48efe71c3be33e2b0005f339b7673daddbbe611658e9b929b4 |
|
MD5 | da759c0fd17f4bfb90adca1761d6f929 |
|
BLAKE2b-256 | b810abb4449366540d4d507b03cc2f3ca44a4866a5f8d12dfd8eadefdb8b8965 |
Hashes for pyxirr-0.4.0-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 794323d1f6bd9b795bd0b192e6d839913f82e4da0a5b7de557db01d5c1d94745 |
|
MD5 | 6969044501ffe4c963d948c6f8ba083a |
|
BLAKE2b-256 | 437bd6e98d07f267919406204344b332f9a29b175cfedd435890f0041f4b4a9b |
Hashes for pyxirr-0.4.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2940b9590081e0fd58bc8cf331c0c6cf2215e7faeb0cf9df2017b20564e51a15 |
|
MD5 | bad48066311de59f644ba07f7175597b |
|
BLAKE2b-256 | 1fa49812ae445bee0d220a3968e108ad2c83527e482eb64f96535415382865db |
Hashes for pyxirr-0.4.0-cp36-cp36m-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f5a7da27b2c30abbc9f1d3868930a023722b28675fc6b921a4e68ad608148a7 |
|
MD5 | e99267b4c12d9e89f758dc51d0e2c39f |
|
BLAKE2b-256 | e5aa3c8df4e9fd306bf9eb46534fac388bdedd4d3144e8b3a5f1bffc1680384e |