diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index 3c027b035c2b8..4804172a22529 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -1,11 +1,8 @@ """ common utilities """ import itertools -from warnings import catch_warnings import numpy as np -from pandas.core.dtypes.common import is_scalar - from pandas import DataFrame, Float64Index, MultiIndex, Series, UInt64Index, date_range import pandas._testing as tm @@ -115,27 +112,6 @@ def generate_indices(self, f, values=False): return itertools.product(*axes) - def get_result(self, obj, method, key, axis): - """ return the result for this obj with this key and this axis """ - - if isinstance(key, dict): - key = key[axis] - - # use an artificial conversion to map the key as integers to the labels - # so ix can work for comparisons - if method == "indexer": - method = "ix" - key = obj._get_axis(axis)[key] - - # in case we actually want 0 index slicing - with catch_warnings(record=True): - try: - xp = getattr(obj, method).__getitem__(_axify(obj, key, axis)) - except AttributeError: - xp = getattr(obj, method).__getitem__(key) - - return xp - def get_value(self, name, f, i, values=False): """ return the value for the location i """ @@ -170,45 +146,30 @@ def check_values(self, f, func, values=False): tm.assert_almost_equal(result, expected) def check_result( - self, method1, key1, method2, key2, typs=None, axes=None, fails=None, + self, method, key, typs=None, axes=None, fails=None, ): - def _eq(axis, obj, key1, key2): + def _eq(axis, obj, key): """ compare equal for these 2 keys """ - if axis > obj.ndim - 1: - return + axified = _axify(obj, key, axis) try: - rs = getattr(obj, method1).__getitem__(_axify(obj, key1, axis)) - - try: - xp = self.get_result(obj=obj, method=method2, key=key2, axis=axis) - except (KeyError, IndexError): - # TODO: why is this allowed? - return - - if is_scalar(rs) and is_scalar(xp): - assert rs == xp - else: - tm.assert_equal(rs, xp) + getattr(obj, method).__getitem__(axified) 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 = f"ok ({type(detail).__name__})" return - - result = type(detail).__name__ - raise AssertionError(result, detail) + raise if typs is None: typs = self._typs if axes is None: axes = [0, 1] - elif not isinstance(axes, (tuple, list)): - assert isinstance(axes, int) + else: + assert axes in [0, 1] axes = [axes] # check @@ -217,8 +178,8 @@ def _eq(axis, obj, key1, key2): d = getattr(self, kind) for ax in axes: for typ in typs: - if typ not in self._typs: - continue + assert typ in self._typs obj = d[typ] - _eq(axis=ax, obj=obj, key1=key1, key2=key2) + if ax < obj.ndim: + _eq(axis=ax, obj=obj, key=key) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 08ea4c1579ef8..bc5ba3d9b03e5 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -18,8 +18,6 @@ class TestiLoc(Base): def test_iloc_getitem_int(self): # integer self.check_result( - "iloc", - 2, "iloc", 2, typs=["labels", "mixed", "ts", "floats", "empty"], @@ -29,8 +27,6 @@ def test_iloc_getitem_int(self): def test_iloc_getitem_neg_int(self): # neg integer self.check_result( - "iloc", - -1, "iloc", -1, typs=["labels", "mixed", "ts", "floats", "empty"], @@ -39,8 +35,6 @@ def test_iloc_getitem_neg_int(self): def test_iloc_getitem_list_int(self): self.check_result( - "iloc", - [0, 1, 2], "iloc", [0, 1, 2], typs=["labels", "mixed", "ts", "floats", "empty"], diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 3a726fb9923ee..02652d993e0f3 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -16,32 +16,27 @@ class TestLoc(Base): def test_loc_getitem_int(self): # int label - self.check_result("loc", 2, "loc", 2, typs=["label"], fails=KeyError) + self.check_result("loc", 2, typs=["labels"], fails=TypeError) def test_loc_getitem_label(self): # label - self.check_result("loc", "c", "loc", "c", typs=["empty"], fails=KeyError) + self.check_result("loc", "c", typs=["empty"], fails=KeyError) def test_loc_getitem_label_out_of_range(self): # out of range label self.check_result( - "loc", - "f", - "loc", - "f", - typs=["ints", "uints", "labels", "mixed", "ts"], - fails=KeyError, + "loc", "f", typs=["ints", "uints", "labels", "mixed", "ts"], fails=KeyError, ) - self.check_result("loc", "f", "ix", "f", typs=["floats"], fails=KeyError) - self.check_result("loc", "f", "loc", "f", typs=["floats"], fails=KeyError) + self.check_result("loc", "f", typs=["floats"], fails=KeyError) + self.check_result("loc", "f", typs=["floats"], fails=KeyError) self.check_result( - "loc", 20, "loc", 20, typs=["ints", "uints", "mixed"], fails=KeyError, + "loc", 20, typs=["ints", "uints", "mixed"], fails=KeyError, ) - self.check_result("loc", 20, "loc", 20, typs=["labels"], fails=TypeError) - self.check_result("loc", 20, "loc", 20, typs=["ts"], axes=0, fails=TypeError) - self.check_result("loc", 20, "loc", 20, typs=["floats"], axes=0, fails=KeyError) + self.check_result("loc", 20, typs=["labels"], fails=TypeError) + self.check_result("loc", 20, typs=["ts"], axes=0, fails=TypeError) + self.check_result("loc", 20, typs=["floats"], axes=0, fails=KeyError) def test_loc_getitem_label_list(self): # TODO: test something here? @@ -50,49 +45,25 @@ def test_loc_getitem_label_list(self): def test_loc_getitem_label_list_with_missing(self): self.check_result( - "loc", [0, 1, 2], "loc", [0, 1, 2], typs=["empty"], fails=KeyError, + "loc", [0, 1, 2], typs=["empty"], fails=KeyError, ) self.check_result( - "loc", - [0, 2, 10], - "ix", - [0, 2, 10], - typs=["ints", "uints", "floats"], - axes=0, - fails=KeyError, + "loc", [0, 2, 10], typs=["ints", "uints", "floats"], axes=0, fails=KeyError, ) self.check_result( - "loc", - [3, 6, 7], - "ix", - [3, 6, 7], - typs=["ints", "uints", "floats"], - axes=1, - fails=KeyError, + "loc", [3, 6, 7], typs=["ints", "uints", "floats"], axes=1, fails=KeyError, ) # GH 17758 - MultiIndex and missing keys self.check_result( - "loc", - [(1, 3), (1, 4), (2, 5)], - "ix", - [(1, 3), (1, 4), (2, 5)], - typs=["multi"], - axes=0, - fails=KeyError, + "loc", [(1, 3), (1, 4), (2, 5)], typs=["multi"], axes=0, fails=KeyError, ) def test_loc_getitem_label_list_fails(self): # fails self.check_result( - "loc", - [20, 30, 40], - "loc", - [20, 30, 40], - typs=["ints", "uints"], - axes=1, - fails=KeyError, + "loc", [20, 30, 40], typs=["ints", "uints"], axes=1, fails=KeyError, ) def test_loc_getitem_label_array_like(self): @@ -104,7 +75,7 @@ def test_loc_getitem_bool(self): # boolean indexers b = [True, False, True, False] - self.check_result("loc", b, "loc", b, typs=["empty"], fails=IndexError) + self.check_result("loc", b, typs=["empty"], fails=IndexError) def test_loc_getitem_label_slice(self): @@ -115,8 +86,6 @@ def test_loc_getitem_label_slice(self): # GH 14316 self.check_result( - "loc", - slice(1, 3), "loc", slice(1, 3), typs=["labels", "mixed", "empty", "ts", "floats"], @@ -124,42 +93,18 @@ def test_loc_getitem_label_slice(self): ) self.check_result( - "loc", - slice("20130102", "20130104"), - "loc", - slice("20130102", "20130104"), - typs=["ts"], - axes=1, - fails=TypeError, + "loc", slice("20130102", "20130104"), typs=["ts"], axes=1, fails=TypeError, ) self.check_result( - "loc", - slice(2, 8), - "loc", - slice(2, 8), - typs=["mixed"], - axes=0, - fails=TypeError, + "loc", slice(2, 8), typs=["mixed"], axes=0, fails=TypeError, ) self.check_result( - "loc", - slice(2, 8), - "loc", - slice(2, 8), - typs=["mixed"], - axes=1, - fails=KeyError, + "loc", slice(2, 8), typs=["mixed"], axes=1, fails=KeyError, ) self.check_result( - "loc", - slice(2, 4, 2), - "loc", - slice(2, 4, 2), - typs=["mixed"], - axes=0, - fails=TypeError, + "loc", slice(2, 4, 2), typs=["mixed"], axes=0, fails=TypeError, )