Skip to content

Commit

Permalink
CLN: trim unnecessary code in indexing tests (pandas-dev#29845)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent 4259dc1 commit 9a64d35
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 209 deletions.
70 changes: 11 additions & 59 deletions pandas/tests/indexing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
from pandas import DataFrame, Float64Index, MultiIndex, Series, UInt64Index, date_range
import pandas.util.testing as tm

from pandas.io.formats.printing import pprint_thing

_verbose = False


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

def check_result(
self,
name,
method1,
key1,
method2,
key2,
typs=None,
kinds=None,
axes=None,
fails=None,
self, method1, key1, method2, key2, typs=None, axes=None, fails=None,
):
def _eq(typ, kind, axis, obj, key1, key2):
def _eq(axis, obj, key1, key2):
""" compare equal for these 2 keys """
if axis > obj.ndim - 1:
return

def _print(result, error=None):
err = str(error) if error is not None else ""
msg = (
"%-16.16s [%-16.16s]: [typ->%-8.8s,obj->%-8.8s,"
"key1->(%-4.4s),key2->(%-4.4s),axis->%s] %s"
% (name, result, typ, kind, method1, method2, axis, err)
)
if _verbose:
pprint_thing(msg)

try:
rs = getattr(obj, method1).__getitem__(_axify(obj, key1, axis))

Expand All @@ -215,60 +192,35 @@ def _print(result, error=None):
except (KeyError, IndexError):
# TODO: why is this allowed?
result = "no comp"
_print(result)
return

detail = None

try:
if is_scalar(rs) and is_scalar(xp):
assert rs == xp
else:
tm.assert_equal(rs, xp)
result = "ok"
except AssertionError as exc:
detail = str(exc)
result = "fail"

# reverse the checks
if fails is True:
if result == "fail":
result = "ok (fail)"

_print(result)
if not result.startswith("ok"):
raise AssertionError(detail)

except AssertionError:
raise
if is_scalar(rs) and is_scalar(xp):
assert rs == xp
else:
tm.assert_equal(rs, xp)

except (IndexError, TypeError, KeyError) as detail:

# if we are in fails, the ok, otherwise raise it
if fails is not None:
if isinstance(detail, fails):
result = "ok ({0.__name__})".format(type(detail))
_print(result)
result = f"ok ({type(detail).__name__})"
return

result = type(detail).__name__
raise AssertionError(_print(result, error=detail))
raise AssertionError(result, detail)

if typs is None:
typs = self._typs

if kinds is None:
kinds = self._kinds

if axes is None:
axes = [0, 1]
elif not isinstance(axes, (tuple, list)):
assert isinstance(axes, int)
axes = [axes]

# check
for kind in kinds:
if kind not in self._kinds:
continue
for kind in self._kinds:

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

obj = d[typ]
_eq(typ=typ, kind=kind, axis=ax, obj=obj, key1=key1, key2=key2)
_eq(axis=ax, obj=obj, key1=key1, key2=key2)
30 changes: 4 additions & 26 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,8 @@ def test_iloc_non_integer_raises(self, index, columns, index_vals, column_vals):
def test_iloc_getitem_int(self):

# integer
self.check_result("iloc", 2, "ix", {0: 4, 1: 6, 2: 8}, typs=["ints", "uints"])
self.check_result(
"integer", "iloc", 2, "ix", {0: 4, 1: 6, 2: 8}, typs=["ints", "uints"]
)
self.check_result(
"integer",
"iloc",
2,
"indexer",
Expand All @@ -153,11 +150,8 @@ def test_iloc_getitem_int(self):
def test_iloc_getitem_neg_int(self):

# neg integer
self.check_result("iloc", -1, "ix", {0: 6, 1: 9, 2: 12}, typs=["ints", "uints"])
self.check_result(
"neg int", "iloc", -1, "ix", {0: 6, 1: 9, 2: 12}, typs=["ints", "uints"]
)
self.check_result(
"neg int",
"iloc",
-1,
"indexer",
Expand Down Expand Up @@ -196,23 +190,16 @@ def test_iloc_getitem_list_int(self):

# list of ints
self.check_result(
"list int",
"iloc",
[0, 1, 2],
"ix",
{0: [0, 2, 4], 1: [0, 3, 6], 2: [0, 4, 8]},
typs=["ints", "uints"],
)
self.check_result(
"list int",
"iloc",
[2],
"ix",
{0: [4], 1: [6], 2: [8]},
typs=["ints", "uints"],
"iloc", [2], "ix", {0: [4], 1: [6], 2: [8]}, typs=["ints", "uints"],
)
self.check_result(
"list int",
"iloc",
[0, 1, 2],
"indexer",
Expand All @@ -224,23 +211,20 @@ def test_iloc_getitem_list_int(self):
# array of ints (GH5006), make sure that a single indexer is returning
# the correct type
self.check_result(
"array int",
"iloc",
np.array([0, 1, 2]),
"ix",
{0: [0, 2, 4], 1: [0, 3, 6], 2: [0, 4, 8]},
typs=["ints", "uints"],
)
self.check_result(
"array int",
"iloc",
np.array([2]),
"ix",
{0: [4], 1: [6], 2: [8]},
typs=["ints", "uints"],
)
self.check_result(
"array int",
"iloc",
np.array([0, 1, 2]),
"indexer",
Expand Down Expand Up @@ -279,12 +263,10 @@ def test_iloc_getitem_neg_int_can_reach_first_index(self):
def test_iloc_getitem_dups(self):

self.check_result(
"list int (dups)",
"iloc",
[0, 1, 1, 3],
"ix",
{0: [0, 2, 2, 6], 1: [0, 3, 3, 9]},
kinds=["series", "frame"],
typs=["ints", "uints"],
)

Expand All @@ -306,7 +288,6 @@ def test_iloc_getitem_array(self):
# array like
s = Series(index=range(1, 4))
self.check_result(
"array like",
"iloc",
s.index,
"ix",
Expand All @@ -318,9 +299,8 @@ def test_iloc_getitem_bool(self):

# boolean indexers
b = [True, False, True, False]
self.check_result("bool", "iloc", b, "ix", b, typs=["ints", "uints"])
self.check_result("iloc", b, "ix", b, typs=["ints", "uints"])
self.check_result(
"bool",
"iloc",
b,
"ix",
Expand All @@ -343,15 +323,13 @@ def test_iloc_getitem_slice(self):

# slices
self.check_result(
"slice",
"iloc",
slice(1, 3),
"ix",
{0: [2, 4], 1: [3, 6], 2: [4, 8]},
typs=["ints", "uints"],
)
self.check_result(
"slice",
"iloc",
slice(1, 3),
"indexer",
Expand Down
Loading

0 comments on commit 9a64d35

Please sign in to comment.