From 7e6d8e85e2332c88ee2c1b2cccf3491c86e43fe9 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 27 Nov 2019 10:59:22 -0800 Subject: [PATCH] CLN: trim unnecessary code in indexing tests (#29845) --- pandas/tests/indexing/common.py | 70 +++----------- pandas/tests/indexing/test_iloc.py | 30 +----- pandas/tests/indexing/test_loc.py | 148 +++++------------------------ 3 files changed, 39 insertions(+), 209 deletions(-) diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index fea34f795bd03..db6dddfdca11b 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -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)] @@ -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)) @@ -215,50 +192,27 @@ 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)): @@ -266,9 +220,7 @@ def _print(result, error=None): 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: @@ -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) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 7c1d8ddd14317..d826d89f85ef5 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -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", @@ -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", @@ -196,7 +190,6 @@ def test_iloc_getitem_list_int(self): # list of ints self.check_result( - "list int", "iloc", [0, 1, 2], "ix", @@ -204,15 +197,9 @@ def test_iloc_getitem_list_int(self): 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", @@ -224,7 +211,6 @@ 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", @@ -232,7 +218,6 @@ def test_iloc_getitem_list_int(self): typs=["ints", "uints"], ) self.check_result( - "array int", "iloc", np.array([2]), "ix", @@ -240,7 +225,6 @@ def test_iloc_getitem_list_int(self): typs=["ints", "uints"], ) self.check_result( - "array int", "iloc", np.array([0, 1, 2]), "indexer", @@ -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"], ) @@ -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", @@ -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", @@ -343,7 +323,6 @@ def test_iloc_getitem_slice(self): # slices self.check_result( - "slice", "iloc", slice(1, 3), "ix", @@ -351,7 +330,6 @@ def test_iloc_getitem_slice(self): typs=["ints", "uints"], ) self.check_result( - "slice", "iloc", slice(1, 3), "indexer", diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 732914b3b8947..d3af3f6322ef2 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -96,34 +96,23 @@ def test_loc_setitem_slice(self): def test_loc_getitem_int(self): # int label - self.check_result( - "int label", "loc", 2, "ix", 2, typs=["ints", "uints"], axes=0 - ) - self.check_result( - "int label", "loc", 3, "ix", 3, typs=["ints", "uints"], axes=1 - ) - self.check_result( - "int label", "loc", 2, "ix", 2, typs=["label"], fails=KeyError - ) + self.check_result("loc", 2, "ix", 2, typs=["ints", "uints"], axes=0) + self.check_result("loc", 3, "ix", 3, typs=["ints", "uints"], axes=1) + self.check_result("loc", 2, "ix", 2, typs=["label"], fails=KeyError) def test_loc_getitem_label(self): # label - self.check_result("label", "loc", "c", "ix", "c", typs=["labels"], axes=0) - self.check_result("label", "loc", "null", "ix", "null", typs=["mixed"], axes=0) - self.check_result("label", "loc", 8, "ix", 8, typs=["mixed"], axes=0) - self.check_result( - "label", "loc", Timestamp("20130102"), "ix", 1, typs=["ts"], axes=0 - ) - self.check_result( - "label", "loc", "c", "ix", "c", typs=["empty"], fails=KeyError - ) + self.check_result("loc", "c", "ix", "c", typs=["labels"], axes=0) + self.check_result("loc", "null", "ix", "null", typs=["mixed"], axes=0) + self.check_result("loc", 8, "ix", 8, typs=["mixed"], axes=0) + self.check_result("loc", Timestamp("20130102"), "ix", 1, typs=["ts"], axes=0) + self.check_result("loc", "c", "ix", "c", typs=["empty"], fails=KeyError) def test_loc_getitem_label_out_of_range(self): # out of range label self.check_result( - "label range", "loc", "f", "ix", @@ -131,78 +120,33 @@ def test_loc_getitem_label_out_of_range(self): typs=["ints", "uints", "labels", "mixed", "ts"], fails=KeyError, ) + self.check_result("loc", "f", "ix", "f", typs=["floats"], fails=KeyError) self.check_result( - "label range", "loc", "f", "ix", "f", typs=["floats"], fails=KeyError - ) - self.check_result( - "label range", - "loc", - 20, - "ix", - 20, - typs=["ints", "uints", "mixed"], - fails=KeyError, - ) - self.check_result( - "label range", "loc", 20, "ix", 20, typs=["labels"], fails=TypeError - ) - self.check_result( - "label range", "loc", 20, "ix", 20, typs=["ts"], axes=0, fails=TypeError - ) - self.check_result( - "label range", "loc", 20, "ix", 20, typs=["floats"], axes=0, fails=KeyError + "loc", 20, "ix", 20, typs=["ints", "uints", "mixed"], fails=KeyError, ) + self.check_result("loc", 20, "ix", 20, typs=["labels"], fails=TypeError) + self.check_result("loc", 20, "ix", 20, typs=["ts"], axes=0, fails=TypeError) + self.check_result("loc", 20, "ix", 20, typs=["floats"], axes=0, fails=KeyError) def test_loc_getitem_label_list(self): # list of labels self.check_result( - "list lbl", - "loc", - [0, 2, 4], - "ix", - [0, 2, 4], - typs=["ints", "uints"], - axes=0, + "loc", [0, 2, 4], "ix", [0, 2, 4], typs=["ints", "uints"], axes=0, ) self.check_result( - "list lbl", - "loc", - [3, 6, 9], - "ix", - [3, 6, 9], - typs=["ints", "uints"], - axes=1, + "loc", [3, 6, 9], "ix", [3, 6, 9], typs=["ints", "uints"], axes=1, ) self.check_result( - "list lbl", - "loc", - ["a", "b", "d"], - "ix", - ["a", "b", "d"], - typs=["labels"], - axes=0, + "loc", ["a", "b", "d"], "ix", ["a", "b", "d"], typs=["labels"], axes=0, ) self.check_result( - "list lbl", - "loc", - ["A", "B", "C"], - "ix", - ["A", "B", "C"], - typs=["labels"], - axes=1, + "loc", ["A", "B", "C"], "ix", ["A", "B", "C"], typs=["labels"], axes=1, ) self.check_result( - "list lbl", - "loc", - [2, 8, "null"], - "ix", - [2, 8, "null"], - typs=["mixed"], - axes=0, + "loc", [2, 8, "null"], "ix", [2, 8, "null"], typs=["mixed"], axes=0, ) self.check_result( - "list lbl", "loc", [Timestamp("20130102"), Timestamp("20130103")], "ix", @@ -213,17 +157,10 @@ def test_loc_getitem_label_list(self): def test_loc_getitem_label_list_with_missing(self): self.check_result( - "list lbl", - "loc", - [0, 1, 2], - "indexer", - [0, 1, 2], - typs=["empty"], - fails=KeyError, + "loc", [0, 1, 2], "indexer", [0, 1, 2], typs=["empty"], fails=KeyError, ) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): self.check_result( - "list lbl", "loc", [0, 2, 10], "ix", @@ -235,7 +172,6 @@ def test_loc_getitem_label_list_with_missing(self): with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): self.check_result( - "list lbl", "loc", [3, 6, 7], "ix", @@ -248,7 +184,6 @@ def test_loc_getitem_label_list_with_missing(self): # GH 17758 - MultiIndex and missing keys with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): self.check_result( - "list lbl", "loc", [(1, 3), (1, 4), (2, 5)], "ix", @@ -271,7 +206,6 @@ def test_getitem_label_list_with_missing(self): def test_loc_getitem_label_list_fails(self): # fails self.check_result( - "list lbl", "loc", [20, 30, 40], "ix", @@ -284,7 +218,6 @@ def test_loc_getitem_label_list_fails(self): def test_loc_getitem_label_array_like(self): # array like self.check_result( - "array like", "loc", Series(index=[0, 2, 4]).index, "ix", @@ -293,7 +226,6 @@ def test_loc_getitem_label_array_like(self): axes=0, ) self.check_result( - "array like", "loc", Series(index=[3, 6, 9]).index, "ix", @@ -306,14 +238,13 @@ def test_loc_getitem_bool(self): # boolean indexers b = [True, False, True, False] self.check_result( - "bool", "loc", b, "ix", b, typs=["ints", "uints", "labels", "mixed", "ts", "floats"], ) - self.check_result("bool", "loc", b, "ix", b, typs=["empty"], fails=IndexError) + self.check_result("loc", b, "ix", b, typs=["empty"], fails=IndexError) @pytest.mark.parametrize("index", [[True, False], [True, False, True, False]]) def test_loc_getitem_bool_diff_len(self, index): @@ -329,22 +260,10 @@ def test_loc_getitem_int_slice(self): # ok self.check_result( - "int slice2", - "loc", - slice(2, 4), - "ix", - [2, 4], - typs=["ints", "uints"], - axes=0, + "loc", slice(2, 4), "ix", [2, 4], typs=["ints", "uints"], axes=0, ) self.check_result( - "int slice2", - "loc", - slice(3, 6), - "ix", - [3, 6], - typs=["ints", "uints"], - axes=1, + "loc", slice(3, 6), "ix", [3, 6], typs=["ints", "uints"], axes=1, ) def test_loc_to_fail(self): @@ -444,7 +363,6 @@ def test_loc_getitem_label_slice(self): # label slices (with ints) self.check_result( - "lab slice", "loc", slice(1, 3), "ix", @@ -455,26 +373,13 @@ def test_loc_getitem_label_slice(self): # real label slices self.check_result( - "lab slice", - "loc", - slice("a", "c"), - "ix", - slice("a", "c"), - typs=["labels"], - axes=0, + "loc", slice("a", "c"), "ix", slice("a", "c"), typs=["labels"], axes=0, ) self.check_result( - "lab slice", - "loc", - slice("A", "C"), - "ix", - slice("A", "C"), - typs=["labels"], - axes=1, + "loc", slice("A", "C"), "ix", slice("A", "C"), typs=["labels"], axes=1, ) self.check_result( - "ts slice", "loc", slice("20130102", "20130104"), "ix", @@ -483,7 +388,6 @@ def test_loc_getitem_label_slice(self): axes=0, ) self.check_result( - "ts slice", "loc", slice("20130102", "20130104"), "ix", @@ -495,7 +399,6 @@ def test_loc_getitem_label_slice(self): # GH 14316 self.check_result( - "ts slice rev", "loc", slice("20130104", "20130102"), "indexer", @@ -505,7 +408,6 @@ def test_loc_getitem_label_slice(self): ) self.check_result( - "mixed slice", "loc", slice(2, 8), "ix", @@ -515,7 +417,6 @@ def test_loc_getitem_label_slice(self): fails=TypeError, ) self.check_result( - "mixed slice", "loc", slice(2, 8), "ix", @@ -526,7 +427,6 @@ def test_loc_getitem_label_slice(self): ) self.check_result( - "mixed slice", "loc", slice(2, 4, 2), "ix",