Skip to content

Commit 39c5e29

Browse files
authored
CLN: _wrap_applied_output (#36260)
1 parent 06b3f5d commit 39c5e29

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

pandas/core/groupby/generic.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -1203,16 +1203,27 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
12031203

12041204
values = [x if (x is not None) else backup for x in values]
12051205

1206-
v = values[0]
1207-
1208-
if not isinstance(v, (np.ndarray, Index, Series)) and self.as_index:
1206+
if isinstance(first_not_none, (np.ndarray, Index)):
1207+
# GH#1738: values is list of arrays of unequal lengths
1208+
# fall through to the outer else clause
1209+
# TODO: sure this is right? we used to do this
1210+
# after raising AttributeError above
1211+
return self.obj._constructor_sliced(
1212+
values, index=key_index, name=self._selection_name
1213+
)
1214+
elif not isinstance(first_not_none, Series):
12091215
# values are not series or array-like but scalars
12101216
# self._selection_name not passed through to Series as the
12111217
# result should not take the name of original selection
12121218
# of columns
1213-
return self.obj._constructor_sliced(values, index=key_index)
1219+
if self.as_index:
1220+
return self.obj._constructor_sliced(values, index=key_index)
1221+
else:
1222+
result = DataFrame(values, index=key_index, columns=[self._selection])
1223+
self._insert_inaxis_grouper_inplace(result)
1224+
return result
12141225

1215-
if isinstance(v, Series):
1226+
else:
12161227
all_indexed_same = all_indexes_same((x.index for x in values))
12171228

12181229
# GH3596
@@ -1253,31 +1264,19 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
12531264

12541265
if self.axis == 0:
12551266
index = key_index
1256-
columns = v.index.copy()
1267+
columns = first_not_none.index.copy()
12571268
if columns.name is None:
12581269
# GH6124 - propagate name of Series when it's consistent
12591270
names = {v.name for v in values}
12601271
if len(names) == 1:
12611272
columns.name = list(names)[0]
12621273
else:
1263-
index = v.index
1274+
index = first_not_none.index
12641275
columns = key_index
12651276
stacked_values = stacked_values.T
12661277

12671278
result = self.obj._constructor(stacked_values, index=index, columns=columns)
12681279

1269-
elif not self.as_index:
1270-
# We add grouping column below, so create a frame here
1271-
result = DataFrame(values, index=key_index, columns=[self._selection])
1272-
else:
1273-
# GH#1738: values is list of arrays of unequal lengths
1274-
# fall through to the outer else clause
1275-
# TODO: sure this is right? we used to do this
1276-
# after raising AttributeError above
1277-
return self.obj._constructor_sliced(
1278-
values, index=key_index, name=self._selection_name
1279-
)
1280-
12811280
# if we have date/time like in the original, then coerce dates
12821281
# as we are stacking can easily have object dtypes here
12831282
so = self._selected_obj

pandas/tests/groupby/test_apply.py

+10
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,16 @@ def test_apply_frame_to_series(df):
381381
tm.assert_numpy_array_equal(result.values, expected.values)
382382

383383

384+
def test_apply_frame_not_as_index_column_name(df):
385+
# GH 35964 - path within _wrap_applied_output not hit by a test
386+
grouped = df.groupby(["A", "B"], as_index=False)
387+
result = grouped.apply(len)
388+
expected = grouped.count().rename(columns={"C": np.nan}).drop(columns="D")
389+
# TODO: Use assert_frame_equal when column name is not np.nan (GH 36306)
390+
tm.assert_index_equal(result.index, expected.index)
391+
tm.assert_numpy_array_equal(result.values, expected.values)
392+
393+
384394
def test_apply_frame_concat_series():
385395
def trans(group):
386396
return group.groupby("B")["C"].sum().sort_values()[:2]

0 commit comments

Comments
 (0)