Skip to content

Commit c376468

Browse files
committed
Fix rank, json tests
1 parent c5f8c28 commit c376468

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

pandas/io/json/_json.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,13 @@ def _read_ujson(self) -> DataFrame | Series:
994994
else:
995995
obj = self._get_object_parser(self.data)
996996
if self.dtype_backend is not lib.no_default:
997+
if self.dtype_backend == "pyarrow":
998+
# The construction above takes "null" to NaN, which we want to
999+
# convert to NA. But .convert_dtypes to pyarrow doesn't allow
1000+
# 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+
)
9971004
return obj.convert_dtypes(
9981005
infer_objects=False, dtype_backend=self.dtype_backend
9991006
)
@@ -1071,6 +1078,13 @@ def __next__(self) -> DataFrame | Series:
10711078
raise ex
10721079

10731080
if self.dtype_backend is not lib.no_default:
1081+
if self.dtype_backend == "pyarrow":
1082+
# The construction above takes "null" to NaN, which we want to
1083+
# convert to NA. But .convert_dtypes to pyarrow doesn't allow
1084+
# that, so we do a 2-step conversion through numpy-nullable.
1085+
obj = obj.convert_dtypes(
1086+
infer_objects=False, dtype_backend="numpy_nullable"
1087+
)
10741088
return obj.convert_dtypes(
10751089
infer_objects=False, dtype_backend=self.dtype_backend
10761090
)

pandas/tests/extension/test_arrow.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,10 @@ def test_map(self, data_missing, na_action):
285285
tm.assert_numpy_array_equal(result, expected)
286286
else:
287287
result = data_missing.map(lambda x: x, na_action=na_action)
288-
if data_missing.dtype == "float32[pyarrow]":
288+
if (
289+
data_missing.dtype == "float32[pyarrow]"
290+
and not using_pyarrow_strict_nans()
291+
):
289292
# map roundtrips through objects, which converts to float64
290293
expected = data_missing.to_numpy(dtype="float64", na_value=np.nan)
291294
else:

pandas/tests/series/methods/test_rank.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,12 @@ def test_rank_signature(self):
270270

271271
def test_rank_tie_methods(self, ser, results, dtype, using_infer_string):
272272
method, exp = results
273-
if dtype == "int64" or (not using_infer_string and dtype == "str"):
273+
if (
274+
dtype == "int64"
275+
or dtype == "int64[pyarrow]"
276+
or dtype == "uint64[pyarrow]"
277+
or (not using_infer_string and dtype == "str")
278+
):
274279
pytest.skip("int64/str does not support NaN")
275280

276281
ser = ser if dtype is None else ser.astype(dtype)
@@ -282,7 +287,15 @@ def test_rank_tie_methods(self, ser, results, dtype, using_infer_string):
282287
exp[np.isnan(ser)] = 9.5
283288
elif method == "dense":
284289
exp[np.isnan(ser)] = 6
285-
tm.assert_series_equal(result, Series(exp, dtype=expected_dtype(dtype, method)))
290+
elif method == "max":
291+
exp[np.isnan(ser)] = 10
292+
elif method == "min":
293+
exp[np.isnan(ser)] = 9
294+
elif method == "first":
295+
exp[np.isnan(ser)] = [9, 10]
296+
297+
expected = Series(exp, dtype=expected_dtype(dtype, method))
298+
tm.assert_series_equal(result, expected)
286299

287300
@pytest.mark.parametrize("na_option", ["top", "bottom", "keep"])
288301
@pytest.mark.parametrize(
@@ -394,8 +407,12 @@ def test_rank_dense_method(self, dtype, ser, exp):
394407

395408
def test_rank_descending(self, ser, results, dtype, using_infer_string):
396409
method, _ = results
397-
if dtype == "int64" or (not using_infer_string and dtype == "str"):
398-
s = ser.dropna()
410+
if (
411+
dtype == "int64"
412+
or dtype == "int64[pyarrow]"
413+
or (not using_infer_string and dtype == "str")
414+
):
415+
s = ser.dropna().astype(dtype)
399416
else:
400417
s = ser.astype(dtype)
401418

0 commit comments

Comments
 (0)