Skip to content

Commit a1b1d3d

Browse files
committed
Tests
1 parent e39d8a4 commit a1b1d3d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

pandas/tests/groupby/transform/test_transform.py

+39
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
date_range,
2020
)
2121
import pandas._testing as tm
22+
from pandas.core.groupby import NamedAgg
2223
from pandas.tests.groupby import get_groupby_method_args
2324

2425

@@ -84,6 +85,44 @@ def demean(arr):
8485
tm.assert_frame_equal(result, expected)
8586

8687

88+
def test_transform_with_namedagg():
89+
df = DataFrame({"A": list("aaabbbccc"), "B": range(9), "D": range(9, 18)})
90+
result = df.groupby("A").transform(
91+
b_min=NamedAgg(column="B", aggfunc="min"),
92+
d_sum=NamedAgg(column="D", aggfunc="sum"),
93+
)
94+
expected = DataFrame(
95+
{
96+
"b_min": [0, 0, 0, 3, 3, 3, 6, 6, 6],
97+
"d_sum": [30, 30, 30, 39, 39, 39, 48, 48, 48],
98+
}
99+
)
100+
tm.assert_frame_equal(result, expected)
101+
102+
103+
def test_transform_with_list_like():
104+
df = DataFrame({"col": list("aab"), "val": range(3)})
105+
result = df.groupby("col").transform(["sum", "min"])
106+
expected = DataFrame({"val_sum": [1, 1, 2], "val_min": [0, 0, 2]})
107+
expected.columns = MultiIndex.from_tuples([("val", "sum"), ("val", "min")])
108+
tm.assert_frame_equal(result, expected)
109+
110+
111+
def test_transform_with_duplicate_columns():
112+
df = DataFrame({"A": list("aaabbbccc"), "B": range(9, 18)})
113+
result = df.groupby("A").transform(
114+
b_min=NamedAgg(column="B", aggfunc="min"),
115+
b_max=NamedAgg(column="B", aggfunc="max"),
116+
)
117+
expected = DataFrame(
118+
{
119+
"b_min": [9, 9, 9, 12, 12, 12, 15, 15, 15],
120+
"b_max": [11, 11, 11, 14, 14, 14, 17, 17, 17],
121+
}
122+
)
123+
tm.assert_frame_equal(result, expected)
124+
125+
87126
def test_transform_fast():
88127
df = DataFrame(
89128
{

try.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def process_user(name: str, age: int) -> str:
2+
return f"{name} is {age} years old."
3+
4+
5+
print(
6+
process_user("John", "twenty")
7+
) # mypy will not catch this type error, but Python will run it without complaints

0 commit comments

Comments
 (0)