Skip to content

Commit 2a624dc

Browse files
authored
CLN: _wrap_applied_output (#36053)
1 parent ecc5015 commit 2a624dc

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

pandas/core/groupby/generic.py

+13-23
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,6 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
12061206
key_index = self.grouper.result_index if self.as_index else None
12071207

12081208
if isinstance(first_not_none, Series):
1209-
12101209
# this is to silence a DeprecationWarning
12111210
# TODO: Remove when default dtype of empty Series is object
12121211
kwargs = first_not_none._construct_axes_dict()
@@ -1218,16 +1217,26 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
12181217

12191218
v = values[0]
12201219

1221-
if isinstance(v, (np.ndarray, Index, Series)) or not self.as_index:
1220+
if not isinstance(v, (np.ndarray, Index, Series)) and self.as_index:
1221+
# values are not series or array-like but scalars
1222+
# self._selection_name not passed through to Series as the
1223+
# result should not take the name of original selection
1224+
# of columns
1225+
return self.obj._constructor_sliced(values, index=key_index)
1226+
1227+
else:
12221228
if isinstance(v, Series):
1223-
applied_index = self._selected_obj._get_axis(self.axis)
12241229
all_indexed_same = all_indexes_same((x.index for x in values))
1225-
singular_series = len(values) == 1 and applied_index.nlevels == 1
12261230

12271231
# GH3596
12281232
# provide a reduction (Frame -> Series) if groups are
12291233
# unique
12301234
if self.squeeze:
1235+
applied_index = self._selected_obj._get_axis(self.axis)
1236+
singular_series = (
1237+
len(values) == 1 and applied_index.nlevels == 1
1238+
)
1239+
12311240
# assign the name to this series
12321241
if singular_series:
12331242
values[0].name = keys[0]
@@ -1253,18 +1262,6 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
12531262
# GH 8467
12541263
return self._concat_objects(keys, values, not_indexed_same=True)
12551264

1256-
# GH6124 if the list of Series have a consistent name,
1257-
# then propagate that name to the result.
1258-
index = v.index.copy()
1259-
if index.name is None:
1260-
# Only propagate the series name to the result
1261-
# if all series have a consistent name. If the
1262-
# series do not have a consistent name, do
1263-
# nothing.
1264-
names = {v.name for v in values}
1265-
if len(names) == 1:
1266-
index.name = list(names)[0]
1267-
12681265
# Combine values
12691266
# vstack+constructor is faster than concat and handles MI-columns
12701267
stacked_values = np.vstack([np.asarray(v) for v in values])
@@ -1313,13 +1310,6 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
13131310

13141311
return self._reindex_output(result)
13151312

1316-
# values are not series or array-like but scalars
1317-
else:
1318-
# self._selection_name not passed through to Series as the
1319-
# result should not take the name of original selection
1320-
# of columns
1321-
return self.obj._constructor_sliced(values, index=key_index)
1322-
13231313
def _transform_general(
13241314
self, func, *args, engine="cython", engine_kwargs=None, **kwargs
13251315
):

0 commit comments

Comments
 (0)