Skip to content

Commit 059405c

Browse files
committed
Reworked logic for non-NDFrame cases
1 parent bb032ea commit 059405c

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

pandas/core/groupby/generic.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -1231,20 +1231,8 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
12311231

12321232
key_index = self.grouper.result_index if self.as_index else None
12331233

1234-
if isinstance(first_not_none, NDFrame):
1235-
1236-
# this is to silence a DeprecationWarning
1237-
# TODO: Remove when default dtype of empty Series is object
1238-
kwargs = first_not_none._construct_axes_dict()
1239-
if isinstance(first_not_none, Series):
1240-
backup = create_series_with_explicit_dtype(
1241-
**kwargs, dtype_if_empty=object
1242-
)
1243-
else:
1244-
backup = first_not_none._constructor(**kwargs)
1234+
if not isinstance(first_not_none, (Series, np.ndarray, Index)):
12451235

1246-
values = [x if (x is not None) else backup for x in values]
1247-
else:
12481236
# values are not series or array-like but scalars
12491237
# self._selection_name not passed through to Series as the
12501238
# result should not take the name of original selection
@@ -1256,24 +1244,31 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
12561244
self._insert_inaxis_grouper_inplace(result)
12571245
return result
12581246

1259-
v = values[0]
1247+
elif not isinstance(first_not_none, Series):
12601248

1261-
if not isinstance(v, ABCSeries):
12621249
# GH1738: values is list of arrays of unequal lengths
12631250
# TODO: sure this is right? we used to do this
12641251
# after raising AttributeError above
12651252
return self.obj._constructor_sliced(
12661253
values, index=key_index, name=self._selection_name
12671254
)
12681255

1256+
# this is to silence a DeprecationWarning
1257+
# TODO: Replace when default dtype of empty Series is object
1258+
# with backup = first_not_none._constructor(**kwargs)
1259+
kwargs = first_not_none._construct_axes_dict()
1260+
backup = create_series_with_explicit_dtype(**kwargs, dtype_if_empty=object)
1261+
values = [x if (x is not None) else backup for x in values]
1262+
1263+
v = values[0]
12691264
all_indexed_same = all_indexes_same((x.index for x in values))
12701265

12711266
# GH3596 - provide a reduction (Frame -> Series) if groups are unique
12721267
if self.squeeze:
12731268
# assign the name to this series
12741269
applied_index = self._selected_obj._get_axis(self.axis)
12751270
if len(values) == 1 and applied_index.nlevels == 1:
1276-
values[0].name = keys[0]
1271+
v.name = keys[0]
12771272

12781273
# GH2893
12791274
# we have series in the values array, we want to

0 commit comments

Comments
 (0)