Skip to content

Commit cf7b229

Browse files
committed
Test for setitem/construction
1 parent cd7ec33 commit cf7b229

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
@@ -19,6 +19,8 @@
1919

2020
import numpy as np
2121

22+
from pandas._config import option_context
23+
2224
from pandas._libs import lib
2325
from pandas._libs.json import (
2426
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
@@ -3606,3 +3606,35 @@ def test_ops_with_nan_is_na(using_nan_is_na):
36063606
assert result.isna()[1]
36073607
else:
36083608
assert not result.isna()[1]
3609+
3610+
3611+
def test_setitem_float_nan_is_na(using_nan_is_na):
3612+
# GH#61732
3613+
import pyarrow as pa
3614+
3615+
ser = pd.Series([-1, 0, 1], dtype="int64[pyarrow]")
3616+
3617+
if using_nan_is_na:
3618+
ser[1] = np.nan
3619+
assert ser.isna()[1]
3620+
else:
3621+
msg = "Could not convert nan with type float: tried to convert to int64"
3622+
with pytest.raises(pa.lib.ArrowInvalid, match=msg):
3623+
ser[1] = np.nan
3624+
3625+
ser = pd.Series([-1, np.nan, 1], dtype="float64[pyarrow]")
3626+
if using_nan_is_na:
3627+
assert ser.isna()[1]
3628+
assert ser[1] is pd.NA
3629+
3630+
ser[1] = np.nan
3631+
assert ser[1] is pd.NA
3632+
3633+
else:
3634+
assert not ser.isna()[1]
3635+
assert isinstance(ser[1], float)
3636+
assert np.isnan(ser[1])
3637+
3638+
ser[2] = np.nan
3639+
assert isinstance(ser[2], float)
3640+
assert np.isnan(ser[2])

0 commit comments

Comments
 (0)