Skip to content

BUG: Inputting a masked 64-bit integer array from numpy gets mangled by a hidden float conversion #58173

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
2 of 3 tasks
erykoff opened this issue Apr 6, 2024 · 4 comments
Closed
2 of 3 tasks
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@erykoff
Copy link

erykoff commented Apr 6, 2024

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 numpy as np
import pandas as pd

data = np.array([2**52, 2**52+1, 2**53, 2**53+1, 1649900760361600113, 1649900760361609541, 0])
masked_data = np.ma.masked_array(data, mask=np.zeros(len(data), dtype=np.bool_))

s1 = pd.Series(data, dtype="Int64")
print(s1)
s2 = pd.Series(masked_data, dtype="Int64")
print(s2)

masked_data.mask[-1] = True

s3 = pd.Series(masked_data, dtype="Int64")
print(s3)


### Issue Description

When making a series or dataframe out of a masked 64-bit numpy array then it seems that it first gets converted to a 64-bit float which loses precision.  In the example above the presence of a masked value means that the final converted Series does not match the initial converted Series.  (This also happens when using an astropy MaskedColumn).

### Expected Behavior

Well, it should not change the datatype and round the large integer values.  I expect `np.all(s3 == s1)` to return `True`.

### Installed Versions

<details>

INSTALLED VERSIONS
------------------
commit              : a671b5a8bf5dd13fb19f0e88edc679bc9e15c673
python              : 3.11.7.final.0
python-bits         : 64
OS                  : Darwin
OS-release          : 23.4.0
Version             : Darwin Kernel Version 23.4.0: Fri Mar 15 00:19:22 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8112
machine             : arm64
processor           : arm
byteorder           : little
LC_ALL              : None
LANG                : en_US.UTF-8
LOCALE              : en_US.UTF-8

pandas              : 2.1.4
numpy               : 1.24.4
pytz                : 2023.3.post1
dateutil            : 2.8.2
setuptools          : 69.0.3
pip                 : 23.3.2
Cython              : None
pytest              : 7.4.4
hypothesis          : 6.92.2
sphinx              : 7.2.6
blosc               : None
feather             : None
xlsxwriter          : None
lxml.etree          : 4.9.3
html5lib            : 1.1
pymysql             : None
psycopg2            : 2.9.9
jinja2              : 3.1.2
IPython             : 8.19.0
pandas_datareader   : None
bs4                 : 4.12.2
bottleneck          : 1.3.7
dataframe-api-compat: None
fastparquet         : 2023.10.1
fsspec              : 2023.12.2
gcsfs               : None
matplotlib          : 3.8.2
numba               : 0.58.1
numexpr             : 2.8.8
odfpy               : None
openpyxl            : None
pandas_gbq          : None
pyarrow             : 14.0.2
pyreadstat          : None
pyxlsb              : None
s3fs                : None
scipy               : 1.11.4
sqlalchemy          : 2.0.25
tables              : 3.9.2
tabulate            : 0.9.0
xarray              : 2023.12.0
xlrd                : None
zstandard           : 0.22.0
tzdata              : 2023.4
qtpy                : None
pyqt5               : None

</details>
@erykoff erykoff added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 6, 2024
@dontgoto
Copy link
Contributor

dontgoto commented Apr 7, 2024

This still happens on main and indeed seems to be an issue with the series constructor (the data sanitation in particular) and not with the numpy masked array constructor. I'll take a closer look.

@dontgoto
Copy link
Contributor

dontgoto commented Apr 7, 2024

take

@dontgoto
Copy link
Contributor

dontgoto commented Apr 7, 2024

This is a duplicate of #56566. And the issue seems to go further than that. When fixing your issue here in the array sanitising, Series.to_numpy() of the non-mangled ints fails similarly as the construction of the series prior to the fix. There might be more issues with such large ints and masked arrays in Series/DataFrames beyond that.

@mroeschke
Copy link
Member

Closing as a duplicate of #56566

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants