CLN: _wrap_applied_output (#36160) · pandas-dev/pandas@1234234 (original) (raw)
`@@ -1192,113 +1192,104 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
`
1192
1192
`return self.obj._constructor()
`
1193
1193
`elif isinstance(first_not_none, DataFrame):
`
1194
1194
`return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
`
1195
``
`-
else:
`
1196
``
`-
key_index = self.grouper.result_index if self.as_index else None
`
1197
``
-
1198
``
`-
if isinstance(first_not_none, Series):
`
1199
``
`-
this is to silence a DeprecationWarning
`
1200
``
`-
TODO: Remove when default dtype of empty Series is object
`
1201
``
`-
kwargs = first_not_none._construct_axes_dict()
`
1202
``
`-
backup = create_series_with_explicit_dtype(
`
1203
``
`-
dtype_if_empty=object, **kwargs
`
1204
``
`-
)
`
1205
``
-
1206
``
`-
values = [x if (x is not None) else backup for x in values]
`
1207
1195
``
1208
``
`-
v = values[0]
`
1209
``
-
1210
``
`-
if not isinstance(v, (np.ndarray, Index, Series)) and self.as_index:
`
1211
``
`-
values are not series or array-like but scalars
`
1212
``
`-
self._selection_name not passed through to Series as the
`
1213
``
`-
result should not take the name of original selection
`
1214
``
`-
of columns
`
1215
``
`-
return self.obj._constructor_sliced(values, index=key_index)
`
``
1196
`+
key_index = self.grouper.result_index if self.as_index else None
`
``
1197
+
``
1198
`+
if isinstance(first_not_none, Series):
`
``
1199
`+
this is to silence a DeprecationWarning
`
``
1200
`+
TODO: Remove when default dtype of empty Series is object
`
``
1201
`+
kwargs = first_not_none._construct_axes_dict()
`
``
1202
`+
backup = create_series_with_explicit_dtype(dtype_if_empty=object, **kwargs)
`
``
1203
+
``
1204
`+
values = [x if (x is not None) else backup for x in values]
`
``
1205
+
``
1206
`+
v = values[0]
`
``
1207
+
``
1208
`+
if not isinstance(v, (np.ndarray, Index, Series)) and self.as_index:
`
``
1209
`+
values are not series or array-like but scalars
`
``
1210
`+
self._selection_name not passed through to Series as the
`
``
1211
`+
result should not take the name of original selection
`
``
1212
`+
of columns
`
``
1213
`+
return self.obj._constructor_sliced(values, index=key_index)
`
``
1214
+
``
1215
`+
if isinstance(v, Series):
`
``
1216
`+
all_indexed_same = all_indexes_same((x.index for x in values))
`
``
1217
+
``
1218
`+
GH3596
`
``
1219
`+
provide a reduction (Frame -> Series) if groups are
`
``
1220
`+
unique
`
``
1221
`+
if self.squeeze:
`
``
1222
`+
applied_index = self._selected_obj._get_axis(self.axis)
`
``
1223
`+
singular_series = len(values) == 1 and applied_index.nlevels == 1
`
``
1224
+
``
1225
`+
assign the name to this series
`
``
1226
`+
if singular_series:
`
``
1227
`+
values[0].name = keys[0]
`
``
1228
+
``
1229
`+
GH2893
`
``
1230
`+
we have series in the values array, we want to
`
``
1231
`+
produce a series:
`
``
1232
`+
if any of the sub-series are not indexed the same
`
``
1233
`+
OR we don't have a multi-index and we have only a
`
``
1234
`+
single values
`
``
1235
`+
return self._concat_objects(
`
``
1236
`+
keys, values, not_indexed_same=not_indexed_same
`
``
1237
`+
)
`
1216
1238
``
``
1239
`+
still a series
`
``
1240
`+
path added as of GH 5545
`
``
1241
`+
elif all_indexed_same:
`
``
1242
`+
from pandas.core.reshape.concat import concat
`
``
1243
+
``
1244
`+
return concat(values)
`
``
1245
+
``
1246
`+
if not all_indexed_same:
`
``
1247
`+
GH 8467
`
``
1248
`+
return self._concat_objects(keys, values, not_indexed_same=True)
`
``
1249
+
``
1250
`+
Combine values
`
``
1251
`+
vstack+constructor is faster than concat and handles MI-columns
`
``
1252
`+
stacked_values = np.vstack([np.asarray(v) for v in values])
`
``
1253
+
``
1254
`+
if self.axis == 0:
`
``
1255
`+
index = key_index
`
``
1256
`+
columns = v.index.copy()
`
``
1257
`+
if columns.name is None:
`
``
1258
`+
GH6124 - propagate name of Series when it's consistent
`
``
1259
`+
names = {v.name for v in values}
`
``
1260
`+
if len(names) == 1:
`
``
1261
`+
columns.name = list(names)[0]
`
1217
1262
`else:
`
1218
``
`-
if isinstance(v, Series):
`
1219
``
`-
all_indexed_same = all_indexes_same((x.index for x in values))
`
1220
``
-
1221
``
`-
GH3596
`
1222
``
`-
provide a reduction (Frame -> Series) if groups are
`
1223
``
`-
unique
`
1224
``
`-
if self.squeeze:
`
1225
``
`-
applied_index = self._selected_obj._get_axis(self.axis)
`
1226
``
`-
singular_series = (
`
1227
``
`-
len(values) == 1 and applied_index.nlevels == 1
`
1228
``
`-
)
`
1229
``
-
1230
``
`-
assign the name to this series
`
1231
``
`-
if singular_series:
`
1232
``
`-
values[0].name = keys[0]
`
1233
``
-
1234
``
`-
GH2893
`
1235
``
`-
we have series in the values array, we want to
`
1236
``
`-
produce a series:
`
1237
``
`-
if any of the sub-series are not indexed the same
`
1238
``
`-
OR we don't have a multi-index and we have only a
`
1239
``
`-
single values
`
1240
``
`-
return self._concat_objects(
`
1241
``
`-
keys, values, not_indexed_same=not_indexed_same
`
1242
``
`-
)
`
1243
``
-
1244
``
`-
still a series
`
1245
``
`-
path added as of GH 5545
`
1246
``
`-
elif all_indexed_same:
`
1247
``
`-
from pandas.core.reshape.concat import concat
`
1248
``
-
1249
``
`-
return concat(values)
`
1250
``
-
1251
``
`-
if not all_indexed_same:
`
1252
``
`-
GH 8467
`
1253
``
`-
return self._concat_objects(keys, values, not_indexed_same=True)
`
1254
``
-
1255
``
`-
Combine values
`
1256
``
`-
vstack+constructor is faster than concat and handles MI-columns
`
1257
``
`-
stacked_values = np.vstack([np.asarray(v) for v in values])
`
1258
``
-
1259
``
`-
if self.axis == 0:
`
1260
``
`-
index = key_index
`
1261
``
`-
columns = v.index.copy()
`
1262
``
`-
if columns.name is None:
`
1263
``
`-
GH6124 - propagate name of Series when it's consistent
`
1264
``
`-
names = {v.name for v in values}
`
1265
``
`-
if len(names) == 1:
`
1266
``
`-
columns.name = list(names)[0]
`
1267
``
`-
else:
`
1268
``
`-
index = v.index
`
1269
``
`-
columns = key_index
`
1270
``
`-
stacked_values = stacked_values.T
`
1271
``
-
1272
``
`-
result = self.obj._constructor(
`
1273
``
`-
stacked_values, index=index, columns=columns
`
1274
``
`-
)
`
``
1263
`+
index = v.index
`
``
1264
`+
columns = key_index
`
``
1265
`+
stacked_values = stacked_values.T
`
1275
1266
``
1276
``
`-
elif not self.as_index:
`
1277
``
`-
We add grouping column below, so create a frame here
`
1278
``
`-
result = DataFrame(
`
1279
``
`-
values, index=key_index, columns=[self._selection]
`
1280
``
`-
)
`
1281
``
`-
else:
`
1282
``
`-
GH#1738: values is list of arrays of unequal lengths
`
1283
``
`-
fall through to the outer else clause
`
1284
``
`-
TODO: sure this is right? we used to do this
`
1285
``
`-
after raising AttributeError above
`
1286
``
`-
return self.obj._constructor_sliced(
`
1287
``
`-
values, index=key_index, name=self._selection_name
`
1288
``
`-
)
`
``
1267
`+
result = self.obj._constructor(stacked_values, index=index, columns=columns)
`
1289
1268
``
1290
``
`-
if we have date/time like in the original, then coerce dates
`
1291
``
`-
as we are stacking can easily have object dtypes here
`
1292
``
`-
so = self._selected_obj
`
1293
``
`-
if so.ndim == 2 and so.dtypes.apply(needs_i8_conversion).any():
`
1294
``
`-
result = _recast_datetimelike_result(result)
`
1295
``
`-
else:
`
1296
``
`-
result = result._convert(datetime=True)
`
``
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
`+
if we have date/time like in the original, then coerce dates
`
``
1282
`+
as we are stacking can easily have object dtypes here
`
``
1283
`+
so = self._selected_obj
`
``
1284
`+
if so.ndim == 2 and so.dtypes.apply(needs_i8_conversion).any():
`
``
1285
`+
result = _recast_datetimelike_result(result)
`
``
1286
`+
else:
`
``
1287
`+
result = result._convert(datetime=True)
`
1297
1288
``
1298
``
`-
if not self.as_index:
`
1299
``
`-
self._insert_inaxis_grouper_inplace(result)
`
``
1289
`+
if not self.as_index:
`
``
1290
`+
self._insert_inaxis_grouper_inplace(result)
`
1300
1291
``
1301
``
`-
return self._reindex_output(result)
`
``
1292
`+
return self._reindex_output(result)
`
1302
1293
``
1303
1294
`def _transform_general(
`
1304
1295
`self, func, *args, engine="cython", engine_kwargs=None, **kwargs
`