Skip to content

Commit b9cf9be

Browse files
authored
CLN: Replace first_not_none function with default argument to next (pandas-dev#33343)
1 parent 48838da commit b9cf9be

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

pandas/core/groupby/generic.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -1197,20 +1197,14 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
11971197

11981198
key_names = self.grouper.names
11991199

1200-
# GH12824.
1201-
def first_not_none(values):
1202-
try:
1203-
return next(com.not_none(*values))
1204-
except StopIteration:
1205-
return None
1206-
1207-
v = first_not_none(values)
1200+
# GH12824
1201+
first_not_none = next(com.not_none(*values), None)
12081202

1209-
if v is None:
1203+
if first_not_none is None:
12101204
# GH9684. If all values are None, then this will throw an error.
12111205
# We'd prefer it return an empty dataframe.
12121206
return DataFrame()
1213-
elif isinstance(v, DataFrame):
1207+
elif isinstance(first_not_none, DataFrame):
12141208
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
12151209
elif self.grouper.groupings is not None:
12161210
if len(self.grouper.groupings) > 1:
@@ -1227,6 +1221,9 @@ def first_not_none(values):
12271221

12281222
# reorder the values
12291223
values = [values[i] for i in indexer]
1224+
1225+
# update due to the potential reorder
1226+
first_not_none = next(com.not_none(*values), None)
12301227
else:
12311228

12321229
key_index = Index(keys, name=key_names[0])
@@ -1236,20 +1233,19 @@ def first_not_none(values):
12361233
key_index = None
12371234

12381235
# make Nones an empty object
1239-
v = first_not_none(values)
1240-
if v is None:
1236+
if first_not_none is None:
12411237
return DataFrame()
1242-
elif isinstance(v, NDFrame):
1238+
elif isinstance(first_not_none, NDFrame):
12431239

12441240
# this is to silence a DeprecationWarning
12451241
# TODO: Remove when default dtype of empty Series is object
1246-
kwargs = v._construct_axes_dict()
1247-
if v._constructor is Series:
1242+
kwargs = first_not_none._construct_axes_dict()
1243+
if first_not_none._constructor is Series:
12481244
backup = create_series_with_explicit_dtype(
12491245
**kwargs, dtype_if_empty=object
12501246
)
12511247
else:
1252-
backup = v._constructor(**kwargs)
1248+
backup = first_not_none._constructor(**kwargs)
12531249

12541250
values = [x if (x is not None) else backup for x in values]
12551251

0 commit comments

Comments
 (0)