Skip to content

Commit 5b5cfb2

Browse files
authored
CLN: Break up wrap applied output (pandas-dev#36536)
1 parent 254f509 commit 5b5cfb2

File tree

1 file changed

+69
-56
lines changed

1 file changed

+69
-56
lines changed

pandas/core/groupby/generic.py

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,64 +1210,77 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
12101210
self._insert_inaxis_grouper_inplace(result)
12111211
return result
12121212
else:
1213-
# this is to silence a DeprecationWarning
1214-
# TODO: Remove when default dtype of empty Series is object
1215-
kwargs = first_not_none._construct_axes_dict()
1216-
backup = create_series_with_explicit_dtype(dtype_if_empty=object, **kwargs)
1217-
values = [x if (x is not None) else backup for x in values]
1218-
1219-
all_indexed_same = all_indexes_same(x.index for x in values)
1220-
1221-
# GH3596
1222-
# provide a reduction (Frame -> Series) if groups are
1223-
# unique
1224-
if self.squeeze:
1225-
applied_index = self._selected_obj._get_axis(self.axis)
1226-
singular_series = len(values) == 1 and applied_index.nlevels == 1
1227-
1228-
# assign the name to this series
1229-
if singular_series:
1230-
values[0].name = keys[0]
1231-
1232-
# GH2893
1233-
# we have series in the values array, we want to
1234-
# produce a series:
1235-
# if any of the sub-series are not indexed the same
1236-
# OR we don't have a multi-index and we have only a
1237-
# single values
1238-
return self._concat_objects(
1239-
keys, values, not_indexed_same=not_indexed_same
1240-
)
1213+
# values are Series
1214+
return self._wrap_applied_output_series(
1215+
keys, values, not_indexed_same, first_not_none, key_index
1216+
)
12411217

1242-
# still a series
1243-
# path added as of GH 5545
1244-
elif all_indexed_same:
1245-
from pandas.core.reshape.concat import concat
1246-
1247-
return concat(values)
1248-
1249-
if not all_indexed_same:
1250-
# GH 8467
1251-
return self._concat_objects(keys, values, not_indexed_same=True)
1252-
1253-
# Combine values
1254-
# vstack+constructor is faster than concat and handles MI-columns
1255-
stacked_values = np.vstack([np.asarray(v) for v in values])
1256-
1257-
if self.axis == 0:
1258-
index = key_index
1259-
columns = first_not_none.index.copy()
1260-
if columns.name is None:
1261-
# GH6124 - propagate name of Series when it's consistent
1262-
names = {v.name for v in values}
1263-
if len(names) == 1:
1264-
columns.name = list(names)[0]
1265-
else:
1266-
index = first_not_none.index
1267-
columns = key_index
1268-
stacked_values = stacked_values.T
1218+
def _wrap_applied_output_series(
1219+
self,
1220+
keys,
1221+
values: List[Series],
1222+
not_indexed_same: bool,
1223+
first_not_none,
1224+
key_index,
1225+
) -> FrameOrSeriesUnion:
1226+
# this is to silence a DeprecationWarning
1227+
# TODO: Remove when default dtype of empty Series is object
1228+
kwargs = first_not_none._construct_axes_dict()
1229+
backup = create_series_with_explicit_dtype(dtype_if_empty=object, **kwargs)
1230+
values = [x if (x is not None) else backup for x in values]
1231+
1232+
all_indexed_same = all_indexes_same(x.index for x in values)
1233+
1234+
# GH3596
1235+
# provide a reduction (Frame -> Series) if groups are
1236+
# unique
1237+
if self.squeeze:
1238+
applied_index = self._selected_obj._get_axis(self.axis)
1239+
singular_series = len(values) == 1 and applied_index.nlevels == 1
1240+
1241+
# assign the name to this series
1242+
if singular_series:
1243+
values[0].name = keys[0]
1244+
1245+
# GH2893
1246+
# we have series in the values array, we want to
1247+
# produce a series:
1248+
# if any of the sub-series are not indexed the same
1249+
# OR we don't have a multi-index and we have only a
1250+
# single values
1251+
return self._concat_objects(
1252+
keys, values, not_indexed_same=not_indexed_same
1253+
)
1254+
1255+
# still a series
1256+
# path added as of GH 5545
1257+
elif all_indexed_same:
1258+
from pandas.core.reshape.concat import concat
1259+
1260+
return concat(values)
1261+
1262+
if not all_indexed_same:
1263+
# GH 8467
1264+
return self._concat_objects(keys, values, not_indexed_same=True)
1265+
1266+
# Combine values
1267+
# vstack+constructor is faster than concat and handles MI-columns
1268+
stacked_values = np.vstack([np.asarray(v) for v in values])
1269+
1270+
if self.axis == 0:
1271+
index = key_index
1272+
columns = first_not_none.index.copy()
1273+
if columns.name is None:
1274+
# GH6124 - propagate name of Series when it's consistent
1275+
names = {v.name for v in values}
1276+
if len(names) == 1:
1277+
columns.name = list(names)[0]
1278+
else:
1279+
index = first_not_none.index
1280+
columns = key_index
1281+
stacked_values = stacked_values.T
12691282

1270-
result = self.obj._constructor(stacked_values, index=index, columns=columns)
1283+
result = self.obj._constructor(stacked_values, index=index, columns=columns)
12711284

12721285
# if we have date/time like in the original, then coerce dates
12731286
# as we are stacking can easily have object dtypes here

0 commit comments

Comments
 (0)