Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pandas/core/groupby/base.py : 54%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Provide basic components for groupby. These definitions
3hold the whitelist of methods that are exposed on the
4SeriesGroupBy and the DataFrameGroupBy objects.
5"""
6import collections
8from pandas.core.dtypes.common import is_list_like, is_scalar
10OutputKey = collections.namedtuple("OutputKey", ["label", "position"])
13class GroupByMixin:
14 """
15 Provide the groupby facilities to the mixed object.
16 """
18 def _gotitem(self, key, ndim, subset=None):
19 """
20 Sub-classes to define. Return a sliced object.
22 Parameters
23 ----------
24 key : string / list of selections
25 ndim : 1,2
26 requested ndim of result
27 subset : object, default None
28 subset to act on
29 """
30 # create a new object to prevent aliasing
31 if subset is None:
32 subset = self.obj
34 # we need to make a shallow copy of ourselves
35 # with the same groupby
36 kwargs = {attr: getattr(self, attr) for attr in self._attributes}
38 # Try to select from a DataFrame, falling back to a Series
39 try:
40 groupby = self._groupby[key]
41 except IndexError:
42 groupby = self._groupby
44 self = type(self)(subset, groupby=groupby, parent=self, **kwargs)
45 self._reset_cache()
46 if subset.ndim == 2:
47 if is_scalar(key) and key in subset or is_list_like(key):
48 self._selection = key
49 return self
52# special case to prevent duplicate plots when catching exceptions when
53# forwarding methods from NDFrames
54plotting_methods = frozenset(["plot", "hist"])
56common_apply_whitelist = (
57 frozenset(
58 [
59 "quantile",
60 "fillna",
61 "mad",
62 "take",
63 "idxmax",
64 "idxmin",
65 "tshift",
66 "skew",
67 "corr",
68 "cov",
69 "diff",
70 ]
71 )
72 | plotting_methods
73)
75series_apply_whitelist = (
76 (
77 common_apply_whitelist
78 | {
79 "nlargest",
80 "nsmallest",
81 "is_monotonic_increasing",
82 "is_monotonic_decreasing",
83 }
84 )
85) | frozenset(["dtype", "unique"])
87dataframe_apply_whitelist = common_apply_whitelist | frozenset(["dtypes", "corrwith"])
89# cythonized transformations or canned "agg+broadcast", which do not
90# require postprocessing of the result by transform.
91cythonized_kernels = frozenset(["cumprod", "cumsum", "shift", "cummin", "cummax"])
93cython_cast_blacklist = frozenset(["rank", "count", "size", "idxmin", "idxmax"])
95# List of aggregation/reduction functions.
96# These map each group to a single numeric value
97reduction_kernels = frozenset(
98 [
99 "all",
100 "any",
101 "count",
102 "first",
103 "idxmax",
104 "idxmin",
105 "last",
106 "mad",
107 "max",
108 "mean",
109 "median",
110 "min",
111 "ngroup",
112 "nth",
113 "nunique",
114 "prod",
115 # as long as `quantile`'s signature accepts only
116 # a single quantile value, it's a reduction.
117 # GH#27526 might change that.
118 "quantile",
119 "sem",
120 "size",
121 "skew",
122 "std",
123 "sum",
124 "var",
125 ]
126)
128# List of transformation functions.
129# a transformation is a function that, for each group,
130# produces a result that has the same shape as the group.
131transformation_kernels = frozenset(
132 [
133 "backfill",
134 "bfill",
135 "corrwith",
136 "cumcount",
137 "cummax",
138 "cummin",
139 "cumprod",
140 "cumsum",
141 "diff",
142 "ffill",
143 "fillna",
144 "pad",
145 "pct_change",
146 "rank",
147 "shift",
148 "tshift",
149 ]
150)
152# these are all the public methods on Grouper which don't belong
153# in either of the above lists
154groupby_other_methods = frozenset(
155 [
156 "agg",
157 "aggregate",
158 "apply",
159 "boxplot",
160 # corr and cov return ngroups*ncolumns rows, so they
161 # are neither a transformation nor a reduction
162 "corr",
163 "cov",
164 "describe",
165 "dtypes",
166 "expanding",
167 "filter",
168 "get_group",
169 "groups",
170 "head",
171 "hist",
172 "indices",
173 "ndim",
174 "ngroups",
175 "ohlc",
176 "pipe",
177 "plot",
178 "resample",
179 "rolling",
180 "tail",
181 "take",
182 "transform",
183 ]
184)
185# Valid values of `name` for `groupby.transform(name)`
186# NOTE: do NOT edit this directly. New additions should be inserted
187# into the appropriate list above.
188transform_kernel_whitelist = reduction_kernels | transformation_kernels