@@ -1197,57 +1197,25 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1197
1197
if len (keys ) == 0 :
1198
1198
return self .obj ._constructor (index = keys )
1199
1199
1200
- key_names = self .grouper .names
1201
-
1202
1200
# GH12824
1203
1201
first_not_none = next (com .not_none (* values ), None )
1204
1202
1205
1203
if first_not_none is None :
1206
- # GH9684. If all values are None, then this will throw an error.
1207
- # We'd prefer it return an empty dataframe.
1204
+ # GH9684 - All values are None, return an empty frame.
1208
1205
return self .obj ._constructor ()
1209
1206
elif isinstance (first_not_none , DataFrame ):
1210
1207
return self ._concat_objects (keys , values , not_indexed_same = not_indexed_same )
1211
1208
else :
1212
- if len (self .grouper .groupings ) > 1 :
1213
- key_index = self .grouper .result_index
1214
-
1215
- else :
1216
- ping = self .grouper .groupings [0 ]
1217
- if len (keys ) == ping .ngroups :
1218
- key_index = ping .group_index
1219
- key_index .name = key_names [0 ]
1220
-
1221
- key_lookup = Index (keys )
1222
- indexer = key_lookup .get_indexer (key_index )
1223
-
1224
- # reorder the values
1225
- values = [values [i ] for i in indexer ]
1226
-
1227
- # update due to the potential reorder
1228
- first_not_none = next (com .not_none (* values ), None )
1229
- else :
1230
-
1231
- key_index = Index (keys , name = key_names [0 ])
1232
-
1233
- # don't use the key indexer
1234
- if not self .as_index :
1235
- key_index = None
1209
+ key_index = self .grouper .result_index if self .as_index else None
1236
1210
1237
- # make Nones an empty object
1238
- if first_not_none is None :
1239
- return self .obj ._constructor ()
1240
- elif isinstance (first_not_none , NDFrame ):
1211
+ if isinstance (first_not_none , Series ):
1241
1212
1242
1213
# this is to silence a DeprecationWarning
1243
1214
# TODO: Remove when default dtype of empty Series is object
1244
1215
kwargs = first_not_none ._construct_axes_dict ()
1245
- if isinstance (first_not_none , Series ):
1246
- backup = create_series_with_explicit_dtype (
1247
- ** kwargs , dtype_if_empty = object
1248
- )
1249
- else :
1250
- backup = first_not_none ._constructor (** kwargs )
1216
+ backup = create_series_with_explicit_dtype (
1217
+ ** kwargs , dtype_if_empty = object
1218
+ )
1251
1219
1252
1220
values = [x if (x is not None ) else backup for x in values ]
1253
1221
@@ -1256,7 +1224,7 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1256
1224
if isinstance (v , (np .ndarray , Index , Series )) or not self .as_index :
1257
1225
if isinstance (v , Series ):
1258
1226
applied_index = self ._selected_obj ._get_axis (self .axis )
1259
- all_indexed_same = all_indexes_same ([ x .index for x in values ] )
1227
+ all_indexed_same = all_indexes_same (( x .index for x in values ) )
1260
1228
singular_series = len (values ) == 1 and applied_index .nlevels == 1
1261
1229
1262
1230
# GH3596
@@ -1288,7 +1256,6 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1288
1256
# GH 8467
1289
1257
return self ._concat_objects (keys , values , not_indexed_same = True )
1290
1258
1291
- if self .axis == 0 and isinstance (v , ABCSeries ):
1292
1259
# GH6124 if the list of Series have a consistent name,
1293
1260
# then propagate that name to the result.
1294
1261
index = v .index .copy ()
@@ -1301,34 +1268,27 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1301
1268
if len (names ) == 1 :
1302
1269
index .name = list (names )[0 ]
1303
1270
1304
- # normally use vstack as its faster than concat
1305
- # and if we have mi-columns
1306
- if (
1307
- isinstance (v .index , MultiIndex )
1308
- or key_index is None
1309
- or isinstance (key_index , MultiIndex )
1310
- ):
1311
- stacked_values = np .vstack ([np .asarray (v ) for v in values ])
1312
- result = self .obj ._constructor (
1313
- stacked_values , index = key_index , columns = index
1314
- )
1315
- else :
1316
- # GH5788 instead of stacking; concat gets the
1317
- # dtypes correct
1318
- from pandas .core .reshape .concat import concat
1319
-
1320
- result = concat (
1321
- values ,
1322
- keys = key_index ,
1323
- names = key_index .names ,
1324
- axis = self .axis ,
1325
- ).unstack ()
1326
- result .columns = index
1327
- elif isinstance (v , ABCSeries ):
1271
+ # Combine values
1272
+ # vstack+constructor is faster than concat and handles MI-columns
1328
1273
stacked_values = np .vstack ([np .asarray (v ) for v in values ])
1274
+
1275
+ if self .axis == 0 :
1276
+ index = key_index
1277
+ columns = v .index .copy ()
1278
+ if columns .name is None :
1279
+ # GH6124 - propagate name of Series when it's consistent
1280
+ names = {v .name for v in values }
1281
+ if len (names ) == 1 :
1282
+ columns .name = list (names )[0 ]
1283
+ else :
1284
+ index = v .index
1285
+ columns = key_index
1286
+ stacked_values = stacked_values .T
1287
+
1329
1288
result = self .obj ._constructor (
1330
- stacked_values . T , index = v . index , columns = key_index
1289
+ stacked_values , index = index , columns = columns
1331
1290
)
1291
+
1332
1292
elif not self .as_index :
1333
1293
# We add grouping column below, so create a frame here
1334
1294
result = DataFrame (
0 commit comments