|
6 | 6 | import pandas._testing as tm
|
7 | 7 |
|
8 | 8 |
|
| 9 | +def _isnan(val): |
| 10 | + try: |
| 11 | + return val is not pd.NA and np.isnan(val) |
| 12 | + except TypeError: |
| 13 | + return False |
| 14 | + |
| 15 | + |
| 16 | +class TestGetLoc: |
| 17 | + def test_get_loc(self, any_string_dtype): |
| 18 | + index = Index(["a", "b", "c"], dtype=any_string_dtype) |
| 19 | + assert index.get_loc("b") == 1 |
| 20 | + |
| 21 | + def test_get_loc_raises(self, any_string_dtype): |
| 22 | + index = Index(["a", "b", "c"], dtype=any_string_dtype) |
| 23 | + with pytest.raises(KeyError, match="d"): |
| 24 | + index.get_loc("d") |
| 25 | + |
| 26 | + def test_get_loc_invalid_value(self, any_string_dtype): |
| 27 | + index = Index(["a", "b", "c"], dtype=any_string_dtype) |
| 28 | + with pytest.raises(KeyError, match="1"): |
| 29 | + index.get_loc(1) |
| 30 | + |
| 31 | + def test_get_loc_non_unique(self, any_string_dtype): |
| 32 | + index = Index(["a", "b", "a"], dtype=any_string_dtype) |
| 33 | + result = index.get_loc("a") |
| 34 | + expected = np.array([True, False, True]) |
| 35 | + tm.assert_numpy_array_equal(result, expected) |
| 36 | + |
| 37 | + def test_get_loc_non_missing(self, any_string_dtype, nulls_fixture): |
| 38 | + index = Index(["a", "b", "c"], dtype=any_string_dtype) |
| 39 | + with pytest.raises(KeyError): |
| 40 | + index.get_loc(nulls_fixture) |
| 41 | + |
| 42 | + def test_get_loc_missing(self, any_string_dtype, nulls_fixture): |
| 43 | + index = Index(["a", "b", nulls_fixture], dtype=any_string_dtype) |
| 44 | + if any_string_dtype == "string" and ( |
| 45 | + (any_string_dtype.na_value is pd.NA and nulls_fixture is not pd.NA) |
| 46 | + or (_isnan(any_string_dtype.na_value) and not _isnan(nulls_fixture)) |
| 47 | + ): |
| 48 | + with pytest.raises(KeyError): |
| 49 | + index.get_loc(nulls_fixture) |
| 50 | + else: |
| 51 | + assert index.get_loc(nulls_fixture) == 2 |
| 52 | + |
| 53 | + |
9 | 54 | class TestGetIndexer:
|
10 | 55 | @pytest.mark.parametrize(
|
11 | 56 | "method,expected",
|
@@ -41,23 +86,60 @@ def test_get_indexer_strings_raises(self, any_string_dtype):
|
41 | 86 | ["a", "b", "c", "d"], method="pad", tolerance=[2, 2, 2, 2]
|
42 | 87 | )
|
43 | 88 |
|
| 89 | + @pytest.mark.parametrize("null", [None, np.nan, float("nan"), pd.NA]) |
| 90 | + def test_get_indexer_missing(self, any_string_dtype, null, using_infer_string): |
| 91 | + # NaT and Decimal("NaN") from null_fixture are not supported for string dtype |
| 92 | + index = Index(["a", "b", null], dtype=any_string_dtype) |
| 93 | + result = index.get_indexer(["a", null, "c"]) |
| 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) |
44 | 103 |
|
45 |
| -class TestGetIndexerNonUnique: |
46 |
| - @pytest.mark.xfail(reason="TODO(infer_string)", strict=False) |
47 |
| - def test_get_indexer_non_unique_nas(self, any_string_dtype, nulls_fixture): |
48 |
| - index = Index(["a", "b", None], dtype=any_string_dtype) |
49 |
| - indexer, missing = index.get_indexer_non_unique([nulls_fixture]) |
| 104 | + tm.assert_numpy_array_equal(result, expected) |
50 | 105 |
|
51 |
| - expected_indexer = np.array([2], dtype=np.intp) |
52 |
| - expected_missing = np.array([], dtype=np.intp) |
| 106 | + |
| 107 | +class TestGetIndexerNonUnique: |
| 108 | + @pytest.mark.parametrize("null", [None, np.nan, float("nan"), pd.NA]) |
| 109 | + def test_get_indexer_non_unique_nas( |
| 110 | + self, any_string_dtype, null, using_infer_string |
| 111 | + ): |
| 112 | + index = Index(["a", "b", null], dtype=any_string_dtype) |
| 113 | + indexer, missing = index.get_indexer_non_unique(["a", null]) |
| 114 | + |
| 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) |
53 | 127 | tm.assert_numpy_array_equal(indexer, expected_indexer)
|
54 | 128 | tm.assert_numpy_array_equal(missing, expected_missing)
|
55 | 129 |
|
56 | 130 | # actually non-unique
|
57 |
| - index = Index(["a", None, "b", None], dtype=any_string_dtype) |
58 |
| - indexer, missing = index.get_indexer_non_unique([nulls_fixture]) |
59 |
| - |
60 |
| - expected_indexer = np.array([1, 3], dtype=np.intp) |
| 131 | + index = Index(["a", null, "b", null], dtype=any_string_dtype) |
| 132 | + indexer, missing = index.get_indexer_non_unique(["a", null]) |
| 133 | + |
| 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) |
61 | 143 | tm.assert_numpy_array_equal(indexer, expected_indexer)
|
62 | 144 | tm.assert_numpy_array_equal(missing, expected_missing)
|
63 | 145 |
|
|
0 commit comments