1"""
2Provide basic components for groupby.
3"""
4from __future__ import annotations
5
6import dataclasses
7from typing import TYPE_CHECKING
8
9if TYPE_CHECKING:
10 from collections.abc import Hashable
11
12
13@dataclasses.dataclass(order=True, frozen=True)
14class OutputKey:
15 label: Hashable
16 position: int
17
18
19# special case to prevent duplicate plots when catching exceptions when
20# forwarding methods from NDFrames
21plotting_methods = frozenset(["plot", "hist"])
22
23# cythonized transformations or canned "agg+broadcast", which do not
24# require postprocessing of the result by transform.
25cythonized_kernels = frozenset(["cumprod", "cumsum", "shift", "cummin", "cummax"])
26
27# List of aggregation/reduction functions.
28# These map each group to a single numeric value
29reduction_kernels = frozenset(
30 [
31 "all",
32 "any",
33 "corrwith",
34 "count",
35 "first",
36 "idxmax",
37 "idxmin",
38 "last",
39 "max",
40 "mean",
41 "median",
42 "min",
43 "nunique",
44 "prod",
45 # as long as `quantile`'s signature accepts only
46 # a single quantile value, it's a reduction.
47 # GH#27526 might change that.
48 "quantile",
49 "sem",
50 "size",
51 "skew",
52 "std",
53 "sum",
54 "var",
55 ]
56)
57
58# List of transformation functions.
59# a transformation is a function that, for each group,
60# produces a result that has the same shape as the group.
61
62
63transformation_kernels = frozenset(
64 [
65 "bfill",
66 "cumcount",
67 "cummax",
68 "cummin",
69 "cumprod",
70 "cumsum",
71 "diff",
72 "ffill",
73 "fillna",
74 "ngroup",
75 "pct_change",
76 "rank",
77 "shift",
78 ]
79)
80
81# these are all the public methods on Grouper which don't belong
82# in either of the above lists
83groupby_other_methods = frozenset(
84 [
85 "agg",
86 "aggregate",
87 "apply",
88 "boxplot",
89 # corr and cov return ngroups*ncolumns rows, so they
90 # are neither a transformation nor a reduction
91 "corr",
92 "cov",
93 "describe",
94 "dtypes",
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