Skip to content

Commit c78c537

Browse files
authored
TST: Test for groupby transform on categorical column (#35074)
* GH29037 Added test for groupby transform on categorical column * GH29037 - changes for black * GH29037 - moved test to test_categorical
1 parent eb4bbe2 commit c78c537

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

pandas/tests/groupby/test_categorical.py

+50
Original file line numberDiff line numberDiff line change
@@ -1509,3 +1509,53 @@ def test_aggregate_categorical_with_isnan():
15091509
index=index,
15101510
)
15111511
tm.assert_frame_equal(result, expected)
1512+
1513+
1514+
def test_categorical_transform():
1515+
# GH 29037
1516+
df = pd.DataFrame(
1517+
{
1518+
"package_id": [1, 1, 1, 2, 2, 3],
1519+
"status": [
1520+
"Waiting",
1521+
"OnTheWay",
1522+
"Delivered",
1523+
"Waiting",
1524+
"OnTheWay",
1525+
"Waiting",
1526+
],
1527+
}
1528+
)
1529+
1530+
delivery_status_type = pd.CategoricalDtype(
1531+
categories=["Waiting", "OnTheWay", "Delivered"], ordered=True
1532+
)
1533+
df["status"] = df["status"].astype(delivery_status_type)
1534+
df["last_status"] = df.groupby("package_id")["status"].transform(max)
1535+
result = df.copy()
1536+
1537+
expected = pd.DataFrame(
1538+
{
1539+
"package_id": [1, 1, 1, 2, 2, 3],
1540+
"status": [
1541+
"Waiting",
1542+
"OnTheWay",
1543+
"Delivered",
1544+
"Waiting",
1545+
"OnTheWay",
1546+
"Waiting",
1547+
],
1548+
"last_status": [
1549+
"Delivered",
1550+
"Delivered",
1551+
"Delivered",
1552+
"OnTheWay",
1553+
"OnTheWay",
1554+
"Waiting",
1555+
],
1556+
}
1557+
)
1558+
1559+
expected["status"] = expected["status"].astype(delivery_status_type)
1560+
1561+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)