Skip to content

Commit 22499b8

Browse files
committed
Merge branch 'issue-29268' of github.com:CSCD01/pandas-team24 into issue-29268
2 parents 35d9129 + 6817d26 commit 22499b8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pandas/tests/groupby/aggregate/test_aggregate.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,31 @@ def test_mangled(self):
636636
)
637637
tm.assert_frame_equal(result, expected)
638638

639+
def test_agg_multiple_columns(self):
640+
df = pd.DataFrame({"A": [0, 0, 1, 1], "B": [1, 2, 3, 4], "C": [3, 4, 5, 6]})
641+
result = df.groupby("A").agg(
642+
add=(["B", "C"], lambda x: x["B"].max() + x["C"].min()),
643+
minus=(["C", "B"], lambda x: x["B"].max() - x["C"].min())
644+
)
645+
expected = pd.DataFrame(
646+
{"add": [5, 9], "minus": [-1, -1]}, index=pd.Index([0, 1], name="A")
647+
)
648+
tm.assert_frame_equal(result, expected)
649+
650+
def test_agg_multi_missing_column_raises(self):
651+
df = pd.DataFrame({"A": [0, 0, 1, 1], "B": [1, 2, 3, 4], "C": [3, 4, 5, 6]})
652+
with pytest.raises(KeyError, match="Column 'D' does not exist"):
653+
df.groupby("A").agg(
654+
minus=(["D", "C"], lambda x: x["D"].max() - x["C"].min()),
655+
)
656+
657+
def test_agg_multi_missing_key_raises(self):
658+
df = pd.DataFrame({"A": [0, 0, 1, 1], "B": [1, 2, 3, 4], "C": [3, 4, 5, 6]})
659+
with pytest.raises(KeyError, match="D"):
660+
df.groupby("A").agg(
661+
minus=(["B", "C"], lambda x: x["D"].max() - x["D"].min()),
662+
)
663+
639664

640665
@pytest.mark.parametrize(
641666
"agg_col1, agg_col2, agg_col3, agg_result1, agg_result2, agg_result3",

0 commit comments

Comments
 (0)