|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +import pandas._testing as tm |
| 6 | +from pandas.api.indexers import check_array_indexer |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize( |
| 10 | + "indexer, expected", |
| 11 | + [ |
| 12 | + # integer |
| 13 | + ([1, 2], np.array([1, 2], dtype=np.intp)), |
| 14 | + (np.array([1, 2], dtype="int64"), np.array([1, 2], dtype=np.intp)), |
| 15 | + (pd.array([1, 2], dtype="Int32"), np.array([1, 2], dtype=np.intp)), |
| 16 | + (pd.Index([1, 2]), np.array([1, 2], dtype=np.intp)), |
| 17 | + # boolean |
| 18 | + ([True, False, True], np.array([True, False, True], dtype=np.bool_)), |
| 19 | + (np.array([True, False, True]), np.array([True, False, True], dtype=np.bool_)), |
| 20 | + ( |
| 21 | + pd.array([True, False, True], dtype="boolean"), |
| 22 | + np.array([True, False, True], dtype=np.bool_), |
| 23 | + ), |
| 24 | + # other |
| 25 | + ([], np.array([], dtype=np.intp)), |
| 26 | + ], |
| 27 | +) |
| 28 | +def test_valid_input(indexer, expected): |
| 29 | + array = np.array([1, 2, 3]) |
| 30 | + result = check_array_indexer(array, indexer) |
| 31 | + tm.assert_numpy_array_equal(result, expected) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + "indexer", [[True, False, None], pd.array([True, False, None], dtype="boolean")], |
| 36 | +) |
| 37 | +def test_bool_raise_missing_values(indexer): |
| 38 | + array = np.array([1, 2, 3]) |
| 39 | + |
| 40 | + msg = "Cannot mask with a boolean indexer containing NA values" |
| 41 | + with pytest.raises(ValueError, match=msg): |
| 42 | + check_array_indexer(array, indexer) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + "indexer", |
| 47 | + [ |
| 48 | + [True, False], |
| 49 | + pd.array([True, False], dtype="boolean"), |
| 50 | + np.array([True, False], dtype=np.bool_), |
| 51 | + ], |
| 52 | +) |
| 53 | +def test_bool_raise_length(indexer): |
| 54 | + array = np.array([1, 2, 3]) |
| 55 | + |
| 56 | + msg = "Boolean index has wrong length" |
| 57 | + with pytest.raises(IndexError, match=msg): |
| 58 | + check_array_indexer(array, indexer) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "indexer", [[0, 1, None], pd.array([0, 1, pd.NA], dtype="Int64")], |
| 63 | +) |
| 64 | +def test_int_raise_missing_values(indexer): |
| 65 | + array = np.array([1, 2, 3]) |
| 66 | + |
| 67 | + msg = "Cannot index with an integer indexer containing NA values" |
| 68 | + with pytest.raises(ValueError, match=msg): |
| 69 | + check_array_indexer(array, indexer) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + "indexer", |
| 74 | + [ |
| 75 | + [0.0, 1.0], |
| 76 | + np.array([1.0, 2.0], dtype="float64"), |
| 77 | + np.array([True, False], dtype=object), |
| 78 | + pd.Index([True, False], dtype=object), |
| 79 | + pd.array(["a", "b"], dtype="string"), |
| 80 | + ], |
| 81 | +) |
| 82 | +def test_raise_invalid_array_dtypes(indexer): |
| 83 | + array = np.array([1, 2, 3]) |
| 84 | + |
| 85 | + msg = "arrays used as indices must be of integer or boolean type" |
| 86 | + with pytest.raises(IndexError, match=msg): |
| 87 | + check_array_indexer(array, indexer) |
0 commit comments