1"""
2Provide basic components for groupby.
3"""
4
5from __future__ import annotations
6
7import dataclasses
8from typing import TYPE_CHECKING
9
10if TYPE_CHECKING:
11 from collections.abc import Hashable
12
13
14@dataclasses.dataclass(order=True, frozen=True)
15class OutputKey:
16 label: Hashable
17 position: int
18
19
20# special case to prevent duplicate plots when catching exceptions when
21# forwarding methods from NDFrames
22plotting_methods = frozenset(["plot", "hist"])
23
24# cythonized transformations or canned "agg+broadcast", which do not
25# require postprocessing of the result by transform.
26cythonized_kernels = frozenset(["cumprod", "cumsum", "shift", "cummin", "cummax"])
27
28# List of aggregation/reduction functions.
29# These map each group to a single numeric value
30reduction_kernels = frozenset(
31 [
32 "all",
33 "any",
34 "corrwith",
35 "count",
36 "first",
37 "idxmax",
38 "idxmin",
39 "last",
40 "max",
41 "mean",
42 "median",
43 "min",
44 "nunique",
45 "prod",
46 # as long as `quantile`'s signature accepts only
47 # a single quantile value, it's a reduction.
48 # GH#27526 might change that.
49 "quantile",
50 "sem",
51 "size",
52 "skew",
53 "kurt",
54 "std",
55 "sum",
56 "var",
57 ]
58)
59
60# List of transformation functions.
61# a transformation is a function that, for each group,
62# produces a result that has the same shape as the group.
63
64
65transformation_kernels = frozenset(
66 [
67 "bfill",
68 "cumcount",
69 "cummax",
70 "cummin",
71 "cumprod",
72 "cumsum",
73 "diff",
74 "ffill",
75 "ngroup",
76 "pct_change",
77 "rank",
78 "shift",
79 ]
80)
81
82# these are all the public methods on Grouper which don't belong
83# in either of the above lists
84groupby_other_methods = frozenset(
85 [
86 "agg",
87 "aggregate",
88 "apply",
89 "boxplot",
90 # corr and cov return ngroups*ncolumns rows, so they
91 # are neither a transformation nor a reduction
92 "corr",
93 "cov",
94 "describe",
95 "expanding",
96 "ewm",
97 "filter",
98 "get_group",
99 "groups",
100 "head",
101 "hist",
102 "indices",
103 "ndim",
104 "ngroups",
105 "nth",
106 "ohlc",
107 "pipe",
108 "plot",
109 "resample",
110 "rolling",
111 "tail",
112 "take",
113 "transform",
114 "sample",
115 "value_counts",
116 ]
117)
118# Valid values of `name` for `groupby.transform(name)`
119# NOTE: do NOT edit this directly. New additions should be inserted
120# into the appropriate list above.
121transform_kernel_allowlist = reduction_kernels | transformation_kernels