-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
ERR: raise if values passed to Categorical is a DataFrame #18207
Conversation
pandas/core/categorical.py
Outdated
@@ -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 isinstance(values, ABCDataFrame): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of this specific tests, rather check the ndim of the passed values, it must be 1 (or None)
e.g.
getattr(values, 'ndim', None) in [1, None]
pandas/core/categorical.py
Outdated
@@ -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 TypeError("> 1 ndim Categorical are not " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a NotImplementedError
instead of a TypeError
? Similar to how the existing error with this message on line 350 is a NotImplementedError
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call.
Codecov Report
@@ Coverage Diff @@
## master #18207 +/- ##
==========================================
+ Coverage 91.4% 91.4% +<.01%
==========================================
Files 163 163
Lines 50064 50066 +2
==========================================
+ Hits 45759 45763 +4
+ Misses 4305 4303 -2
Continue to review full report at Codecov.
|
1 similar comment
Codecov Report
@@ Coverage Diff @@
## master #18207 +/- ##
==========================================
+ Coverage 91.4% 91.4% +<.01%
==========================================
Files 163 163
Lines 50064 50066 +2
==========================================
+ Hits 45759 45763 +4
+ Misses 4305 4303 -2
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #18207 +/- ##
==========================================
+ Coverage 91.4% 91.4% +<.01%
==========================================
Files 163 163
Lines 50064 50066 +2
==========================================
+ Hits 45759 45763 +4
+ Misses 4305 4303 -2
Continue to review full report at Codecov.
|
def test_constructor_dataframe_error(self): | ||
# GH#17112 | ||
df = pd.DataFrame(np.random.randn(3, 2), columns=['A', 'B']) | ||
with pytest.raises(NotImplementedError): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, will do.
@@ -2331,6 +2334,8 @@ def _factorize_from_iterable(values): | |||
categories=values.categories, | |||
ordered=values.ordered) | |||
codes = values.codes | |||
elif getattr(values, 'ndim', 0) > 1: |
There was a problem hiding this comment.
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
def test_constructor_dataframe_error(self): | ||
# GH#17112 | ||
df = pd.DataFrame(np.random.randn(3, 2), columns=['A', 'B']) | ||
with pytest.raises(NotImplementedError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree here
@@ -173,6 +173,12 @@ def test_constructor_unsortable(self): | |||
pytest.raises( | |||
TypeError, lambda: Categorical(arr, ordered=True)) | |||
|
|||
def test_constructor_dataframe_error(self): |
There was a problem hiding this comment.
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
Don't want to deal with rebase right now. Will return to this bug later. Closing. |
2nd of 3 to address #17112. This raises instead of making the following mistake: