From bb7f6651e90ab370bc8cd02094d87d2954cee3a2 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 8 Oct 2019 08:44:26 -0700 Subject: [PATCH 1/6] More AbstractMethodErrors in base class --- pandas/core/groupby/generic.py | 3 +++ pandas/core/groupby/groupby.py | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 0bd6f746e4f3a..8953cdcb911f3 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -132,6 +132,9 @@ def pinner(cls): class SeriesGroupBy(GroupBy): _apply_whitelist = base.series_apply_whitelist + def _iterate_slices(self): + yield self._selection_name, self._selected_obj + @property def _selection_name(self): """ diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index cb56f7b8d535b..74213d28e05be 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -747,7 +747,7 @@ def _python_apply_general(self, f): ) def _iterate_slices(self): - yield self._selection_name, self._selected_obj + raise AbstractMethodError(self) def transform(self, func, *args, **kwargs): raise AbstractMethodError(self) @@ -872,6 +872,12 @@ def _cython_transform(self, how, numeric_only=True, **kwargs): def _wrap_aggregated_output(self, output, names=None): raise AbstractMethodError(self) + def _wrap_transformed_output(self, output, names=None): + raise AbstractMethodError(self) + + def _wrap_applied_output(self, keys, values, not_indexed_same=False): + raise AbstractMethodError(self) + def _cython_agg_general(self, how, alt=None, numeric_only=True, min_count=-1): output = {} for name, obj in self._iterate_slices(): From deab5081ed9b73a47c4528d0361ba51c222f40f5 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 8 Oct 2019 08:48:04 -0700 Subject: [PATCH 2/6] Removed _wrap_applied_output with wrong signature --- pandas/core/groupby/groupby.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 74213d28e05be..a3808d1e85e33 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -928,9 +928,6 @@ def _python_agg_general(self, func, *args, **kwargs): return self._wrap_aggregated_output(output) - def _wrap_applied_output(self, *args, **kwargs): - raise AbstractMethodError(self) - def _concat_objects(self, keys, values, not_indexed_same=False): from pandas.core.reshape.concat import concat From fad1af2dea4d312b90be26d17767c38e8a38541c Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 8 Oct 2019 08:51:49 -0700 Subject: [PATCH 3/6] Cleaned up naming conventions --- pandas/core/groupby/generic.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 8953cdcb911f3..519da2f941005 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -326,7 +326,7 @@ def _aggregate_multiple_funcs(self, arg, _level): return DataFrame(results, columns=columns) - def _wrap_output(self, output, index, names=None): + def _wrap_series_output(self, output, index, names=None): """ common agg/transform wrapping logic """ output = output[self._selection_name] @@ -339,13 +339,13 @@ def _wrap_output(self, output, index, names=None): return Series(output, index=index, name=name) def _wrap_aggregated_output(self, output, names=None): - result = self._wrap_output( + result = self._wrap_series_output( output=output, index=self.grouper.result_index, names=names ) return self._reindex_output(result)._convert(datetime=True) def _wrap_transformed_output(self, output, names=None): - return self._wrap_output(output=output, index=self.obj.index, names=names) + return self._wrap_series_output(output=output, index=self.obj.index, names=names) def _wrap_applied_output(self, keys, values, not_indexed_same=False): if len(keys) == 0: @@ -869,7 +869,7 @@ def aggregate(self, func=None, *args, **kwargs): if self.grouper.nkeys > 1: return self._python_agg_general(func, *args, **kwargs) elif args or kwargs: - result = self._aggregate_generic(func, *args, **kwargs) + result = self._aggregate_frame(func, *args, **kwargs) else: # try to treat as if we are passing a list @@ -878,7 +878,7 @@ def aggregate(self, func=None, *args, **kwargs): [func], _level=_level, _axis=self.axis ) except Exception: - result = self._aggregate_generic(func) + result = self._aggregate_frame(func) else: result.columns = Index( result.columns.levels[0], name=self._selected_obj.columns.name @@ -1002,7 +1002,7 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1): return new_items, new_blocks - def _aggregate_generic(self, func, *args, **kwargs): + def _aggregate_frame(self, func, *args, **kwargs): if self.grouper.nkeys != 1: raise AssertionError("Number of keys must be 1") @@ -1025,7 +1025,7 @@ def _aggregate_generic(self, func, *args, **kwargs): wrapper = lambda x: func(x, *args, **kwargs) result[name] = data.apply(wrapper, axis=axis) - return self._wrap_generic_output(result, obj) + return self._wrap_frame_output(result, obj) def _aggregate_item_by_item(self, func, *args, **kwargs): # only for axis==0 @@ -1509,7 +1509,7 @@ def _gotitem(self, key, ndim, subset=None): raise AssertionError("invalid ndim for _gotitem") - def _wrap_generic_output(self, result, obj): + def _wrap_frame_output(self, result, obj): result_index = self.grouper.levels[0] if self.axis == 0: From 32736ae1573174b13945e72a3aafe68d72e4c0f0 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 8 Oct 2019 08:57:45 -0700 Subject: [PATCH 4/6] Aggregate as abstractmethod --- pandas/core/groupby/groupby.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index a3808d1e85e33..3458975592d76 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -739,6 +739,9 @@ def f(g): return result + def aggregate(self, func=None, *args, **kwargs): + raise AbstractMethodError + def _python_apply_general(self, f): keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis) From d6d2b61cc1b15e5eb940813690624e39728f33bb Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 8 Oct 2019 09:04:24 -0700 Subject: [PATCH 5/6] black --- pandas/core/groupby/generic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 519da2f941005..41a5195008f0c 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -345,7 +345,9 @@ def _wrap_aggregated_output(self, output, names=None): return self._reindex_output(result)._convert(datetime=True) def _wrap_transformed_output(self, output, names=None): - return self._wrap_series_output(output=output, index=self.obj.index, names=names) + return self._wrap_series_output( + output=output, index=self.obj.index, names=names + ) def _wrap_applied_output(self, keys, values, not_indexed_same=False): if len(keys) == 0: From ac1fd6836c64c0052c604bd105e624850cce9c49 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 8 Oct 2019 09:15:37 -0700 Subject: [PATCH 6/6] Removed aggregate from GroupBy (already in SelectionMixin) --- pandas/core/groupby/groupby.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 3458975592d76..a3808d1e85e33 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -739,9 +739,6 @@ def f(g): return result - def aggregate(self, func=None, *args, **kwargs): - raise AbstractMethodError - def _python_apply_general(self, f): keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)