@@ -1197,20 +1197,14 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1197
1197
1198
1198
key_names = self .grouper .names
1199
1199
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 )
1208
1202
1209
- if v is None :
1203
+ if first_not_none is None :
1210
1204
# GH9684. If all values are None, then this will throw an error.
1211
1205
# We'd prefer it return an empty dataframe.
1212
1206
return DataFrame ()
1213
- elif isinstance (v , DataFrame ):
1207
+ elif isinstance (first_not_none , DataFrame ):
1214
1208
return self ._concat_objects (keys , values , not_indexed_same = not_indexed_same )
1215
1209
elif self .grouper .groupings is not None :
1216
1210
if len (self .grouper .groupings ) > 1 :
@@ -1227,6 +1221,9 @@ def first_not_none(values):
1227
1221
1228
1222
# reorder the values
1229
1223
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 )
1230
1227
else :
1231
1228
1232
1229
key_index = Index (keys , name = key_names [0 ])
@@ -1236,20 +1233,19 @@ def first_not_none(values):
1236
1233
key_index = None
1237
1234
1238
1235
# make Nones an empty object
1239
- v = first_not_none (values )
1240
- if v is None :
1236
+ if first_not_none is None :
1241
1237
return DataFrame ()
1242
- elif isinstance (v , NDFrame ):
1238
+ elif isinstance (first_not_none , NDFrame ):
1243
1239
1244
1240
# this is to silence a DeprecationWarning
1245
1241
# 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 :
1248
1244
backup = create_series_with_explicit_dtype (
1249
1245
** kwargs , dtype_if_empty = object
1250
1246
)
1251
1247
else :
1252
- backup = v ._constructor (** kwargs )
1248
+ backup = first_not_none ._constructor (** kwargs )
1253
1249
1254
1250
values = [x if (x is not None ) else backup for x in values ]
1255
1251
0 commit comments