@@ -65,22 +65,24 @@ def test_pivot_table(self, observed):
65
65
index + [columns ])['D' ].agg (np .mean ).unstack ()
66
66
tm .assert_frame_equal (table , expected )
67
67
68
+ def test_pivot_table_categorical_observed (self , observed ):
69
+ # issue #24923
68
70
df = pd .DataFrame ({'col1' : list ('abcde' ),
69
71
'col2' : list ('fghij' ),
70
72
'col3' : [1 , 2 , 3 , 4 , 5 ]})
71
73
72
74
df .col1 = df .col1 .astype ('category' )
73
75
df .col2 = df .col1 .astype ('category' )
74
76
75
- df_pivot = df .pivot_table (index = 'col1' , values = 'col3' ,
77
+ expected = df .pivot_table (index = 'col1' , values = 'col3' ,
76
78
columns = 'col2' , aggfunc = np .sum ,
77
79
fill_value = 0 )
78
80
79
- df_pivot_observed = df .pivot_table (index = 'col1' , values = 'col3' ,
80
- columns = 'col2' , aggfunc = np .sum ,
81
- fill_value = 0 , observed = observed )
81
+ result = df .pivot_table (index = 'col1' , values = 'col3' ,
82
+ columns = 'col2' , aggfunc = np .sum ,
83
+ fill_value = 0 , observed = observed )
82
84
83
- tm .assert_frame_equal (df_pivot , df_pivot_observed )
85
+ tm .assert_frame_equal (result , expected )
84
86
85
87
def test_pivot_table_nocols (self ):
86
88
df = DataFrame ({'rows' : ['a' , 'b' , 'c' ],
0 commit comments