Skip to content

TST: add tests to validate margin results for pivot (#25815) #27245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 9, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,25 @@ def test_pivot_with_interval_index(self, interval_values, dropna):
expected = DataFrame({"B": 1}, index=Index(interval_values.unique(), name="A"))
tm.assert_frame_equal(result, expected)

def test_pivot_with_interval_index_margins(self, dropna):
# GH 25815
ordered_cat = pd.IntervalIndex.from_arrays(
[0, 0, 1, 1], [1, 1, 2, 2])
df = pd.DataFrame({
'A': np.arange(4, 0, -1),
'B': ['a', 'b', 'a', 'b'],
'C': pd.Categorical(ordered_cat,
ordered=True).sort_values(ascending=False)
})

pivot_tab = pd.pivot_table(data=df, index='C', columns='B', values='A',
aggfunc='sum', margins=True)

result = pivot_tab['All']
expected = pivot_tab.iloc[:, :-1].sum(axis=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just construct this manually? I think generating the result and expected values from the same variable can let bugs pass through undetected

tm.assert_series_equal(result, expected, check_dtype=False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove check_dtype and check_names?

check_names=False)

def test_pass_array(self):
result = self.data.pivot_table("D", index=self.data.A, columns=self.data.C)
expected = self.data.pivot_table("D", index="A", columns="C")
Expand Down