Skip to content

Commit 8495a98

Browse files
committed
Pusn test up and add observed fixture
1 parent 4cbdf26 commit 8495a98

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

pandas/tests/reshape/test_pivot.py

+35-35
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,41 @@ def test_categorical_margins_category(self, observed):
16561656
table = df.pivot_table("x", "y", "z", dropna=observed, margins=True)
16571657
tm.assert_frame_equal(table, expected)
16581658

1659+
def test_pivot_with_categorical(self, observed):
1660+
# gh-21370
1661+
idx = [np.nan, "low", "high", "low", np.nan]
1662+
col = [np.nan, "A", "B", np.nan, "A"]
1663+
df = pd.DataFrame(
1664+
{
1665+
"In": pd.Categorical(idx, categories=["low", "high"], ordered=True),
1666+
"Col": pd.Categorical(col, categories=["A", "B"], ordered=True),
1667+
"Val": range(1, 6),
1668+
}
1669+
)
1670+
# case with index/columns/value
1671+
result = df.pivot_table(index="In", columns="Col", values="Val")
1672+
1673+
expected_cols = pd.CategoricalIndex(["A", "B"], ordered=True, name="Col")
1674+
1675+
expected = pd.DataFrame(
1676+
data=[[2.0, np.nan], [np.nan, 3.0]], columns=expected_cols
1677+
)
1678+
expected.index = Index(
1679+
pd.Categorical(["low", "high"], categories=["low", "high"], ordered=True),
1680+
name="In",
1681+
)
1682+
1683+
tm.assert_frame_equal(result, expected)
1684+
1685+
# case with columns/value
1686+
result = df.pivot_table(columns="Col", values="Val")
1687+
1688+
expected = pd.DataFrame(
1689+
data=[[3.5, 3.0]], columns=expected_cols, index=Index(["Val"])
1690+
)
1691+
1692+
tm.assert_frame_equal(result, expected)
1693+
16591694
def test_categorical_aggfunc(self, observed):
16601695
# GH 9534
16611696
df = pd.DataFrame(
@@ -2554,38 +2589,3 @@ def test_margin_normalize(self):
25542589
names=["A", "B"],
25552590
)
25562591
tm.assert_frame_equal(result, expected)
2557-
2558-
def test_pivot_with_categorical(self):
2559-
# gh-21370
2560-
idx = [np.nan, "low", "high", "low", np.nan]
2561-
col = [np.nan, "A", "B", np.nan, "A"]
2562-
df = pd.DataFrame(
2563-
{
2564-
"In": pd.Categorical(idx, categories=["low", "high"], ordered=True),
2565-
"Col": pd.Categorical(col, categories=["A", "B"], ordered=True),
2566-
"Val": range(1, 6),
2567-
}
2568-
)
2569-
# case with index/columns/value
2570-
result = df.pivot_table(index="In", columns="Col", values="Val")
2571-
2572-
expected_cols = pd.CategoricalIndex(["A", "B"], ordered=True, name="Col")
2573-
2574-
expected = pd.DataFrame(
2575-
data=[[2.0, np.nan], [np.nan, 3.0]], columns=expected_cols
2576-
)
2577-
expected.index = Index(
2578-
pd.Categorical(["low", "high"], categories=["low", "high"], ordered=True),
2579-
name="In",
2580-
)
2581-
2582-
tm.assert_frame_equal(result, expected)
2583-
2584-
# case with columns/value
2585-
result = df.pivot_table(columns="Col", values="Val")
2586-
2587-
expected = pd.DataFrame(
2588-
data=[[3.5, 3.0]], columns=expected_cols, index=Index(["Val"])
2589-
)
2590-
2591-
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)