@@ -1229,113 +1229,104 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1229
1229
return self .obj ._constructor ()
1230
1230
elif isinstance (first_not_none , DataFrame ):
1231
1231
return self ._concat_objects (keys , values , not_indexed_same = not_indexed_same )
1232
- else :
1233
- key_index = self .grouper .result_index if self .as_index else None
1234
-
1235
- if isinstance (first_not_none , Series ):
1236
- # this is to silence a DeprecationWarning
1237
- # TODO: Remove when default dtype of empty Series is object
1238
- kwargs = first_not_none ._construct_axes_dict ()
1239
- backup = create_series_with_explicit_dtype (
1240
- dtype_if_empty = object , ** kwargs
1241
- )
1242
-
1243
- values = [x if (x is not None ) else backup for x in values ]
1244
1232
1245
- v = values [0 ]
1246
-
1247
- if not isinstance (v , (np .ndarray , Index , Series )) and self .as_index :
1248
- # values are not series or array-like but scalars
1249
- # self._selection_name not passed through to Series as the
1250
- # result should not take the name of original selection
1251
- # of columns
1252
- return self .obj ._constructor_sliced (values , index = key_index )
1233
+ key_index = self .grouper .result_index if self .as_index else None
1234
+
1235
+ if isinstance (first_not_none , Series ):
1236
+ # this is to silence a DeprecationWarning
1237
+ # TODO: Remove when default dtype of empty Series is object
1238
+ kwargs = first_not_none ._construct_axes_dict ()
1239
+ backup = create_series_with_explicit_dtype (dtype_if_empty = object , ** kwargs )
1240
+
1241
+ values = [x if (x is not None ) else backup for x in values ]
1242
+
1243
+ v = values [0 ]
1244
+
1245
+ if not isinstance (v , (np .ndarray , Index , Series )) and self .as_index :
1246
+ # values are not series or array-like but scalars
1247
+ # self._selection_name not passed through to Series as the
1248
+ # result should not take the name of original selection
1249
+ # of columns
1250
+ return self .obj ._constructor_sliced (values , index = key_index )
1251
+
1252
+ if isinstance (v , Series ):
1253
+ all_indexed_same = all_indexes_same ((x .index for x in values ))
1254
+
1255
+ # GH3596
1256
+ # provide a reduction (Frame -> Series) if groups are
1257
+ # unique
1258
+ if self .squeeze :
1259
+ applied_index = self ._selected_obj ._get_axis (self .axis )
1260
+ singular_series = len (values ) == 1 and applied_index .nlevels == 1
1261
+
1262
+ # assign the name to this series
1263
+ if singular_series :
1264
+ values [0 ].name = keys [0 ]
1265
+
1266
+ # GH2893
1267
+ # we have series in the values array, we want to
1268
+ # produce a series:
1269
+ # if any of the sub-series are not indexed the same
1270
+ # OR we don't have a multi-index and we have only a
1271
+ # single values
1272
+ return self ._concat_objects (
1273
+ keys , values , not_indexed_same = not_indexed_same
1274
+ )
1253
1275
1276
+ # still a series
1277
+ # path added as of GH 5545
1278
+ elif all_indexed_same :
1279
+ from pandas .core .reshape .concat import concat
1280
+
1281
+ return concat (values )
1282
+
1283
+ if not all_indexed_same :
1284
+ # GH 8467
1285
+ return self ._concat_objects (keys , values , not_indexed_same = True )
1286
+
1287
+ # Combine values
1288
+ # vstack+constructor is faster than concat and handles MI-columns
1289
+ stacked_values = np .vstack ([np .asarray (v ) for v in values ])
1290
+
1291
+ if self .axis == 0 :
1292
+ index = key_index
1293
+ columns = v .index .copy ()
1294
+ if columns .name is None :
1295
+ # GH6124 - propagate name of Series when it's consistent
1296
+ names = {v .name for v in values }
1297
+ if len (names ) == 1 :
1298
+ columns .name = list (names )[0 ]
1254
1299
else :
1255
- if isinstance (v , Series ):
1256
- all_indexed_same = all_indexes_same ((x .index for x in values ))
1257
-
1258
- # GH3596
1259
- # provide a reduction (Frame -> Series) if groups are
1260
- # unique
1261
- if self .squeeze :
1262
- applied_index = self ._selected_obj ._get_axis (self .axis )
1263
- singular_series = (
1264
- len (values ) == 1 and applied_index .nlevels == 1
1265
- )
1266
-
1267
- # assign the name to this series
1268
- if singular_series :
1269
- values [0 ].name = keys [0 ]
1270
-
1271
- # GH2893
1272
- # we have series in the values array, we want to
1273
- # produce a series:
1274
- # if any of the sub-series are not indexed the same
1275
- # OR we don't have a multi-index and we have only a
1276
- # single values
1277
- return self ._concat_objects (
1278
- keys , values , not_indexed_same = not_indexed_same
1279
- )
1280
-
1281
- # still a series
1282
- # path added as of GH 5545
1283
- elif all_indexed_same :
1284
- from pandas .core .reshape .concat import concat
1285
-
1286
- return concat (values )
1287
-
1288
- if not all_indexed_same :
1289
- # GH 8467
1290
- return self ._concat_objects (keys , values , not_indexed_same = True )
1291
-
1292
- # Combine values
1293
- # vstack+constructor is faster than concat and handles MI-columns
1294
- stacked_values = np .vstack ([np .asarray (v ) for v in values ])
1295
-
1296
- if self .axis == 0 :
1297
- index = key_index
1298
- columns = v .index .copy ()
1299
- if columns .name is None :
1300
- # GH6124 - propagate name of Series when it's consistent
1301
- names = {v .name for v in values }
1302
- if len (names ) == 1 :
1303
- columns .name = list (names )[0 ]
1304
- else :
1305
- index = v .index
1306
- columns = key_index
1307
- stacked_values = stacked_values .T
1308
-
1309
- result = self .obj ._constructor (
1310
- stacked_values , index = index , columns = columns
1311
- )
1300
+ index = v .index
1301
+ columns = key_index
1302
+ stacked_values = stacked_values .T
1312
1303
1313
- elif not self .as_index :
1314
- # We add grouping column below, so create a frame here
1315
- result = DataFrame (
1316
- values , index = key_index , columns = [self ._selection ]
1317
- )
1318
- else :
1319
- # GH#1738: values is list of arrays of unequal lengths
1320
- # fall through to the outer else clause
1321
- # TODO: sure this is right? we used to do this
1322
- # after raising AttributeError above
1323
- return self .obj ._constructor_sliced (
1324
- values , index = key_index , name = self ._selection_name
1325
- )
1304
+ result = self .obj ._constructor (stacked_values , index = index , columns = columns )
1326
1305
1327
- # if we have date/time like in the original, then coerce dates
1328
- # as we are stacking can easily have object dtypes here
1329
- so = self ._selected_obj
1330
- if so .ndim == 2 and so .dtypes .apply (needs_i8_conversion ).any ():
1331
- result = _recast_datetimelike_result (result )
1332
- else :
1333
- result = result ._convert (datetime = True )
1306
+ elif not self .as_index :
1307
+ # We add grouping column below, so create a frame here
1308
+ result = DataFrame (values , index = key_index , columns = [self ._selection ])
1309
+ else :
1310
+ # GH#1738: values is list of arrays of unequal lengths
1311
+ # fall through to the outer else clause
1312
+ # TODO: sure this is right? we used to do this
1313
+ # after raising AttributeError above
1314
+ return self .obj ._constructor_sliced (
1315
+ values , index = key_index , name = self ._selection_name
1316
+ )
1317
+
1318
+ # if we have date/time like in the original, then coerce dates
1319
+ # as we are stacking can easily have object dtypes here
1320
+ so = self ._selected_obj
1321
+ if so .ndim == 2 and so .dtypes .apply (needs_i8_conversion ).any ():
1322
+ result = _recast_datetimelike_result (result )
1323
+ else :
1324
+ result = result ._convert (datetime = True )
1334
1325
1335
- if not self .as_index :
1336
- self ._insert_inaxis_grouper_inplace (result )
1326
+ if not self .as_index :
1327
+ self ._insert_inaxis_grouper_inplace (result )
1337
1328
1338
- return self ._reindex_output (result )
1329
+ return self ._reindex_output (result )
1339
1330
1340
1331
def _transform_general (
1341
1332
self , func , * args , engine = "cython" , engine_kwargs = None , ** kwargs
0 commit comments