Skip to content

Commit 9858023

Browse files
committed
Fix rank, json tests
1 parent 14582d0 commit 9858023

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
@@ -275,7 +275,12 @@ def test_rank_signature(self):
275275

276276
def test_rank_tie_methods(self, ser, results, dtype, using_infer_string):
277277
method, exp = results
278-
if dtype == "int64" or (not using_infer_string and dtype == "str"):
278+
if (
279+
dtype == "int64"
280+
or dtype == "int64[pyarrow]"
281+
or dtype == "uint64[pyarrow]"
282+
or (not using_infer_string and dtype == "str")
283+
):
279284
pytest.skip("int64/str does not support NaN")
280285

281286
ser = ser if dtype is None else ser.astype(dtype)
@@ -287,7 +292,15 @@ def test_rank_tie_methods(self, ser, results, dtype, using_infer_string):
287292
exp[np.isnan(ser)] = 9.5
288293
elif method == "dense":
289294
exp[np.isnan(ser)] = 6
290-
tm.assert_series_equal(result, Series(exp, dtype=expected_dtype(dtype, method)))
295+
elif method == "max":
296+
exp[np.isnan(ser)] = 10
297+
elif method == "min":
298+
exp[np.isnan(ser)] = 9
299+
elif method == "first":
300+
exp[np.isnan(ser)] = [9, 10]
301+
302+
expected = Series(exp, dtype=expected_dtype(dtype, method))
303+
tm.assert_series_equal(result, expected)
291304

292305
@pytest.mark.parametrize("na_option", ["top", "bottom", "keep"])
293306
@pytest.mark.parametrize(
@@ -406,8 +419,12 @@ def test_rank_dense_method(self, dtype, ser, exp):
406419

407420
def test_rank_descending(self, ser, results, dtype, using_infer_string):
408421
method, _ = results
409-
if dtype == "int64" or (not using_infer_string and dtype == "str"):
410-
s = ser.dropna()
422+
if (
423+
dtype == "int64"
424+
or dtype == "int64[pyarrow]"
425+
or (not using_infer_string and dtype == "str")
426+
):
427+
s = ser.dropna().astype(dtype)
411428
else:
412429
s = ser.astype(dtype)
413430

0 commit comments

Comments
 (0)