Skip to content

Commit f47c746

Browse files
committed
Fix rank, json tests
1 parent 9d8fef4 commit f47c746

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

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

277282
ser = ser if dtype is None else ser.astype(dtype)
@@ -283,7 +288,15 @@ def test_rank_tie_methods(self, ser, results, dtype, using_infer_string):
283288
exp[np.isnan(ser)] = 9.5
284289
elif method == "dense":
285290
exp[np.isnan(ser)] = 6
286-
tm.assert_series_equal(result, Series(exp, dtype=expected_dtype(dtype, method)))
291+
elif method == "max":
292+
exp[np.isnan(ser)] = 10
293+
elif method == "min":
294+
exp[np.isnan(ser)] = 9
295+
elif method == "first":
296+
exp[np.isnan(ser)] = [9, 10]
297+
298+
expected = Series(exp, dtype=expected_dtype(dtype, method))
299+
tm.assert_series_equal(result, expected)
287300

288301
@pytest.mark.parametrize("na_option", ["top", "bottom", "keep"])
289302
@pytest.mark.parametrize(
@@ -395,8 +408,12 @@ def test_rank_dense_method(self, dtype, ser, exp):
395408

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

0 commit comments

Comments
 (0)