No project description provided
Project description
polars-fin
A plugin for polars to efficiently calculate financial metrics.
To install the plugin, run pip install polars-fin.
Capital Gains Calculation
Currently, the only function in the plugin cap_gains(...) is for calculating capital gains based on the average cost basis of a security.
Usage
The cap_gains function calculates capital gains using the average cost method. It takes three parameters:
ttype: Transaction type ("buy" for buying logic, "sell" for selling logic, other values are ignored)qty: Quantity of the transactionamt: Total amount of the transaction
Returns a Polars Struct with fields for cumulative quantity, average cost, unit cost, cost of units sold, and realized gain. Use unnest() to flatten into separate columns.
import polars as pl
from datetime import date
import polars_fin as pf
data = {
"date": [
date(2023, 1, 10),
date(2023, 2, 15),
date(2023, 3, 20),
date(2023, 4, 5),
date(2023, 5, 5),
date(2023, 6, 1),
],
"security": ["XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ"],
"type": ["buy", "buy", "sell", "dividend", "sell", "buy"],
"quantity": [100, 50, 30, 0, 120, 100],
"transaction_value": [1000.0, 600.0, 450.0, 12, 960, 700.0],
}
df = pl.DataFrame(data)
result = df.with_columns(
pf.cap_gains("type", "quantity", "transaction_value").alias("cap_gains")
).select("cap_gains").unnest("cap_gains")
result
Output:
shape: (6, 5)
┌───────────┬────────────────┬───────────────┬─────────────────┬───────────────┐
│ cumul_qty ┆ cumul_avg_cost ┆ avg_unit_cost ┆ cost_units_sold ┆ realized_gain │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═══════════╪════════════════╪═══════════════╪═════════════════╪═══════════════╡
│ 100.0 ┆ 1000.0 ┆ 10.0 ┆ 0.0 ┆ 0.0 │
│ 150.0 ┆ 1600.0 ┆ 10.666667 ┆ 0.0 ┆ 0.0 │
│ 120.0 ┆ 1280.0 ┆ 10.666667 ┆ 320.0 ┆ 130.0 │
│ 120.0 ┆ 1280.0 ┆ 10.666667 ┆ 0.0 ┆ 0.0 │
│ 0.0 ┆ 0.0 ┆ 0.0 ┆ 1280.0 ┆ -320.0 │
│ 100.0 ┆ 700.0 ┆ 7.0 ┆ 0.0 ┆ 0.0 │
└───────────┴────────────────┴───────────────┴─────────────────┴───────────────┘
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polars_fin-0.1.0.tar.gz.
File metadata
- Download URL: polars_fin-0.1.0.tar.gz
- Upload date:
- Size: 40.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57ab40fe66a02fba6838ee58bb2dafc55937f06cdfaa6a604885d26a673b250c
|
|
| MD5 |
e372d5739d5b691d3052271e9cab4943
|
|
| BLAKE2b-256 |
981c7867ad15e897f5fcf46d5b92e079e7c3861c4f19ae8249c95d14b7584367
|
File details
Details for the file polars_fin-0.1.0-cp39-abi3-win_amd64.whl.
File metadata
- Download URL: polars_fin-0.1.0-cp39-abi3-win_amd64.whl
- Upload date:
- Size: 4.2 MB
- Tags: CPython 3.9+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8a90c2ebe1656042343ffa3eb1c4d85726e4a80e82b5d1dd3444a6e744a54c4
|
|
| MD5 |
b6c0d9934533c4bdc0b94070f281ecd7
|
|
| BLAKE2b-256 |
7f795bcf969561d57626dd92ef784e1a4862a45506d447c44322fcf756ea1890
|
File details
Details for the file polars_fin-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: polars_fin-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02411405b99cdc0d6350128bfd7977bb62169586b5db1424932f38f0864a1363
|
|
| MD5 |
440311034521af185bd9837ed0a51488
|
|
| BLAKE2b-256 |
1b2f62d212469b77f162222178eaeddfd475d1e0aee4eb211923e332e2ea4cc8
|
File details
Details for the file polars_fin-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: polars_fin-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 4.6 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12fb81010e0d1632aca91be1e13ac80894bc453c3ae33d9255a25fa08ab51b6e
|
|
| MD5 |
84529c022ad777ddf797ad91a381b917
|
|
| BLAKE2b-256 |
ce069a4260b432c7ca4877e4d79005be09a5bb1e45ec3642be74f056c082cabf
|
File details
Details for the file polars_fin-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: polars_fin-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.1 MB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23b280ef31587f643b015edaaddf5223c52ff98ed03d966511b8babf198384e3
|
|
| MD5 |
eab2d4a0b616491181c7485fe712a5df
|
|
| BLAKE2b-256 |
785cbb8844d84cd6c19c80c8e36366371260bf2993a63da5a74573a7c94356c9
|
File details
Details for the file polars_fin-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: polars_fin-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.9+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14258d3ca6868b521781c3369650d186ede6768834522f3ba83ff25cbbb60fd7
|
|
| MD5 |
a3be9ade86da78d0568b027c2c400d8a
|
|
| BLAKE2b-256 |
c16c518670bca7e931543466ec2f0b53a19c8284a459518d72ca88eb5b6de322
|