Skip to content

Commit 7e6d8e8

Browse files
jbrockmendeljreback
authored andcommitted
CLN: trim unnecessary code in indexing tests (#29845)
1 parent d7328d3 commit 7e6d8e8

File tree

3 files changed

+39
-209
lines changed

3 files changed

+39
-209
lines changed

pandas/tests/indexing/common.py

+11-59
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
from pandas import DataFrame, Float64Index, MultiIndex, Series, UInt64Index, date_range
1010
import pandas.util.testing as tm
1111

12-
from pandas.io.formats.printing import pprint_thing
13-
14-
_verbose = False
15-
1612

1713
def _mklbl(prefix, n):
1814
return ["{prefix}{i}".format(prefix=prefix, i=i) for i in range(n)]
@@ -177,32 +173,13 @@ def check_values(self, f, func, values=False):
177173
tm.assert_almost_equal(result, expected)
178174

179175
def check_result(
180-
self,
181-
name,
182-
method1,
183-
key1,
184-
method2,
185-
key2,
186-
typs=None,
187-
kinds=None,
188-
axes=None,
189-
fails=None,
176+
self, method1, key1, method2, key2, typs=None, axes=None, fails=None,
190177
):
191-
def _eq(typ, kind, axis, obj, key1, key2):
178+
def _eq(axis, obj, key1, key2):
192179
""" compare equal for these 2 keys """
193180
if axis > obj.ndim - 1:
194181
return
195182

196-
def _print(result, error=None):
197-
err = str(error) if error is not None else ""
198-
msg = (
199-
"%-16.16s [%-16.16s]: [typ->%-8.8s,obj->%-8.8s,"
200-
"key1->(%-4.4s),key2->(%-4.4s),axis->%s] %s"
201-
% (name, result, typ, kind, method1, method2, axis, err)
202-
)
203-
if _verbose:
204-
pprint_thing(msg)
205-
206183
try:
207184
rs = getattr(obj, method1).__getitem__(_axify(obj, key1, axis))
208185

@@ -215,60 +192,35 @@ def _print(result, error=None):
215192
except (KeyError, IndexError):
216193
# TODO: why is this allowed?
217194
result = "no comp"
218-
_print(result)
219195
return
220196

221-
detail = None
222-
223-
try:
224-
if is_scalar(rs) and is_scalar(xp):
225-
assert rs == xp
226-
else:
227-
tm.assert_equal(rs, xp)
228-
result = "ok"
229-
except AssertionError as exc:
230-
detail = str(exc)
231-
result = "fail"
232-
233-
# reverse the checks
234-
if fails is True:
235-
if result == "fail":
236-
result = "ok (fail)"
237-
238-
_print(result)
239-
if not result.startswith("ok"):
240-
raise AssertionError(detail)
241-
242-
except AssertionError:
243-
raise
197+
if is_scalar(rs) and is_scalar(xp):
198+
assert rs == xp
199+
else:
200+
tm.assert_equal(rs, xp)
201+
244202
except (IndexError, TypeError, KeyError) as detail:
245203

246204
# if we are in fails, the ok, otherwise raise it
247205
if fails is not None:
248206
if isinstance(detail, fails):
249-
result = "ok ({0.__name__})".format(type(detail))
250-
_print(result)
207+
result = f"ok ({type(detail).__name__})"
251208
return
252209

253210
result = type(detail).__name__
254-
raise AssertionError(_print(result, error=detail))
211+
raise AssertionError(result, detail)
255212

256213
if typs is None:
257214
typs = self._typs
258215

259-
if kinds is None:
260-
kinds = self._kinds
261-
262216
if axes is None:
263217
axes = [0, 1]
264218
elif not isinstance(axes, (tuple, list)):
265219
assert isinstance(axes, int)
266220
axes = [axes]
267221

268222
# check
269-
for kind in kinds:
270-
if kind not in self._kinds:
271-
continue
223+
for kind in self._kinds:
272224

273225
d = getattr(self, kind)
274226
for ax in axes:
@@ -277,4 +229,4 @@ def _print(result, error=None):
277229
continue
278230

279231
obj = d[typ]
280-
_eq(typ=typ, kind=kind, axis=ax, obj=obj, key1=key1, key2=key2)
232+
_eq(axis=ax, obj=obj, key1=key1, key2=key2)

pandas/tests/indexing/test_iloc.py

+4-26
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,8 @@ def test_iloc_non_integer_raises(self, index, columns, index_vals, column_vals):
137137
def test_iloc_getitem_int(self):
138138

139139
# integer
140+
self.check_result("iloc", 2, "ix", {0: 4, 1: 6, 2: 8}, typs=["ints", "uints"])
140141
self.check_result(
141-
"integer", "iloc", 2, "ix", {0: 4, 1: 6, 2: 8}, typs=["ints", "uints"]
142-
)
143-
self.check_result(
144-
"integer",
145142
"iloc",
146143
2,
147144
"indexer",
@@ -153,11 +150,8 @@ def test_iloc_getitem_int(self):
153150
def test_iloc_getitem_neg_int(self):
154151

155152
# neg integer
153+
self.check_result("iloc", -1, "ix", {0: 6, 1: 9, 2: 12}, typs=["ints", "uints"])
156154
self.check_result(
157-
"neg int", "iloc", -1, "ix", {0: 6, 1: 9, 2: 12}, typs=["ints", "uints"]
158-
)
159-
self.check_result(
160-
"neg int",
161155
"iloc",
162156
-1,
163157
"indexer",
@@ -196,23 +190,16 @@ def test_iloc_getitem_list_int(self):
196190

197191
# list of ints
198192
self.check_result(
199-
"list int",
200193
"iloc",
201194
[0, 1, 2],
202195
"ix",
203196
{0: [0, 2, 4], 1: [0, 3, 6], 2: [0, 4, 8]},
204197
typs=["ints", "uints"],
205198
)
206199
self.check_result(
207-
"list int",
208-
"iloc",
209-
[2],
210-
"ix",
211-
{0: [4], 1: [6], 2: [8]},
212-
typs=["ints", "uints"],
200+
"iloc", [2], "ix", {0: [4], 1: [6], 2: [8]}, typs=["ints", "uints"],
213201
)
214202
self.check_result(
215-
"list int",
216203
"iloc",
217204
[0, 1, 2],
218205
"indexer",
@@ -224,23 +211,20 @@ def test_iloc_getitem_list_int(self):
224211
# array of ints (GH5006), make sure that a single indexer is returning
225212
# the correct type
226213
self.check_result(
227-
"array int",
228214
"iloc",
229215
np.array([0, 1, 2]),
230216
"ix",
231217
{0: [0, 2, 4], 1: [0, 3, 6], 2: [0, 4, 8]},
232218
typs=["ints", "uints"],
233219
)
234220
self.check_result(
235-
"array int",
236221
"iloc",
237222
np.array([2]),
238223
"ix",
239224
{0: [4], 1: [6], 2: [8]},
240225
typs=["ints", "uints"],
241226
)
242227
self.check_result(
243-
"array int",
244228
"iloc",
245229
np.array([0, 1, 2]),
246230
"indexer",
@@ -279,12 +263,10 @@ def test_iloc_getitem_neg_int_can_reach_first_index(self):
279263
def test_iloc_getitem_dups(self):
280264

281265
self.check_result(
282-
"list int (dups)",
283266
"iloc",
284267
[0, 1, 1, 3],
285268
"ix",
286269
{0: [0, 2, 2, 6], 1: [0, 3, 3, 9]},
287-
kinds=["series", "frame"],
288270
typs=["ints", "uints"],
289271
)
290272

@@ -306,7 +288,6 @@ def test_iloc_getitem_array(self):
306288
# array like
307289
s = Series(index=range(1, 4))
308290
self.check_result(
309-
"array like",
310291
"iloc",
311292
s.index,
312293
"ix",
@@ -318,9 +299,8 @@ def test_iloc_getitem_bool(self):
318299

319300
# boolean indexers
320301
b = [True, False, True, False]
321-
self.check_result("bool", "iloc", b, "ix", b, typs=["ints", "uints"])
302+
self.check_result("iloc", b, "ix", b, typs=["ints", "uints"])
322303
self.check_result(
323-
"bool",
324304
"iloc",
325305
b,
326306
"ix",
@@ -343,15 +323,13 @@ def test_iloc_getitem_slice(self):
343323

344324
# slices
345325
self.check_result(
346-
"slice",
347326
"iloc",
348327
slice(1, 3),
349328
"ix",
350329
{0: [2, 4], 1: [3, 6], 2: [4, 8]},
351330
typs=["ints", "uints"],
352331
)
353332
self.check_result(
354-
"slice",
355333
"iloc",
356334
slice(1, 3),
357335
"indexer",

0 commit comments

Comments
 (0)