@@ -286,6 +286,32 @@ def test_pivot_with_interval_index(self, interval_values, dropna):
286
286
expected = DataFrame ({"B" : 1 }, index = Index (interval_values .unique (), name = "A" ))
287
287
tm .assert_frame_equal (result , expected )
288
288
289
+ def test_pivot_with_interval_index_margins (self ):
290
+ # GH 25815
291
+ ordered_cat = pd .IntervalIndex .from_arrays ([0 , 0 , 1 , 1 ], [1 , 1 , 2 , 2 ])
292
+ df = DataFrame (
293
+ {
294
+ "A" : np .arange (4 , 0 , - 1 , dtype = np .intp ),
295
+ "B" : ["a" , "b" , "a" , "b" ],
296
+ "C" : pd .Categorical (ordered_cat , ordered = True ).sort_values (
297
+ ascending = False
298
+ ),
299
+ }
300
+ )
301
+
302
+ pivot_tab = pd .pivot_table (
303
+ df , index = "C" , columns = "B" , values = "A" , aggfunc = "sum" , margins = True
304
+ )
305
+
306
+ result = pivot_tab ["All" ]
307
+ expected = Series (
308
+ [3 , 7 , 10 ],
309
+ index = Index ([pd .Interval (0 , 1 ), pd .Interval (1 , 2 ), "All" ], name = "C" ),
310
+ name = "All" ,
311
+ dtype = np .intp ,
312
+ )
313
+ tm .assert_series_equal (result , expected )
314
+
289
315
def test_pass_array (self ):
290
316
result = self .data .pivot_table ("D" , index = self .data .A , columns = self .data .C )
291
317
expected = self .data .pivot_table ("D" , index = "A" , columns = "C" )
0 commit comments