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