WeightedPandas
WeightedPandas extends pandas Series and DataFrame classes to support weighted operations. It provides drop-in replacements for pandas objects that automatically handle weights in statistical operations.
Installation
pip install weightedpandas
Requirements
- Python 3.8+
- pandas 1.4.0+
- numpy 1.20.0+
Features
- Weighted versions of common statistical operations:
sum(),mean(),var(),std()median(),quantile()corr(),cov()
- Preserves weights through arithmetic operations
- Familiar pandas interface
- Supports both Series and DataFrame objects
Usage
import pandas as pd
import numpy as np
from weightedpandas import WeightedSeries, WeightedDataFrame
# Create a weighted series
data = [1, 2, 3, 4, 5]
weights = [5, 4, 3, 2, 1]
ws = WeightedSeries(data, weights=weights)
# Calculate weighted statistics
print(f"Weighted sum: {ws.sum()}")
print(f"Weighted mean: {ws.mean()}")
print(f"Weighted median: {ws.median()}")
print(f"Weighted standard deviation: {ws.std()}")
# Create a weighted dataframe
df_data = {
'A': [1, 2, 3, 4, 5],
'B': [5, 4, 3, 2, 1]
}
wdf = WeightedDataFrame(df_data, weights=weights)
# Calculate weighted statistics
print(wdf.sum())
print(wdf.mean())
print(wdf.corr())
# Weights are preserved through operations
ws2 = ws * 2 + 1
print(ws2.weights) # Same as original weights
How Weights are Applied
In weighted calculations:
sum(): Each value is multiplied by its weight before summingmean(): The weighted sum divided by the sum of weightsvar()andstd(): Each squared deviation is weightedquantile(): The quantile is determined from the weighted cumulative distribution
Helper Functions
For convenience, you can use the following helper functions:
from weightedpandas import weighted_series, weighted_dataframe
# These are equivalent to the constructor calls
ws = weighted_series(data, weights=weights)
wdf = weighted_dataframe(df_data, weights=weights)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for weightedpandas 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 | |
|---|---|---|---|
| weightedpandas-0.1.0.tar.gz | 6.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| weightedpandas-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.8 kB
Release files / weightedpandas-0.1.0.tar.gz
| Download URL | weightedpandas-0.1.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8973d42b0bfbbef01c482cab632bb35f3b30bdc8eb3f662af92868c80368c50e
|
|
BLAKE2b-256 checksum How to use checksums |
432ef7b81ed98829d428f42d5c8c1d5888c251b2165d0a564249339ec982512b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / weightedpandas-0.1.0-py3-none-any.whl
| Download URL | weightedpandas-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5dbe6783413302ec4d8f426d1d015df8e7c0efe1f9372b57c2a9bb65ae50ec39
|
|
BLAKE2b-256 checksum How to use checksums |
541bdad9ad9838c2c8b7dc165214c83696196a6384b2a1b160567ab42f980ff7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|