Skip to content

CLN: tests.indexing.common #31812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 9, 2020
59 changes: 10 additions & 49 deletions pandas/tests/indexing/common.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 """

Expand Down Expand Up @@ -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
Expand All @@ -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)
6 changes: 0 additions & 6 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
93 changes: 19 additions & 74 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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):
Expand All @@ -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):

Expand All @@ -115,51 +86,25 @@ 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"],
fails=TypeError,
)

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,
)


Expand Down