Skip to content

Commit a0b4290

Browse files
topper-123proost
authored andcommitted
clean tests/indexing/common.py (pandas-dev#28904)
1 parent 58e1a5b commit a0b4290

File tree

3 files changed

+38
-51
lines changed

3 files changed

+38
-51
lines changed

pandas/tests/indexing/common.py

+33-46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
""" common utilities """
2-
32
import itertools
43
from warnings import catch_warnings, filterwarnings
54

@@ -29,7 +28,7 @@ def _axify(obj, key, axis):
2928
class Base:
3029
""" indexing comprehensive base class """
3130

32-
_objs = {"series", "frame"}
31+
_kinds = {"series", "frame"}
3332
_typs = {
3433
"ints",
3534
"uints",
@@ -101,13 +100,12 @@ def setup_method(self, method):
101100
self.series_empty = Series()
102101

103102
# form agglomerates
104-
for o in self._objs:
105-
103+
for kind in self._kinds:
106104
d = dict()
107-
for t in self._typs:
108-
d[t] = getattr(self, "{o}_{t}".format(o=o, t=t), None)
105+
for typ in self._typs:
106+
d[typ] = getattr(self, "{kind}_{typ}".format(kind=kind, typ=typ))
109107

110-
setattr(self, o, d)
108+
setattr(self, kind, d)
111109

112110
def generate_indices(self, f, values=False):
113111
""" generate the indices
@@ -117,7 +115,7 @@ def generate_indices(self, f, values=False):
117115

118116
axes = f.axes
119117
if values:
120-
axes = (list(range(len(a))) for a in axes)
118+
axes = (list(range(len(ax))) for ax in axes)
121119

122120
return itertools.product(*axes)
123121

@@ -186,34 +184,34 @@ def check_result(
186184
method2,
187185
key2,
188186
typs=None,
189-
objs=None,
187+
kinds=None,
190188
axes=None,
191189
fails=None,
192190
):
193-
def _eq(t, o, a, obj, k1, k2):
191+
def _eq(typ, kind, axis, obj, key1, key2):
194192
""" compare equal for these 2 keys """
195-
196-
if a is not None and a > obj.ndim - 1:
193+
if axis > obj.ndim - 1:
197194
return
198195

199196
def _print(result, error=None):
200-
if error is not None:
201-
error = str(error)
202-
v = (
197+
err = str(error) if error is not None else ""
198+
msg = (
203199
"%-16.16s [%-16.16s]: [typ->%-8.8s,obj->%-8.8s,"
204200
"key1->(%-4.4s),key2->(%-4.4s),axis->%s] %s"
205-
% (name, result, t, o, method1, method2, a, error or "")
201+
% (name, result, typ, kind, method1, method2, axis, err)
206202
)
207203
if _verbose:
208-
pprint_thing(v)
204+
pprint_thing(msg)
209205

210206
try:
211-
rs = getattr(obj, method1).__getitem__(_axify(obj, k1, a))
207+
rs = getattr(obj, method1).__getitem__(_axify(obj, key1, axis))
212208

213209
with catch_warnings(record=True):
214210
filterwarnings("ignore", "\\n.ix", FutureWarning)
215211
try:
216-
xp = self.get_result(obj, method2, k2, a)
212+
xp = self.get_result(
213+
obj=obj, method=method2, key=key2, axis=axis
214+
)
217215
except (KeyError, IndexError):
218216
# TODO: why is this allowed?
219217
result = "no comp"
@@ -228,8 +226,8 @@ def _print(result, error=None):
228226
else:
229227
tm.assert_equal(rs, xp)
230228
result = "ok"
231-
except AssertionError as e:
232-
detail = str(e)
229+
except AssertionError as exc:
230+
detail = str(exc)
233231
result = "fail"
234232

235233
# reverse the checks
@@ -258,36 +256,25 @@ def _print(result, error=None):
258256
if typs is None:
259257
typs = self._typs
260258

261-
if objs is None:
262-
objs = self._objs
259+
if kinds is None:
260+
kinds = self._kinds
263261

264-
if axes is not None:
265-
if not isinstance(axes, (tuple, list)):
266-
axes = [axes]
267-
else:
268-
axes = list(axes)
269-
else:
262+
if axes is None:
270263
axes = [0, 1]
264+
elif not isinstance(axes, (tuple, list)):
265+
assert isinstance(axes, int)
266+
axes = [axes]
271267

272268
# check
273-
for o in objs:
274-
if o not in self._objs:
269+
for kind in kinds:
270+
if kind not in self._kinds:
275271
continue
276272

277-
d = getattr(self, o)
278-
for a in axes:
279-
for t in typs:
280-
if t not in self._typs:
273+
d = getattr(self, kind)
274+
for ax in axes:
275+
for typ in typs:
276+
if typ not in self._typs:
281277
continue
282278

283-
obj = d[t]
284-
if obj is None:
285-
continue
286-
287-
def _call(obj=obj):
288-
obj = obj.copy()
289-
290-
k2 = key2
291-
_eq(t, o, a, obj, key1, k2)
292-
293-
_call()
279+
obj = d[typ]
280+
_eq(typ=typ, kind=kind, axis=ax, obj=obj, key1=key1, key2=key2)

pandas/tests/indexing/test_iloc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def test_iloc_getitem_dups(self):
284284
[0, 1, 1, 3],
285285
"ix",
286286
{0: [0, 2, 2, 6], 1: [0, 3, 3, 9]},
287-
objs=["series", "frame"],
287+
kinds=["series", "frame"],
288288
typs=["ints", "uints"],
289289
)
290290

pandas/tests/indexing/test_scalar.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def _check(f, func, values=False):
1919
expected = self.get_value(f, i, values)
2020
tm.assert_almost_equal(result, expected)
2121

22-
for o in self._objs:
22+
for kind in self._kinds:
2323

24-
d = getattr(self, o)
24+
d = getattr(self, kind)
2525

2626
# iat
2727
for f in [d["ints"], d["uints"]]:
@@ -47,9 +47,9 @@ def _check(f, func, values=False):
4747
expected = self.get_value(f, i, values)
4848
tm.assert_almost_equal(expected, 1)
4949

50-
for t in self._objs:
50+
for kind in self._kinds:
5151

52-
d = getattr(self, t)
52+
d = getattr(self, kind)
5353

5454
# iat
5555
for f in [d["ints"], d["uints"]]:

0 commit comments

Comments
 (0)