Skip to content

BUG: Coercing bool types to int in qcut #28802

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
2 changes: 1 addition & 1 deletion pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def _coerce_to_type(x):
if dtype is not None:
# GH 19768: force NaT to NaN during integer conversion
if is_bool_dtype(x):
Copy link
Contributor Author

@ryankarlos ryankarlos Oct 5, 2019

Choose a reason for hiding this comment

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

@jreback Not entirely sure where this should go - Adding x.astype(int) under elif is_bool_dtype(x) higher up throws an error in tests with the existing np.where(x.notna(), x.view(np.int64), np.nan) statement if x is ndarray - it passes if x is Series though.
AttributeError: 'numpy.ndarray' object has no attribute 'isnan'

It passes if adding is_bool_dtype(x) with new np.where condition using ~np.isnan(x) like i've done here to account for x being an ndarray

Copy link
Contributor

Choose a reason for hiding this comment

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

so don't use np.isnan, change this to

x = np.where(notna(x), x.astype(np.int64, copy=False), np.nan)

Copy link
Contributor Author

@ryankarlos ryankarlos Oct 5, 2019

Choose a reason for hiding this comment

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

I've gone with doing the integer conversion in the elif block higher up and leaving dtype as None as @jschendel suggested, rather than making any changes here.

x = np.where(x.notna(), x.astype(int), np.nan)
x = np.where(~np.isnan(x), x.astype(int), np.nan)
else:
x = np.where(x.notna(), x.view(np.int64), np.nan)

Expand Down