Skip to content

Commit dc5a848

Browse files
peterpanmjjreback
authored andcommitted
TST: add tests to validate margin results for pivot (#25815) (#27245)
thanks @peterpanmj
1 parent fc86f8d commit dc5a848

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pandas/tests/reshape/test_pivot.py

+26
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,32 @@ 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):
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+
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)