API Reference¶
This page contains the complete API reference for Qanta, automatically generated from the source code.
qanta¶
Qanta - A Python library for analysts and quants.
binned_means
¶
binned_means(df: DataFrame, bins: Mapping[str, int | Collection[float]], *, quantile: bool = True, weights: str | None = None, output_weights: str | None = None, compute_std_errors: bool = False) -> pd.DataFrame
Compute n-dimensional binned means.
Splits observations (rows) into n-dimensional hyper-rectangles using the specified binning strategy, then computes the weighted mean for each bin.
Rows with NaN values in any binned column are excluded. Bins that contain no observations are dropped from the output, so the result may have fewer rows than the total number of bins requested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing the data. |
required |
bins
|
Mapping[str, int | Collection[float]]
|
Dictionary mapping column names to either:
- An integer specifying the number of bins.
- A sequence of values whose meaning depends on |
required |
quantile
|
bool
|
If True (default), use quantile-based binning (equal-count bins). If False, use equal-width binning. |
True
|
weights
|
str | None
|
Column name to use as observation weights. If None, all observations are weighted equally. Weights must be finite and positive. |
None
|
output_weights
|
str | None
|
If provided, include the sum of weights per bin in the output under this column name. Must not collide with any other output column name. |
None
|
compute_std_errors
|
bool
|
If True, include standard errors of the
means as additional columns with |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with one row per non-empty bin, containing:
- Mean values for each column specified in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Examples:
import pandas as pd
df = pd.DataFrame(
{
'x': [0, 1, 7, 8, 9, 10],
'y': [0, 1, 7, 1, 3, 9],
'w': [1, 3, 1, 1, 2, 1],
}
)
result = binned_means(
df,
bins={'x': 2, 'y': 1},
weights='w',
output_weights='w',
)
# x y w
# 0 2.0 2.0 5.0
# 1 9.0 4.0 4.0
Source code in src/qanta/binning.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
weighted_quantile
¶
weighted_quantile(values: Collection[float], weights: Collection[float], quantiles: Collection[float]) -> list[float]
Compute weighted quantiles using staircase interpolation.
Each observation is given a flat plateau in the empirical CDF proportional to its weight. Quantiles that fall within a single observation's plateau return that observation's exact value; quantiles that fall between plateaus are linearly interpolated.
Non-finite values (NaN, inf) in values are silently excluded.
If no finite values remain, returns a list of NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Collection[float]
|
Data values. |
required |
weights
|
Collection[float]
|
Weights for each value. Must be positive. Non-integer weights are internally normalized to integers for the staircase construction. |
required |
quantiles
|
Collection[float]
|
Quantiles to compute (values between 0 and 1). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of percentile values corresponding to the requested |
list[float]
|
quantiles, in the same order. |
Examples:
Heavy weight on the last value pulls the median to 3.0:
With equal weights, q=0.25 falls between the plateaus of 1 and 2 and is linearly interpolated:
Source code in src/qanta/binning.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |