Skip to content

ERR: raise if values passed to Categorical is a DataFrame #18207

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ def __init__(self, values, categories=None, ordered=None, dtype=None,
elif isinstance(values, (ABCIndexClass, ABCSeries)):
# we'll do inference later
pass
elif getattr(values, 'ndim', 0) > 1:
raise NotImplementedError("> 1 ndim Categorical are not "
"supported at this time")

else:

Expand Down Expand Up @@ -2331,6 +2334,8 @@ def _factorize_from_iterable(values):
categories=values.categories,
ordered=values.ordered)
codes = values.codes
elif getattr(values, 'ndim', 0) > 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

this doesn't get hit in the tests

raise NotImplementedError('Factorizing DataFrame is not supported.')
else:
cat = Categorical(values, ordered=True)
categories = cat.categories
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ def test_constructor_unsortable(self):
pytest.raises(
TypeError, lambda: Categorical(arr, ordered=True))

def test_constructor_dataframe_error(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

paramatrize with a ndarray > 1 dim

# GH#17112
df = pd.DataFrame(np.random.randn(3, 2), columns=['A', 'B'])
with pytest.raises(NotImplementedError):
Copy link
Member

Choose a reason for hiding this comment

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

This is where checking the error message is helpful. AFAICT, your tests don't cover both places where a NotImplementedError can be raised.

Copy link
Contributor

Choose a reason for hiding this comment

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

agree here

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, will do.

Categorical(df)

def test_constructor_interval(self):
result = Categorical([Interval(1, 2), Interval(2, 3), Interval(3, 6)],
ordered=True)
Expand Down