Skip to content

Commit 532860e

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

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

pandas/tests/reshape/test_pivot.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ def dropna(request):
2626
return request.param
2727

2828

29+
@pytest.fixture(params=[True, False])
30+
def ordered(request):
31+
return request.param
32+
33+
2934
@pytest.fixture(params=[([0] * 4, [1] * 4), (range(0, 3), range(1, 4))])
3035
def interval_values(request, closed):
3136
left, right = request.param
32-
return Categorical(pd.IntervalIndex.from_arrays(left, right, closed))
37+
return Categorical(
38+
values=pd.IntervalIndex.from_arrays(left, right, closed),
39+
ordered=ordered)
3340

3441

3542
class TestPivotTable:
@@ -286,6 +293,25 @@ def test_pivot_with_interval_index(self, interval_values, dropna):
286293
expected = DataFrame({"B": 1}, index=Index(interval_values.unique(), name="A"))
287294
tm.assert_frame_equal(result, expected)
288295

296+
def test_pivot_with_interval_index_margins(self, ordered, dropna):
297+
# GH 25815
298+
ordered_cat = pd.IntervalIndex.from_arrays(
299+
[0, 0, 1, 1], [1, 1, 2, 2])
300+
df = pd.DataFrame({
301+
'A': np.arange(4, 0, -1),
302+
'B': ['a', 'b', 'a', 'b'],
303+
'C': pd.Categorical(ordered_cat,
304+
ordered=ordered).sort_values(ascending=False)
305+
})
306+
307+
pivot_tab = pd.pivot_table(data=df, index='C', columns='B', values='A',
308+
aggfunc='sum', margins=True)
309+
310+
result = pivot_tab['All']
311+
expected = pivot_tab.iloc[:, :-1].sum(axis=1)
312+
tm.assert_series_equal(result, expected, check_dtype=False,
313+
check_names=False)
314+
289315
def test_pass_array(self):
290316
result = self.data.pivot_table("D", index=self.data.A, columns=self.data.C)
291317
expected = self.data.pivot_table("D", index="A", columns="C")

0 commit comments

Comments
 (0)