@@ -1656,27 +1656,35 @@ def test_categorical_margins_category(self, observed):
1656
1656
table = df .pivot_table ("x" , "y" , "z" , dropna = observed , margins = True )
1657
1657
tm .assert_frame_equal (table , expected )
1658
1658
1659
- def test_pivot_with_categorical (self , observed ):
1659
+ def test_pivot_with_categorical (self , observed , ordered_fixture ):
1660
1660
# gh-21370
1661
1661
idx = [np .nan , "low" , "high" , "low" , np .nan ]
1662
1662
col = [np .nan , "A" , "B" , np .nan , "A" ]
1663
1663
df = pd .DataFrame (
1664
1664
{
1665
- "In" : pd .Categorical (idx , categories = ["low" , "high" ], ordered = True ),
1666
- "Col" : pd .Categorical (col , categories = ["A" , "B" ], ordered = True ),
1665
+ "In" : pd .Categorical (
1666
+ idx , categories = ["low" , "high" ], ordered = ordered_fixture
1667
+ ),
1668
+ "Col" : pd .Categorical (
1669
+ col , categories = ["A" , "B" ], ordered = ordered_fixture
1670
+ ),
1667
1671
"Val" : range (1 , 6 ),
1668
1672
}
1669
1673
)
1670
1674
# case with index/columns/value
1671
1675
result = df .pivot_table (index = "In" , columns = "Col" , values = "Val" )
1672
1676
1673
- expected_cols = pd .CategoricalIndex (["A" , "B" ], ordered = True , name = "Col" )
1677
+ expected_cols = pd .CategoricalIndex (
1678
+ ["A" , "B" ], ordered = ordered_fixture , name = "Col"
1679
+ )
1674
1680
1675
1681
expected = pd .DataFrame (
1676
1682
data = [[2.0 , np .nan ], [np .nan , 3.0 ]], columns = expected_cols
1677
1683
)
1678
1684
expected .index = Index (
1679
- pd .Categorical (["low" , "high" ], categories = ["low" , "high" ], ordered = True ),
1685
+ pd .Categorical (
1686
+ ["low" , "high" ], categories = ["low" , "high" ], ordered = ordered_fixture
1687
+ ),
1680
1688
name = "In" ,
1681
1689
)
1682
1690
0 commit comments