Skip to content

BUG: Inconsistent overflow behavior in Series.__setitem__ #53313

Closed
@wirable23

Description

@wirable23

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

>>> df23 = pd.DataFrame({"Col0": pd.Series([-1291861603, None], dtype="int32[pyarrow]"), "Col1": pd.Series([True, True], dtype="bool[pyarrow]"), "Col10": pd.Series([2.193378806488941e-13, None], dtype="float32[pyarrow]")})
>>> rhs = df23["Col0"]
>>> lhs = df23["Col10"]
>>> lhs[df23["Col1"]] = rhs
>>> lhs
0   -1291861632.0
1            <NA>
Name: Col10, dtype: float[pyarrow]
>>>

Issue Description

An unsafe conversion succeeded. The first value of lhs is rounded when pyarrow should do safe conversions by default. Somehow, when I remove the 2nd row of the df, the expected error about an unsafe conversion is raised. I wouldn't expect the presence of a null value to hide an overflow error.

Expected Behavior

df24 = pd.DataFrame({"Col0": pd.Series([-1291861603], dtype="int32[pyarrow]"), "Col1": pd.Series([True], dtype="bool[pyarrow]"), "Col10": pd.Series([2.193378806488941e-13], dtype="float32[pyarrow]")})
df24
Col0 Col1 Col10
0 -1291861603 True 2.193379e-13
rhs = df24["Col0"]
lhs = df24["Col10"]
lhs[df24["Col1"]] = rhs
Traceback (most recent call last):
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\series.py", line 1159, in setitem
self._set_with_engine(key, value)
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\series.py", line 1222, in _set_with_engine
loc = self.index.get_loc(key)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\indexes\range.py", line 350, in get_loc
self._check_indexing_error(key)
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\indexes\base.py", line 5736, in _check_indexing_error
raise InvalidIndexError(key)
pandas.errors.InvalidIndexError: 0 True
Name: Col1, dtype: bool[pyarrow]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 1, in
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\series.py", line 1209, in setitem
self._where(~key, value, inplace=True)
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\generic.py", line 9730, in _where
new_data = self._mgr.putmask(mask=cond, new=other, align=align)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\internals\managers.py", line 403, in putmask
return self.apply(
^^^^^^^^^^^
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\internals\managers.py", line 352, in apply
applied = getattr(b, f)(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\internals\blocks.py", line 1739, in putmask
values._putmask(mask, new)
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\arrays\base.py", line 1528, in _putmask
self[mask] = val
~~~~^^^^^^
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\arrays\arrow\array.py", line 1383, in setitem
data = self._replace_with_mask(self._data, key, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ca2_ps_311_0515\Lib\site-packages\pandas\core\arrays\arrow\array.py", line 1666, in _replace_with_mask
return pa.array(result, type=values.type, from_pandas=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "pyarrow\array.pxi", line 323, in pyarrow.lib.array
File "pyarrow\array.pxi", line 83, in pyarrow.lib._ndarray_to_array
File "pyarrow\error.pxi", line 100, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: Integer value -1291861603 is outside of the range exactly representable by a IEEE 754 single precision value

Installed Versions

INSTALLED VERSIONS

commit : 37ea63d
python : 3.11.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 7, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.13.2
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 12.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arrowpyarrow functionalityBugClosing CandidateMay be closeable, needs more eyeballs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions