Skip to content

Commit c546a51

Browse files
fix for non-infer_string mode
1 parent 43a3edf commit c546a51

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

pandas/tests/indexes/string/test_indexing.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,59 @@ def test_get_indexer_strings_raises(self, any_string_dtype):
8787
)
8888

8989
@pytest.mark.parametrize("null", [None, np.nan, float("nan"), pd.NA])
90-
def test_get_indexer_missing(self, any_string_dtype, null):
90+
def test_get_indexer_missing(self, any_string_dtype, null, using_infer_string):
9191
# NaT and Decimal("NaN") from null_fixture are not supported for string dtype
9292
index = Index(["a", "b", null], dtype=any_string_dtype)
9393
result = index.get_indexer(["a", null, "c"])
94-
expected = np.array([0, 2, -1], dtype=np.intp)
94+
if using_infer_string:
95+
expected = np.array([0, 2, -1], dtype=np.intp)
96+
elif any_string_dtype == "string" and (
97+
(any_string_dtype.na_value is pd.NA and null is not pd.NA)
98+
or (_isnan(any_string_dtype.na_value) and not _isnan(null))
99+
):
100+
expected = np.array([0, -1, -1], dtype=np.intp)
101+
else:
102+
expected = np.array([0, 2, -1], dtype=np.intp)
103+
95104
tm.assert_numpy_array_equal(result, expected)
96105

97106

98107
class TestGetIndexerNonUnique:
99108
@pytest.mark.parametrize("null", [None, np.nan, float("nan"), pd.NA])
100-
def test_get_indexer_non_unique_nas(self, any_string_dtype, null):
109+
def test_get_indexer_non_unique_nas(
110+
self, any_string_dtype, null, using_infer_string
111+
):
101112
index = Index(["a", "b", null], dtype=any_string_dtype)
102113
indexer, missing = index.get_indexer_non_unique(["a", null])
103114

104-
expected_indexer = np.array([0, 2], dtype=np.intp)
105-
expected_missing = np.array([], dtype=np.intp)
115+
if using_infer_string:
116+
expected_indexer = np.array([0, 2], dtype=np.intp)
117+
expected_missing = np.array([], dtype=np.intp)
118+
elif any_string_dtype == "string" and (
119+
(any_string_dtype.na_value is pd.NA and null is not pd.NA)
120+
or (_isnan(any_string_dtype.na_value) and not _isnan(null))
121+
):
122+
expected_indexer = np.array([0, -1], dtype=np.intp)
123+
expected_missing = np.array([1], dtype=np.intp)
124+
else:
125+
expected_indexer = np.array([0, 2], dtype=np.intp)
126+
expected_missing = np.array([], dtype=np.intp)
106127
tm.assert_numpy_array_equal(indexer, expected_indexer)
107128
tm.assert_numpy_array_equal(missing, expected_missing)
108129

109130
# actually non-unique
110131
index = Index(["a", null, "b", null], dtype=any_string_dtype)
111132
indexer, missing = index.get_indexer_non_unique(["a", null])
112133

113-
expected_indexer = np.array([0, 1, 3], dtype=np.intp)
134+
if using_infer_string:
135+
expected_indexer = np.array([0, 1, 3], dtype=np.intp)
136+
elif any_string_dtype == "string" and (
137+
(any_string_dtype.na_value is pd.NA and null is not pd.NA)
138+
or (_isnan(any_string_dtype.na_value) and not _isnan(null))
139+
):
140+
pass
141+
else:
142+
expected_indexer = np.array([0, 1, 3], dtype=np.intp)
114143
tm.assert_numpy_array_equal(indexer, expected_indexer)
115144
tm.assert_numpy_array_equal(missing, expected_missing)
116145

0 commit comments

Comments
 (0)