Skip to content

Commit c5f4df6

Browse files
committed
TST: add tests to validate margin results for pivot (#25815)
1 parent 2efb607 commit c5f4df6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pandas/tests/reshape/test_pivot.py

+19
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ def test_pivot_with_interval_index(self, interval_values, dropna):
286286
expected = DataFrame({"B": 1}, index=Index(interval_values.unique(), name="A"))
287287
tm.assert_frame_equal(result, expected)
288288

289+
def test_pivot_with_interval_index_margins(self, dropna):
290+
# GH 25815
291+
ordered_cat = pd.IntervalIndex.from_arrays(
292+
[0, 0, 1, 1], [1, 1, 2, 2])
293+
df = pd.DataFrame({
294+
'A': np.arange(4, 0, -1),
295+
'B': ['a', 'b', 'a', 'b'],
296+
'C': pd.Categorical(ordered_cat,
297+
ordered=True).sort_values(ascending=False)
298+
})
299+
300+
pivot_tab = pd.pivot_table(data=df, index='C', columns='B', values='A',
301+
aggfunc='sum', margins=True)
302+
303+
result = pivot_tab['All']
304+
expected = pivot_tab.iloc[:, :-1].sum(axis=1)
305+
tm.assert_series_equal(result, expected, check_dtype=False,
306+
check_names=False)
307+
289308
def test_pass_array(self):
290309
result = self.data.pivot_table("D", index=self.data.A, columns=self.data.C)
291310
expected = self.data.pivot_table("D", index="A", columns="C")

0 commit comments

Comments
 (0)