@@ -1203,16 +1203,27 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1203
1203
1204
1204
values = [x if (x is not None ) else backup for x in values ]
1205
1205
1206
- v = values [0 ]
1207
-
1208
- if not isinstance (v , (np .ndarray , Index , Series )) and self .as_index :
1206
+ if isinstance (first_not_none , (np .ndarray , Index )):
1207
+ # GH#1738: values is list of arrays of unequal lengths
1208
+ # fall through to the outer else clause
1209
+ # TODO: sure this is right? we used to do this
1210
+ # after raising AttributeError above
1211
+ return self .obj ._constructor_sliced (
1212
+ values , index = key_index , name = self ._selection_name
1213
+ )
1214
+ elif not isinstance (first_not_none , Series ):
1209
1215
# values are not series or array-like but scalars
1210
1216
# self._selection_name not passed through to Series as the
1211
1217
# result should not take the name of original selection
1212
1218
# of columns
1213
- return self .obj ._constructor_sliced (values , index = key_index )
1219
+ if self .as_index :
1220
+ return self .obj ._constructor_sliced (values , index = key_index )
1221
+ else :
1222
+ result = DataFrame (values , index = key_index , columns = [self ._selection ])
1223
+ self ._insert_inaxis_grouper_inplace (result )
1224
+ return result
1214
1225
1215
- if isinstance ( v , Series ) :
1226
+ else :
1216
1227
all_indexed_same = all_indexes_same ((x .index for x in values ))
1217
1228
1218
1229
# GH3596
@@ -1253,31 +1264,19 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1253
1264
1254
1265
if self .axis == 0 :
1255
1266
index = key_index
1256
- columns = v .index .copy ()
1267
+ columns = first_not_none .index .copy ()
1257
1268
if columns .name is None :
1258
1269
# GH6124 - propagate name of Series when it's consistent
1259
1270
names = {v .name for v in values }
1260
1271
if len (names ) == 1 :
1261
1272
columns .name = list (names )[0 ]
1262
1273
else :
1263
- index = v .index
1274
+ index = first_not_none .index
1264
1275
columns = key_index
1265
1276
stacked_values = stacked_values .T
1266
1277
1267
1278
result = self .obj ._constructor (stacked_values , index = index , columns = columns )
1268
1279
1269
- elif not self .as_index :
1270
- # We add grouping column below, so create a frame here
1271
- result = DataFrame (values , index = key_index , columns = [self ._selection ])
1272
- else :
1273
- # GH#1738: values is list of arrays of unequal lengths
1274
- # fall through to the outer else clause
1275
- # TODO: sure this is right? we used to do this
1276
- # after raising AttributeError above
1277
- return self .obj ._constructor_sliced (
1278
- values , index = key_index , name = self ._selection_name
1279
- )
1280
-
1281
1280
# if we have date/time like in the original, then coerce dates
1282
1281
# as we are stacking can easily have object dtypes here
1283
1282
so = self ._selected_obj
0 commit comments