Skip to content

BUG: fix IntervalIndex for pivot table raise type error #26765

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 12 commits into from
Jun 13, 2019
Merged
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def contains(cat, key, container):
# can't be in container either.
try:
loc = cat.categories.get_loc(key)
except KeyError:
except (KeyError, TypeError):
return False

# loc is the location of key in categories, but also the *value*
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ def test_pivot_with_non_observable_dropna(self, dropna):

tm.assert_frame_equal(result, expected)

def test_pivot_with_interval_index(self, dropna):
df = pd.DataFrame(
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the issue number as a comment

{'A': pd.Categorical([pd.Interval(0, 1)] * 4),
'B': [1] * 4})
Copy link
Contributor

Choose a reason for hiding this comment

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

does this fully replicate the OP issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just added a fixture to include more scenarios.

result = df.pivot_table(index='A', values='B', dropna=dropna)
expected = pd.DataFrame(
{'B': [1]},
index=pd.Index(pd.Categorical([pd.Interval(0, 1)]),
name='A'))
tm.assert_frame_equal(result, expected)

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