Skip to content

Commit

Permalink
CLN: _wrap_applied_output (pandas-dev#36053)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach committed Sep 1, 2020
1 parent ecc5015 commit 2a624dc
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,6 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
key_index = self.grouper.result_index if self.as_index else None

if isinstance(first_not_none, Series):

# this is to silence a DeprecationWarning
# TODO: Remove when default dtype of empty Series is object
kwargs = first_not_none._construct_axes_dict()
Expand All @@ -1218,16 +1217,26 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):

v = values[0]

if isinstance(v, (np.ndarray, Index, Series)) or not self.as_index:
if not isinstance(v, (np.ndarray, Index, Series)) and self.as_index:
# values are not series or array-like but scalars
# self._selection_name not passed through to Series as the
# result should not take the name of original selection
# of columns
return self.obj._constructor_sliced(values, index=key_index)

else:
if isinstance(v, Series):
applied_index = self._selected_obj._get_axis(self.axis)
all_indexed_same = all_indexes_same((x.index for x in values))
singular_series = len(values) == 1 and applied_index.nlevels == 1

# GH3596
# provide a reduction (Frame -> Series) if groups are
# unique
if self.squeeze:
applied_index = self._selected_obj._get_axis(self.axis)
singular_series = (
len(values) == 1 and applied_index.nlevels == 1
)

# assign the name to this series
if singular_series:
values[0].name = keys[0]
Expand All @@ -1253,18 +1262,6 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
# GH 8467
return self._concat_objects(keys, values, not_indexed_same=True)

# GH6124 if the list of Series have a consistent name,
# then propagate that name to the result.
index = v.index.copy()
if index.name is None:
# Only propagate the series name to the result
# if all series have a consistent name. If the
# series do not have a consistent name, do
# nothing.
names = {v.name for v in values}
if len(names) == 1:
index.name = list(names)[0]

# Combine values
# vstack+constructor is faster than concat and handles MI-columns
stacked_values = np.vstack([np.asarray(v) for v in values])
Expand Down Expand Up @@ -1313,13 +1310,6 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):

return self._reindex_output(result)

# values are not series or array-like but scalars
else:
# self._selection_name not passed through to Series as the
# result should not take the name of original selection
# of columns
return self.obj._constructor_sliced(values, index=key_index)

def _transform_general(
self, func, *args, engine="cython", engine_kwargs=None, **kwargs
):
Expand Down

0 comments on commit 2a624dc

Please sign in to comment.