|
24 | 24 | )
|
25 | 25 | import pandas._testing as tm
|
26 | 26 | from pandas.core.api import Float64Index
|
| 27 | +from pandas.core.indexing import IndexingError |
27 | 28 | from pandas.tests.indexing.common import _mklbl
|
28 | 29 | from pandas.tests.indexing.test_floats import gen_obj
|
29 | 30 |
|
@@ -981,3 +982,31 @@ def test_extension_array_cross_section_converts():
|
981 | 982 |
|
982 | 983 | result = df.iloc[0]
|
983 | 984 | tm.assert_series_equal(result, expected)
|
| 985 | + |
| 986 | + |
| 987 | +@pytest.mark.parametrize( |
| 988 | + "ser, keys", |
| 989 | + [(Series([10]), (0, 0)), (Series([1, 2, 3], index=list("abc")), (0, 1))], |
| 990 | +) |
| 991 | +def test_ser_tup_indexer_exceeds_dimensions(ser, keys, indexer_li): |
| 992 | + # GH#13831 |
| 993 | + exp_err, exp_msg = IndexingError, "Too many indexers" |
| 994 | + with pytest.raises(exp_err, match=exp_msg): |
| 995 | + indexer_li(ser)[keys] |
| 996 | + |
| 997 | + if indexer_li == tm.iloc: |
| 998 | + # For iloc.__setitem__ we let numpy handle the error reporting. |
| 999 | + exp_err, exp_msg = IndexError, "too many indices for array" |
| 1000 | + |
| 1001 | + with pytest.raises(exp_err, match=exp_msg): |
| 1002 | + indexer_li(ser)[keys] = 0 |
| 1003 | + |
| 1004 | + |
| 1005 | +def test_ser_list_indexer_exceeds_dimensions(indexer_li): |
| 1006 | + # GH#13831 |
| 1007 | + # Make sure an exception is raised when a tuple exceeds the dimension of the series, |
| 1008 | + # but not list when a list is used. |
| 1009 | + ser = Series([10]) |
| 1010 | + res = indexer_li(ser)[[0, 0]] |
| 1011 | + exp = Series([10, 10], index=Index([0, 0])) |
| 1012 | + tm.assert_series_equal(res, exp) |
0 commit comments