Extends pandas Series and DataFrame classes to support weighted operations
Project description
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.
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 Distribution
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 weightedpandas-0.1.0.tar.gz.
File metadata
- Download URL: weightedpandas-0.1.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8973d42b0bfbbef01c482cab632bb35f3b30bdc8eb3f662af92868c80368c50e
|
|
| MD5 |
81d4946dab83083cbb062610c5d3ac01
|
|
| BLAKE2b-256 |
432ef7b81ed98829d428f42d5c8c1d5888c251b2165d0a564249339ec982512b
|
File details
Details for the file weightedpandas-0.1.0-py3-none-any.whl.
File metadata
- Download URL: weightedpandas-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5dbe6783413302ec4d8f426d1d015df8e7c0efe1f9372b57c2a9bb65ae50ec39
|
|
| MD5 |
5be64334835af137ca665ba58bba1a0b
|
|
| BLAKE2b-256 |
541bdad9ad9838c2c8b7dc165214c83696196a6384b2a1b160567ab42f980ff7
|