Skip to content
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

BUG: Rounding on BooleanDtype raises an exception #55936

Closed
3 tasks done
JoaquimEsteves opened this issue Nov 13, 2023 · 1 comment · Fixed by #56767
Closed
3 tasks done

BUG: Rounding on BooleanDtype raises an exception #55936

JoaquimEsteves opened this issue Nov 13, 2023 · 1 comment · Fixed by #56767
Labels
Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@JoaquimEsteves
Copy link

JoaquimEsteves commented Nov 13, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd


# Using python's native booleans is A-OK
pd.DataFrame([True]).round(0)
pd.DataFrame([True]).round(4)

pd.DataFrame([True]).astype(pd.BooleanDtype()).round(0)

# TypeError: FloatingArray does not support np.float16 dtype.

pd.DataFrame([True]).astype(pd.BooleanDtype()).round(4)

# numpy.core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'multiply' output from dtype('float64') to dtype('bool') with casting rule 'same_kind'

Issue Description

The type BooleanDtype seems to have a bug when we try to round it.

Internally it looks like the values are stored as a float16 array - this leads to problems when attempting to round those values to 0 as the _maybe_mask_result will attempt to cast the data to a Floating Array. Relevant Line

Note that on an older version of pandas I had lying around (1.4.3) this didn't happen - so it looks to me like a regression.

Expected Behavior

The expected behaviour is that pandas shouldn't crash when attempting to round booleans.

It's noted in a comment in the code that numpy is the one responsible for casting the boolean array into a float16; however the crash only happens with BooleanDtype

Installed Versions

INSTALLED VERSIONS

commit : b2d9ec1
python : 3.10.12.final.0
python-bits : 64
OS : Linux
OS-release : 6.2.0-36-generic
Version : #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct 9 15:34:04 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0.dev0+564.gb2d9ec17c5
numpy : 1.26.2
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@JoaquimEsteves JoaquimEsteves added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 13, 2023
@JoaquimEsteves
Copy link
Author

After debugging a little more.

It looks like round(0) is crashed by Pandas' self._maybe_mask_result[1]
Whereas round(4) is crashed by numpy's np.round [2]

[1]

return FloatingArray(result, mask, copy=False)

[2]
values = np.round(self._data, decimals=decimals, **kwargs)

@mroeschke mroeschke changed the title BUG: Rounding on BooleanDtype crashes the program BUG: Rounding on BooleanDtype raises an exception Nov 13, 2023
@mroeschke mroeschke added NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants