Skip to content

Commit c8ca7f7

Browse files
author
Kei
committed
Project the columns before converting to series group by
1 parent 79a8ea6 commit c8ca7f7

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

pandas/core/apply.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -474,18 +474,19 @@ def compute_dict_like(
474474
elif is_groupby:
475475
# key used for column selection and output
476476

477-
df = obj.obj
477+
df = selected_obj
478478
results, keys = [], []
479479
for key, how in func.items():
480-
for index in range(df.shape[1]):
481-
col = df.iloc[:, index]
482-
if col.name != key:
483-
continue
480+
cols = df[key]
481+
482+
for index in range(cols.shape[1]):
483+
col = cols.iloc[:, index]
484484

485485
series = obj._gotitem(key, ndim=1, subset=col)
486486
result = getattr(series, op_name)(how, **kwargs)
487487
results.append(result)
488488
keys.append(key)
489+
489490
else:
490491
results = [
491492
getattr(obj._gotitem(key, ndim=1), op_name)(how, **kwargs)
@@ -525,11 +526,18 @@ def wrap_results_dict_like(
525526
keys_to_use = ktu
526527

527528
axis: AxisInt = 0 if isinstance(obj, ABCSeries) else 1
528-
result = concat(
529-
results,
530-
axis=axis,
531-
keys=keys_to_use,
532-
)
529+
if len(keys_to_use) == 0:
530+
result = concat(
531+
results,
532+
axis=axis,
533+
)
534+
else:
535+
result = concat(
536+
results,
537+
axis=axis,
538+
keys=keys_to_use,
539+
)
540+
533541
elif any(is_ndframe):
534542
# There is a mix of NDFrames and scalars
535543
raise ValueError(

0 commit comments

Comments
 (0)