Skip to content

Commit 79ca148

Browse files
authored
CLN: tests.indexing.common (#31812)
1 parent 24d1657 commit 79ca148

File tree

3 files changed

+29
-129
lines changed

3 files changed

+29
-129
lines changed

pandas/tests/indexing/common.py

+10-49
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
""" common utilities """
22
import itertools
3-
from warnings import catch_warnings
43

54
import numpy as np
65

7-
from pandas.core.dtypes.common import is_scalar
8-
96
from pandas import DataFrame, Float64Index, MultiIndex, Series, UInt64Index, date_range
107
import pandas._testing as tm
118

@@ -115,27 +112,6 @@ def generate_indices(self, f, values=False):
115112

116113
return itertools.product(*axes)
117114

118-
def get_result(self, obj, method, key, axis):
119-
""" return the result for this obj with this key and this axis """
120-
121-
if isinstance(key, dict):
122-
key = key[axis]
123-
124-
# use an artificial conversion to map the key as integers to the labels
125-
# so ix can work for comparisons
126-
if method == "indexer":
127-
method = "ix"
128-
key = obj._get_axis(axis)[key]
129-
130-
# in case we actually want 0 index slicing
131-
with catch_warnings(record=True):
132-
try:
133-
xp = getattr(obj, method).__getitem__(_axify(obj, key, axis))
134-
except AttributeError:
135-
xp = getattr(obj, method).__getitem__(key)
136-
137-
return xp
138-
139115
def get_value(self, name, f, i, values=False):
140116
""" return the value for the location i """
141117

@@ -170,45 +146,30 @@ def check_values(self, f, func, values=False):
170146
tm.assert_almost_equal(result, expected)
171147

172148
def check_result(
173-
self, method1, key1, method2, key2, typs=None, axes=None, fails=None,
149+
self, method, key, typs=None, axes=None, fails=None,
174150
):
175-
def _eq(axis, obj, key1, key2):
151+
def _eq(axis, obj, key):
176152
""" compare equal for these 2 keys """
177-
if axis > obj.ndim - 1:
178-
return
179153

154+
axified = _axify(obj, key, axis)
180155
try:
181-
rs = getattr(obj, method1).__getitem__(_axify(obj, key1, axis))
182-
183-
try:
184-
xp = self.get_result(obj=obj, method=method2, key=key2, axis=axis)
185-
except (KeyError, IndexError):
186-
# TODO: why is this allowed?
187-
return
188-
189-
if is_scalar(rs) and is_scalar(xp):
190-
assert rs == xp
191-
else:
192-
tm.assert_equal(rs, xp)
156+
getattr(obj, method).__getitem__(axified)
193157

194158
except (IndexError, TypeError, KeyError) as detail:
195159

196160
# if we are in fails, the ok, otherwise raise it
197161
if fails is not None:
198162
if isinstance(detail, fails):
199-
result = f"ok ({type(detail).__name__})"
200163
return
201-
202-
result = type(detail).__name__
203-
raise AssertionError(result, detail)
164+
raise
204165

205166
if typs is None:
206167
typs = self._typs
207168

208169
if axes is None:
209170
axes = [0, 1]
210-
elif not isinstance(axes, (tuple, list)):
211-
assert isinstance(axes, int)
171+
else:
172+
assert axes in [0, 1]
212173
axes = [axes]
213174

214175
# check
@@ -217,8 +178,8 @@ def _eq(axis, obj, key1, key2):
217178
d = getattr(self, kind)
218179
for ax in axes:
219180
for typ in typs:
220-
if typ not in self._typs:
221-
continue
181+
assert typ in self._typs
222182

223183
obj = d[typ]
224-
_eq(axis=ax, obj=obj, key1=key1, key2=key2)
184+
if ax < obj.ndim:
185+
_eq(axis=ax, obj=obj, key=key)

pandas/tests/indexing/test_iloc.py

-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class TestiLoc(Base):
1818
def test_iloc_getitem_int(self):
1919
# integer
2020
self.check_result(
21-
"iloc",
22-
2,
2321
"iloc",
2422
2,
2523
typs=["labels", "mixed", "ts", "floats", "empty"],
@@ -29,8 +27,6 @@ def test_iloc_getitem_int(self):
2927
def test_iloc_getitem_neg_int(self):
3028
# neg integer
3129
self.check_result(
32-
"iloc",
33-
-1,
3430
"iloc",
3531
-1,
3632
typs=["labels", "mixed", "ts", "floats", "empty"],
@@ -39,8 +35,6 @@ def test_iloc_getitem_neg_int(self):
3935

4036
def test_iloc_getitem_list_int(self):
4137
self.check_result(
42-
"iloc",
43-
[0, 1, 2],
4438
"iloc",
4539
[0, 1, 2],
4640
typs=["labels", "mixed", "ts", "floats", "empty"],

pandas/tests/indexing/test_loc.py

+19-74
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,27 @@ class TestLoc(Base):
1616
def test_loc_getitem_int(self):
1717

1818
# int label
19-
self.check_result("loc", 2, "loc", 2, typs=["label"], fails=KeyError)
19+
self.check_result("loc", 2, typs=["labels"], fails=TypeError)
2020

2121
def test_loc_getitem_label(self):
2222

2323
# label
24-
self.check_result("loc", "c", "loc", "c", typs=["empty"], fails=KeyError)
24+
self.check_result("loc", "c", typs=["empty"], fails=KeyError)
2525

2626
def test_loc_getitem_label_out_of_range(self):
2727

2828
# out of range label
2929
self.check_result(
30-
"loc",
31-
"f",
32-
"loc",
33-
"f",
34-
typs=["ints", "uints", "labels", "mixed", "ts"],
35-
fails=KeyError,
30+
"loc", "f", typs=["ints", "uints", "labels", "mixed", "ts"], fails=KeyError,
3631
)
37-
self.check_result("loc", "f", "ix", "f", typs=["floats"], fails=KeyError)
38-
self.check_result("loc", "f", "loc", "f", typs=["floats"], fails=KeyError)
32+
self.check_result("loc", "f", typs=["floats"], fails=KeyError)
33+
self.check_result("loc", "f", typs=["floats"], fails=KeyError)
3934
self.check_result(
40-
"loc", 20, "loc", 20, typs=["ints", "uints", "mixed"], fails=KeyError,
35+
"loc", 20, typs=["ints", "uints", "mixed"], fails=KeyError,
4136
)
42-
self.check_result("loc", 20, "loc", 20, typs=["labels"], fails=TypeError)
43-
self.check_result("loc", 20, "loc", 20, typs=["ts"], axes=0, fails=TypeError)
44-
self.check_result("loc", 20, "loc", 20, typs=["floats"], axes=0, fails=KeyError)
37+
self.check_result("loc", 20, typs=["labels"], fails=TypeError)
38+
self.check_result("loc", 20, typs=["ts"], axes=0, fails=TypeError)
39+
self.check_result("loc", 20, typs=["floats"], axes=0, fails=KeyError)
4540

4641
def test_loc_getitem_label_list(self):
4742
# TODO: test something here?
@@ -50,49 +45,25 @@ def test_loc_getitem_label_list(self):
5045

5146
def test_loc_getitem_label_list_with_missing(self):
5247
self.check_result(
53-
"loc", [0, 1, 2], "loc", [0, 1, 2], typs=["empty"], fails=KeyError,
48+
"loc", [0, 1, 2], typs=["empty"], fails=KeyError,
5449
)
5550
self.check_result(
56-
"loc",
57-
[0, 2, 10],
58-
"ix",
59-
[0, 2, 10],
60-
typs=["ints", "uints", "floats"],
61-
axes=0,
62-
fails=KeyError,
51+
"loc", [0, 2, 10], typs=["ints", "uints", "floats"], axes=0, fails=KeyError,
6352
)
6453

6554
self.check_result(
66-
"loc",
67-
[3, 6, 7],
68-
"ix",
69-
[3, 6, 7],
70-
typs=["ints", "uints", "floats"],
71-
axes=1,
72-
fails=KeyError,
55+
"loc", [3, 6, 7], typs=["ints", "uints", "floats"], axes=1, fails=KeyError,
7356
)
7457

7558
# GH 17758 - MultiIndex and missing keys
7659
self.check_result(
77-
"loc",
78-
[(1, 3), (1, 4), (2, 5)],
79-
"ix",
80-
[(1, 3), (1, 4), (2, 5)],
81-
typs=["multi"],
82-
axes=0,
83-
fails=KeyError,
60+
"loc", [(1, 3), (1, 4), (2, 5)], typs=["multi"], axes=0, fails=KeyError,
8461
)
8562

8663
def test_loc_getitem_label_list_fails(self):
8764
# fails
8865
self.check_result(
89-
"loc",
90-
[20, 30, 40],
91-
"loc",
92-
[20, 30, 40],
93-
typs=["ints", "uints"],
94-
axes=1,
95-
fails=KeyError,
66+
"loc", [20, 30, 40], typs=["ints", "uints"], axes=1, fails=KeyError,
9667
)
9768

9869
def test_loc_getitem_label_array_like(self):
@@ -104,7 +75,7 @@ def test_loc_getitem_bool(self):
10475
# boolean indexers
10576
b = [True, False, True, False]
10677

107-
self.check_result("loc", b, "loc", b, typs=["empty"], fails=IndexError)
78+
self.check_result("loc", b, typs=["empty"], fails=IndexError)
10879

10980
def test_loc_getitem_label_slice(self):
11081

@@ -115,51 +86,25 @@ def test_loc_getitem_label_slice(self):
11586
# GH 14316
11687

11788
self.check_result(
118-
"loc",
119-
slice(1, 3),
12089
"loc",
12190
slice(1, 3),
12291
typs=["labels", "mixed", "empty", "ts", "floats"],
12392
fails=TypeError,
12493
)
12594

12695
self.check_result(
127-
"loc",
128-
slice("20130102", "20130104"),
129-
"loc",
130-
slice("20130102", "20130104"),
131-
typs=["ts"],
132-
axes=1,
133-
fails=TypeError,
96+
"loc", slice("20130102", "20130104"), typs=["ts"], axes=1, fails=TypeError,
13497
)
13598

13699
self.check_result(
137-
"loc",
138-
slice(2, 8),
139-
"loc",
140-
slice(2, 8),
141-
typs=["mixed"],
142-
axes=0,
143-
fails=TypeError,
100+
"loc", slice(2, 8), typs=["mixed"], axes=0, fails=TypeError,
144101
)
145102
self.check_result(
146-
"loc",
147-
slice(2, 8),
148-
"loc",
149-
slice(2, 8),
150-
typs=["mixed"],
151-
axes=1,
152-
fails=KeyError,
103+
"loc", slice(2, 8), typs=["mixed"], axes=1, fails=KeyError,
153104
)
154105

155106
self.check_result(
156-
"loc",
157-
slice(2, 4, 2),
158-
"loc",
159-
slice(2, 4, 2),
160-
typs=["mixed"],
161-
axes=0,
162-
fails=TypeError,
107+
"loc", slice(2, 4, 2), typs=["mixed"], axes=0, fails=TypeError,
163108
)
164109

165110

0 commit comments

Comments
 (0)