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:
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).
$ export PYO3_PYTHON_VERSION=3.8.6
$ PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install ${PYO3_PYTHON_VERSION}
Install dev-requirements
$ pip install -r dev-requirements.txt
Building
$ matirun develop
Testing
$ LD_LIBRARY_PATH=${PYENV_ROOT}/versions/${PYO3_PYTHON_VERSION}/lib cargo tests --no-default-features --features tests
Building and distribution
This library uses matirun to build and distribute python wheels.
$ docker run --rm -v $(pwd):/io konstin2/maturin build --release --manylinux 2010 --strip
$ docker run --rm -v $(pwd):/io konstin2/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.3.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2ef017e06ce6695a6a6bcde7fe6250ad688292e68aaf0052504fe3c17470aee |
|
MD5 | 88d2a4117d5acc0dcd0ce566c84811f1 |
|
BLAKE2b-256 | 667eb33aa7681bcb178c1fde106e1a4bbfc50c363514ccc4b35d82ec428f9d0b |
Hashes for pyxirr-0.3.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dba7f9b1de46fff6828adf87de794faa3f0f14837eaa4f0a34512abc915cbd78 |
|
MD5 | 801f63b60ec45174eb2e73db666d8062 |
|
BLAKE2b-256 | d06d30361109c4dcd93d0b6b312e9eba4d42bce34939f3aac717d474ac41d727 |
Hashes for pyxirr-0.3.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 925ad2968d414908aa3a5e6611093b3fc29985d00dd6127e7d8f3ba2814e392e |
|
MD5 | 0780bf41eda23790b5e671fb89032e5b |
|
BLAKE2b-256 | 7ce52d6435d87f633d609ca7f545007888bd31e90e21d955be21658ccdb1ec7c |
Hashes for pyxirr-0.3.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d441edb1200dc24faa7408a87acbc30c6b32868124289f499d2fd79ee2114e6a |
|
MD5 | c28f036f6e324c91a222466944ec3d51 |
|
BLAKE2b-256 | ab1c1f47c56807ed6cc03099d743cb9eef337817396d3231c4cc59b7114842a3 |