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