pandasMFE
Risk and performance metrics for pandas: annualized return (CAGR), volatility, Sharpe ratio, maximum drawdown, historical and parametric VaR, CVaR (Expected Shortfall).
pandasMFE extends pandas without modifying it: after import pandasmfe, every
DataFrame and Series gains an .mfe namespace, using pandas' official
accessor API.
Prices can be read from a CSV file or from a SQLite database with a SQL query.
pandasMFE is an independent project and is not affiliated with the pandas project.
Installation
Requires Python 3.11+.
pip install pandasMFE # library + `pandasmfe` command
pip install "pandasMFE[excel]" # + .xlsx export
pip install "pandasMFE[data]" # + yfinance, to download real prices
Usage: the .mfe accessor
import pandas as pd
import pandasmfe # registers .mfe
prices = pd.read_csv("prices.csv", index_col=0, parse_dates=True)
# methods on PRICES
prices.mfe.risk_report(risk_free=0.02, alpha=0.95)
prices.mfe.max_drawdown()
prices.mfe.drawdown() # full drawdown series
returns = prices.mfe.returns()
# methods on RETURNS
returns.mfe.sharpe(risk_free=0.02)
returns.mfe.ann_volatility()
returns.mfe.var(alpha=0.99) # historical
returns.mfe.var(alpha=0.99, method="parametric") # Gaussian
returns.mfe.cvar(alpha=0.99)
# works on a single Series too, and chains
prices["STOCK_A"].mfe.returns().mfe.sharpe()
Usage: plain functions
import pandasmfe as pm
pm.risk_report(prices, periods_per_year=252, risk_free=0.02, alpha=0.95)
pm.var_historical(pm.simple_returns(prices), alpha=0.99)
Usage: real prices from a SQL database
Prices are stored in a SQLite database (one file, built into Python: no extra dependency) and read back with a SQL query.
pip install "pandasMFE[data]"
python examples/download_prices.py # SPY, AAPL, MSFT, TLT, GLD since 2015 -> examples/prices.db
import pandasmfe as pm
prices = pm.read_prices_sql("examples/prices.db", tickers=["SPY", "TLT"], start="2020-01-01")
prices.mfe.risk_report(risk_free=0.02)
pm.list_tickers("examples/prices.db") # what is in the database
pm.write_prices_sql(my_prices, "my.db") # save any wide DataFrame of prices
Table schema: prices(date TEXT, ticker TEXT, close REAL), one row per date and ticker.
More queries in examples/queries.sql.
Usage: command line
pandasmfe prices.csv
pandasmfe prices.csv --rf 0.02 --alpha 0.99 -o report.csv
pandasmfe excel_export.csv --eu -o report.xlsx # ';' separator, decimal comma, dd/mm/yyyy dates
pandasmfe examples/prices.db --tickers SPY TLT --start 2020-01-01
python -m pandasmfe prices.csv # equivalent
--tickers, --start and --end work with both CSV files and databases.
Expected CSV format: first column = dates, then one price column per asset.
Date,STOCK_A,STOCK_B
2024-01-02,185.6,370.9
2024-01-03,184.2,370.6
examples/sample_prices.csv contains synthetic prices (geometric Brownian motion),
useful to try the package without downloading anything.
Standalone executable
The same command can be packaged with PyInstaller into a folder that runs on a computer without Python:
pip install pyinstaller
pyinstaller --onedir --name pandasmfe --clean --noconfirm standalone/pandasmfe_app.py
dist/pandasmfe/pandasmfe examples/sample_prices.csv
PyInstaller builds for the operating system it runs on (a Windows .exe must be built on Windows).
Conventions
| Metric | Formula | Sign |
|---|---|---|
ann_return |
(∏(1+r))^(N/n) − 1 | ± |
ann_volatility |
σ · √N (ddof=1) | + |
sharpe |
mean(r − rf) / σ · √N | ± |
max_drawdown |
min(P / cummax(P) − 1) | ≤ 0 |
var_hist_95 |
−quantile₅%(r) | positive loss |
var_param_95 |
−(μ + z₅% σ) | positive loss |
cvar_hist_95 |
−mean(r ∣ r ≤ quantile₅%) | positive loss |
N = periods per year (252 for daily data), n = number of observations. Missing prices are never filled: a gap gives NaN returns, which are skipped.
Tests
pip install -e ".[dev]"
pytest
License
MIT
Metadata
Release files for pandasMFE 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pandasmfe-0.1.0.tar.gz | 20.7 kB | Without provenance | Details for pandasmfe-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pandasmfe-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pandasmfe-0.1.0-py3-none-any.whl |
Total release size: 38.3 kB
Release files / pandasmfe-0.1.0.tar.gz
| Download URL | pandasmfe-0.1.0.tar.gz |
|---|---|
| Size | 20.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d97495251a0aad3c71581a28127afcda3f31dbad6f397bbc0a71ea5a091efa67
|
|
BLAKE2b-256 checksum How to use checksums |
9f8cd11a144b4277de2cd601af1e44eaecebaf240a80772a4f5da84801f4b8c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Release files / pandasmfe-0.1.0-py3-none-any.whl
| Download URL | pandasmfe-0.1.0-py3-none-any.whl |
|---|---|
| Size | 17.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e3f44ab81c5fafbf6cc0977d619f7517037584592a7e40bc4f2d00c2bb2a01c
|
|
BLAKE2b-256 checksum How to use checksums |
158550ae3a0dd97c0bb8cdf0482845528f3f164d4c711c62dbb6008b0479206e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Security for pandasMFE 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.