Statistics and classification¶
Two thin functions on top of a long-format result frame: named quantiles next to the extremes, and a class column that turns a continuous quantity into the bands a study argues in.
There is deliberately no built-in table of admissible values. Touch-voltage
limits depend on the clearing time, the standard edition and the additional
resistances assumed; a plausible-looking constant baked in here would be carried
into a result without ever being checked. classify takes its edges from the
caller, who can cite them.
statistics ¶
Summarise and classify a long-format result frame.
A parameter study produces hundreds of rows; a report needs a handful of numbers and a verdict. Polars already does the arithmetic, so these two functions add only the shape an engineering summary wants -- named quantiles next to the extremes, and a class column that turns a continuous quantity into the bands a study argues in.
Deliberately absent: any built-in table of admissible values. Touch-voltage
limits depend on the clearing time, the standard edition and the additional
resistances assumed, and a plausible-looking constant baked in here would be
carried into a result without ever being checked. :func:classify therefore
takes the edges from the caller, who can cite them.
summarize ¶
summarize(
frame: DataFrame,
value: str,
*,
by: Optional[Sequence[str]] = None,
quantiles: Sequence[float] = DEFAULT_QUANTILES,
) -> pl.DataFrame
Reduce one column to count, spread, quantiles and extremes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
Long-format results, e.g. from
:meth: |
required |
value
|
str
|
Numeric column to summarise. |
required |
by
|
sequence of str
|
Grouping columns. Without them the whole frame is one group. |
None
|
quantiles
|
sequence of float
|
Quantiles in |
DEFAULT_QUANTILES
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per group: the grouping columns, then |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a named column is missing, if the value column is not numeric, or if
a quantile lies outside |
Notes
Read n_nan before the statistics. Polars counts a NaN as a
value, not as a null: it lands in n, not in n_null, it
propagates through mean, std and every quantile, and min /
max skip it -- so a single NaN leaves a summary in which the extremes
look sound and the mean is NaN, with nothing in the row saying why. That
is a realistic input here rather than a hypothetical: a sweep point that
failed under on_error="record" writes NaN into the value column.
n_nan is the column that says how many, and it is 0 for an integer
column, which cannot carry one.
Examples:
>>> import polars as pl
>>> from groundinsight.analysis import summarize
>>> frame = pl.DataFrame({"bus_name": ["A", "A", "B"], "EPR_V": [10.0, 30.0, 5.0]})
>>> summary = summarize(frame, "EPR_V", by=["bus_name"])
>>> summary.select("bus_name", "n", "mean", "max").rows()
[('A', 2, 20.0, 30.0), ('B', 1, 5.0, 5.0)]
Source code in src/groundinsight/analysis/statistics.py
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 | |
classify ¶
classify(
frame: DataFrame,
value: str,
edges: Sequence[float],
*,
labels: Optional[Sequence[str]] = None,
name: str = "class",
) -> pl.DataFrame
Add a class column by binning a numeric column at the given edges.
edges are the interior boundaries: n edges make n + 1 classes.
Bins are closed on the right, so a value exactly on an edge falls into the
lower class -- the conservative reading when the edge is a limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
Frame to extend. |
required |
value
|
str
|
Numeric column to classify. |
required |
edges
|
sequence of float
|
Strictly increasing interior boundaries, e.g. |
required |
labels
|
sequence of str
|
Names for the classes, |
None
|
name
|
str
|
Name of the added column. Defaults to |
'class'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The input with one column added. Rows whose value is null or NaN
get a null class rather than being forced into a band. Banded, a NaN
would land in the highest class, because every |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the column is missing or not numeric, if |
Examples:
>>> import polars as pl
>>> from groundinsight.analysis import classify
>>> frame = pl.DataFrame({"EPR_V": [12.0, 80.0, 450.0]})
>>> classify(frame, "EPR_V", [75.0, 300.0])["class"].to_list()
['<= 75', '75 - 300', '> 300']
Source code in src/groundinsight/analysis/statistics.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 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 | |