Skip to content

Commit 09e5bf5

Browse files
committed
Test for setitem/construction
1 parent 6356cc0 commit 09e5bf5

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def _box_pa_scalar(cls, value, pa_type: pa.DataType | None = None) -> pa.Scalar:
452452
"""
453453
if isinstance(value, pa.Scalar):
454454
pa_scalar = value
455-
elif isna(value) and not lib.is_float(value):
455+
elif isna(value) and not (lib.is_float(value) and not is_nan_na()):
456456
pa_scalar = pa.scalar(None, type=pa_type)
457457
else:
458458
# Workaround https://github.com/apache/arrow/issues/37291

pandas/io/json/_json.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import numpy as np
2020

21+
from pandas._config import option_context
22+
2123
from pandas._libs import lib
2224
from pandas._libs.json import (
2325
ujson_dumps,
@@ -994,16 +996,13 @@ def _read_ujson(self) -> DataFrame | Series:
994996
else:
995997
obj = self._get_object_parser(self.data)
996998
if self.dtype_backend is not lib.no_default:
997-
if self.dtype_backend == "pyarrow":
999+
with option_context("mode.nan_is_na", True):
9981000
# The construction above takes "null" to NaN, which we want to
9991001
# convert to NA. But .convert_dtypes to pyarrow doesn't allow
10001002
# that, so we do a 2-step conversion through numpy-nullable.
1001-
obj = obj.convert_dtypes(
1002-
infer_objects=False, dtype_backend="numpy_nullable"
1003+
return obj.convert_dtypes(
1004+
infer_objects=False, dtype_backend=self.dtype_backend
10031005
)
1004-
return obj.convert_dtypes(
1005-
infer_objects=False, dtype_backend=self.dtype_backend
1006-
)
10071006
else:
10081007
return obj
10091008

@@ -1078,16 +1077,13 @@ def __next__(self) -> DataFrame | Series:
10781077
raise ex
10791078

10801079
if self.dtype_backend is not lib.no_default:
1081-
if self.dtype_backend == "pyarrow":
1080+
with option_context("mode.nan_is_na", True):
10821081
# The construction above takes "null" to NaN, which we want to
10831082
# convert to NA. But .convert_dtypes to pyarrow doesn't allow
10841083
# that, so we do a 2-step conversion through numpy-nullable.
1085-
obj = obj.convert_dtypes(
1086-
infer_objects=False, dtype_backend="numpy_nullable"
1084+
return obj.convert_dtypes(
1085+
infer_objects=False, dtype_backend=self.dtype_backend
10871086
)
1088-
return obj.convert_dtypes(
1089-
infer_objects=False, dtype_backend=self.dtype_backend
1090-
)
10911087
else:
10921088
return obj
10931089

pandas/tests/extension/test_arrow.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,3 +3612,35 @@ def test_ops_with_nan_is_na(using_nan_is_na):
36123612
assert result.isna()[1]
36133613
else:
36143614
assert not result.isna()[1]
3615+
3616+
3617+
def test_setitem_float_nan_is_na(using_nan_is_na):
3618+
# GH#61732
3619+
import pyarrow as pa
3620+
3621+
ser = pd.Series([-1, 0, 1], dtype="int64[pyarrow]")
3622+
3623+
if using_nan_is_na:
3624+
ser[1] = np.nan
3625+
assert ser.isna()[1]
3626+
else:
3627+
msg = "Could not convert nan with type float: tried to convert to int64"
3628+
with pytest.raises(pa.lib.ArrowInvalid, match=msg):
3629+
ser[1] = np.nan
3630+
3631+
ser = pd.Series([-1, np.nan, 1], dtype="float64[pyarrow]")
3632+
if using_nan_is_na:
3633+
assert ser.isna()[1]
3634+
assert ser[1] is pd.NA
3635+
3636+
ser[1] = np.nan
3637+
assert ser[1] is pd.NA
3638+
3639+
else:
3640+
assert not ser.isna()[1]
3641+
assert isinstance(ser[1], float)
3642+
assert np.isnan(ser[1])
3643+
3644+
ser[2] = np.nan
3645+
assert isinstance(ser[2], float)
3646+
assert np.isnan(ser[2])

0 commit comments

Comments
 (0)