From 5dec71fd8e95b9d3c0015563b94da4bd8bbcfd33 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Apr 2020 18:12:34 -0400 Subject: [PATCH 1/2] CLN: Replace first_not_none function with default argument to next --- pandas/core/groupby/generic.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 093c925acbc49..bd72743c647f7 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1193,20 +1193,14 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False): key_names = self.grouper.names - # GH12824. - def first_not_none(values): - try: - return next(com.not_none(*values)) - except StopIteration: - return None - - v = first_not_none(values) + # GH12824 + first_not_none = next(com.not_none(*values), None) - if v is None: + if first_not_none is None: # GH9684. If all values are None, then this will throw an error. # We'd prefer it return an empty dataframe. return DataFrame() - elif isinstance(v, DataFrame): + elif isinstance(first_not_none, DataFrame): return self._concat_objects(keys, values, not_indexed_same=not_indexed_same) elif self.grouper.groupings is not None: if len(self.grouper.groupings) > 1: @@ -1232,20 +1226,19 @@ def first_not_none(values): key_index = None # make Nones an empty object - v = first_not_none(values) - if v is None: + if first_not_none is None: return DataFrame() - elif isinstance(v, NDFrame): + elif isinstance(first_not_none, NDFrame): # this is to silence a DeprecationWarning # TODO: Remove when default dtype of empty Series is object - kwargs = v._construct_axes_dict() - if v._constructor is Series: + kwargs = first_not_none._construct_axes_dict() + if first_not_none._constructor is Series: backup = create_series_with_explicit_dtype( **kwargs, dtype_if_empty=object ) else: - backup = v._constructor(**kwargs) + backup = first_not_none._constructor(**kwargs) values = [x if (x is not None) else backup for x in values] From 0b8e2806b6e2ee36b0f321b8d9abeb68d6d78199 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 6 Apr 2020 19:11:31 -0400 Subject: [PATCH 2/2] Added update to first_not_none back in --- pandas/core/groupby/generic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index bd72743c647f7..25c57b7847656 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1217,6 +1217,9 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False): # reorder the values values = [values[i] for i in indexer] + + # update due to the potential reorder + first_not_none = next(com.not_none(*values), None) else: key_index = Index(keys, name=key_names[0])