8000 Fix categorical from codes nan 21767 by miker985 · Pull Request #21775 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Fix categorical from codes nan 21767 #21775

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
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Address stacklevel request from Tom
  • Loading branch information
miker985 committed Jul 30, 2018
commit 69fb0d5914f9c8a7b2feb86fa5daefce5341bf7d
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def from_codes(cls, codes, categories, ordered=False):
err = False
codes = icodes
warn("float codes will be disallowed in the future",
Copy link
Contributor

Choose a reason for hiding this comment

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

Say that in the future this will raise a ValueError.

FutureWarning)
FutureWarning, stacklevel=2)
else:
err = True
if err:
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of using err, you can create the message say around line 635 like
msg = ......

and then just raise ValueError(msg) where needed, much easier to read

Copy link
Contributor Author

Choose a reason for hiding this comment

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

        if not is_integer_dtype(codes):
            msg = "codes need to be array-like integers"
            if is_float_dtype(codes):
                icodes = codes.astype('i8')
                if (icodes == codes).all():
                    err = False
                    codes = icodes
                    warn("float codes will be disallowed in the future",
                         FutureWarning)
                else:
                    raise ValueError(msg)
            else:
                raise ValueError(msg)

is that what you want? That doesn't read easier to me, but I can change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer the msg way to the err way as well.

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def test_from_codes_with_float(self):
codes = [1.0, 2.0, 0] # integer, but in float dtype
categories = ['a', 'b', 'c']

with tm.assert_produces_warning(FutureWarning, ch 7895 eck_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
Categorical.from_codes(codes, categories)
Copy link
Contributor

Choose a reason for hiding this comment

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

actually, can you assert the results of the float converted to integer here (as it just warns but doesn't raise).


codes = [1.1, 2.0, 0] # non-integer
Expand Down
0