From 56982aff9d1a3ddde97e73f9ff93a020cbc3d225 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 19:23:21 -0800 Subject: [PATCH 1/9] parametrize --- pandas/tests/indexing/test_floats.py | 231 ++++++++++++++------------- pandas/tests/indexing/test_scalar.py | 67 ++++---- 2 files changed, 153 insertions(+), 145 deletions(-) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index 199d9e1013e23..1361818a2932e 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -59,115 +59,117 @@ def test_scalar_error(self, index_func): with pytest.raises(TypeError, match=msg): s.iloc[3.0] = 0 - def test_scalar_non_numeric(self): - - # GH 4892 - # float_indexers should raise exceptions - # on appropriate Index types & accessors - - for index in [ + @pytest.mark.parametrize( + "index_func", + [ tm.makeStringIndex, tm.makeUnicodeIndex, tm.makeCategoricalIndex, tm.makeDateIndex, tm.makeTimedeltaIndex, tm.makePeriodIndex, - ]: + ], + ) + def test_scalar_non_numeric(self, index_func): - i = index(5) + # GH 4892 + # float_indexers should raise exceptions + # on appropriate Index types & accessors - for s in [ - Series(np.arange(len(i)), index=i), - DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i), - ]: + i = index_func(5) - # getting - for idxr, getitem in [(lambda x: x.iloc, False), (lambda x: x, True)]: + for s in [ + Series(np.arange(len(i)), index=i), + DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i), + ]: - # gettitem on a DataFrame is a KeyError as it is indexing - # via labels on the columns - if getitem and isinstance(s, DataFrame): - error = KeyError - msg = r"^3(\.0)?$" - else: - error = TypeError - msg = ( - r"cannot do (label|index|positional) indexing " - r"on {klass} with these indexers \[3\.0\] of " - r"{kind}|" - "Cannot index by location index with a " - "non-integer key".format(klass=type(i), kind=str(float)) - ) - with pytest.raises(error, match=msg): - idxr(s)[3.0] - - # label based can be a TypeError or KeyError - if s.index.inferred_type in { - "categorical", - "string", - "unicode", - "mixed", - }: + # getting + for idxr, getitem in [(lambda x: x.iloc, False), (lambda x: x, True)]: + + # gettitem on a DataFrame is a KeyError as it is indexing + # via labels on the columns + if getitem and isinstance(s, DataFrame): error = KeyError - msg = r"^3\.0$" + msg = r"^3(\.0)?$" else: error = TypeError msg = ( - r"cannot do (label|index) indexing " + r"cannot do (label|index|positional) indexing " r"on {klass} with these indexers \[3\.0\] of " - r"{kind}".format(klass=type(i), kind=str(float)) + r"{kind}|" + "Cannot index by location index with a " + "non-integer key".format(klass=type(i), kind=str(float)) ) with pytest.raises(error, match=msg): - s.loc[3.0] - - # contains - assert 3.0 not in s - - # setting with a float fails with iloc + idxr(s)[3.0] + + # label based can be a TypeError or KeyError + if s.index.inferred_type in { + "categorical", + "string", + "unicode", + "mixed", + }: + error = KeyError + msg = r"^3\.0$" + else: + error = TypeError msg = ( - r"cannot do (label|index|positional) indexing " + r"cannot do (label|index) indexing " r"on {klass} with these indexers \[3\.0\] of " r"{kind}".format(klass=type(i), kind=str(float)) ) - with pytest.raises(TypeError, match=msg): - s.iloc[3.0] = 0 - - # setting with an indexer - if s.index.inferred_type in ["categorical"]: - # Value or Type Error - pass - elif s.index.inferred_type in ["datetime64", "timedelta64", "period"]: - - # these should prob work - # and are inconsistent between series/dataframe ATM - # for idxr in [lambda x: x]: - # s2 = s.copy() - # - # with pytest.raises(TypeError): - # idxr(s2)[3.0] = 0 - pass - - else: - - s2 = s.copy() - s2.loc[3.0] = 10 - assert s2.index.is_object() + with pytest.raises(error, match=msg): + s.loc[3.0] - for idxr in [lambda x: x]: - s2 = s.copy() - idxr(s2)[3.0] = 0 - assert s2.index.is_object() + # contains + assert 3.0 not in s - # fallsback to position selection, series only - s = Series(np.arange(len(i)), index=i) - s[3] + # setting with a float fails with iloc msg = ( - r"cannot do (label|index) indexing " + r"cannot do (label|index|positional) indexing " r"on {klass} with these indexers \[3\.0\] of " r"{kind}".format(klass=type(i), kind=str(float)) ) with pytest.raises(TypeError, match=msg): - s[3.0] + s.iloc[3.0] = 0 + + # setting with an indexer + if s.index.inferred_type in ["categorical"]: + # Value or Type Error + pass + elif s.index.inferred_type in ["datetime64", "timedelta64", "period"]: + + # these should prob work + # and are inconsistent between series/dataframe ATM + # for idxr in [lambda x: x]: + # s2 = s.copy() + # + # with pytest.raises(TypeError): + # idxr(s2)[3.0] = 0 + pass + + else: + + s2 = s.copy() + s2.loc[3.0] = 10 + assert s2.index.is_object() + + for idxr in [lambda x: x]: + s2 = s.copy() + idxr(s2)[3.0] = 0 + assert s2.index.is_object() + + # fallsback to position selection, series only + s = Series(np.arange(len(i)), index=i) + s[3] + msg = ( + r"cannot do (label|index) indexing " + r"on {klass} with these indexers \[3\.0\] of " + r"{kind}".format(klass=type(i), kind=str(float)) + ) + with pytest.raises(TypeError, match=msg): + s[3.0] def test_scalar_with_mixed(self): @@ -222,52 +224,59 @@ def test_scalar_with_mixed(self): expected = 3 assert result == expected - def test_scalar_integer(self): + @pytest.mark.parametrize( + "index_func", + [ + tm.makeIntIndex, + tm.makeRangeIndex, + ], + ) + def test_scalar_integer(self, index_func): # test how scalar float indexers work on int indexes # integer index - for i in [Int64Index(range(5)), RangeIndex(5)]: + i = index_func(5) - for s in [ - Series(np.arange(len(i))), - DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i), - ]: + for s in [ + Series(np.arange(len(i))), + DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i), + ]: - # coerce to equal int - for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: + # coerce to equal int + for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: - result = idxr(s)[3.0] - self.check(result, s, 3, getitem) + result = idxr(s)[3.0] + self.check(result, s, 3, getitem) - # coerce to equal int - for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: + # coerce to equal int + for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: - if isinstance(s, Series): + if isinstance(s, Series): - def compare(x, y): - assert x == y + def compare(x, y): + assert x == y - expected = 100 + expected = 100 + else: + compare = tm.assert_series_equal + if getitem: + expected = Series(100, index=range(len(s)), name=3) else: - compare = tm.assert_series_equal - if getitem: - expected = Series(100, index=range(len(s)), name=3) - else: - expected = Series(100.0, index=range(len(s)), name=3) + expected = Series(100.0, index=range(len(s)), name=3) - s2 = s.copy() - idxr(s2)[3.0] = 100 + s2 = s.copy() + idxr(s2)[3.0] = 100 - result = idxr(s2)[3.0] - compare(result, expected) + result = idxr(s2)[3.0] + compare(result, expected) - result = idxr(s2)[3] - compare(result, expected) + result = idxr(s2)[3] + compare(result, expected) - # contains - # coerce to equal int - assert 3.0 in s + # contains + # coerce to equal int + assert 3.0 in s def test_scalar_float(self): diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index 312a0c6531cfb..55cbe66cf2ec3 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -9,61 +9,60 @@ class TestScalar(Base): - def test_at_and_iat_get(self): + + @pytest.mark.parametrize("kind", ["series", "frame"]) + def test_at_and_iat_get(self, kind): def _check(f, func, values=False): if f is not None: - indicies = self.generate_indices(f, values) - for i in indicies: + indices = self.generate_indices(f, values) + for i in indices: result = getattr(f, func)[i] expected = self.get_value(func, f, i, values) tm.assert_almost_equal(result, expected) - for kind in self._kinds: - - d = getattr(self, kind) + d = getattr(self, kind) - # iat - for f in [d["ints"], d["uints"]]: - _check(f, "iat", values=True) + # iat + for f in [d["ints"], d["uints"]]: + _check(f, "iat", values=True) - for f in [d["labels"], d["ts"], d["floats"]]: - if f is not None: - msg = "iAt based indexing can only have integer indexers" - with pytest.raises(ValueError, match=msg): - self.check_values(f, "iat") + for f in [d["labels"], d["ts"], d["floats"]]: + if f is not None: + msg = "iAt based indexing can only have integer indexers" + with pytest.raises(ValueError, match=msg): + self.check_values(f, "iat") - # at - for f in [d["ints"], d["uints"], d["labels"], d["ts"], d["floats"]]: - _check(f, "at") + # at + for f in [d["ints"], d["uints"], d["labels"], d["ts"], d["floats"]]: + _check(f, "at") - def test_at_and_iat_set(self): + @pytest.mark.parametrize("kind", ["series", "frame"]) + def test_at_and_iat_set(self, kind): def _check(f, func, values=False): if f is not None: - indicies = self.generate_indices(f, values) - for i in indicies: + indices = self.generate_indices(f, values) + for i in indices: getattr(f, func)[i] = 1 expected = self.get_value(func, f, i, values) tm.assert_almost_equal(expected, 1) - for kind in self._kinds: + d = getattr(self, kind) - d = getattr(self, kind) + # iat + for f in [d["ints"], d["uints"]]: + _check(f, "iat", values=True) - # iat - for f in [d["ints"], d["uints"]]: - _check(f, "iat", values=True) - - for f in [d["labels"], d["ts"], d["floats"]]: - if f is not None: - msg = "iAt based indexing can only have integer indexers" - with pytest.raises(ValueError, match=msg): - _check(f, "iat") + for f in [d["labels"], d["ts"], d["floats"]]: + if f is not None: + msg = "iAt based indexing can only have integer indexers" + with pytest.raises(ValueError, match=msg): + _check(f, "iat") - # at - for f in [d["ints"], d["uints"], d["labels"], d["ts"], d["floats"]]: - _check(f, "at") + # at + for f in [d["ints"], d["uints"], d["labels"], d["ts"], d["floats"]]: + _check(f, "at") class TestScalar2: From 31affac95d7d13156c0e4c6dddb58949094c98d3 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 19:25:06 -0800 Subject: [PATCH 2/9] parametrize --- pandas/tests/indexing/test_floats.py | 65 +++++++++++++--------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index 1361818a2932e..e891670001709 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -225,58 +225,55 @@ def test_scalar_with_mixed(self): assert result == expected @pytest.mark.parametrize( - "index_func", - [ - tm.makeIntIndex, - tm.makeRangeIndex, - ], + "index_func", [tm.makeIntIndex, tm.makeRangeIndex], ) - def test_scalar_integer(self, index_func): + @pytest.mark.parametrize("klass", [Series, DataFrame]) + def test_scalar_integer(self, index_func, klass): # test how scalar float indexers work on int indexes # integer index i = index_func(5) - for s in [ - Series(np.arange(len(i))), - DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i), - ]: + if klass is Series: + obj = Series(np.arange(len(i))) + else: + obj = DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i) - # coerce to equal int - for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: + # coerce to equal int + for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: - result = idxr(s)[3.0] - self.check(result, s, 3, getitem) + result = idxr(obj)[3.0] + self.check(result, obj, 3, getitem) - # coerce to equal int - for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: + # coerce to equal int + for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: - if isinstance(s, Series): + if isinstance(obj, Series): - def compare(x, y): - assert x == y + def compare(x, y): + assert x == y - expected = 100 + expected = 100 + else: + compare = tm.assert_series_equal + if getitem: + expected = Series(100, index=range(len(obj)), name=3) else: - compare = tm.assert_series_equal - if getitem: - expected = Series(100, index=range(len(s)), name=3) - else: - expected = Series(100.0, index=range(len(s)), name=3) + expected = Series(100.0, index=range(len(obj)), name=3) - s2 = s.copy() - idxr(s2)[3.0] = 100 + s2 = obj.copy() + idxr(s2)[3.0] = 100 - result = idxr(s2)[3.0] - compare(result, expected) + result = idxr(s2)[3.0] + compare(result, expected) - result = idxr(s2)[3] - compare(result, expected) + result = idxr(s2)[3] + compare(result, expected) - # contains - # coerce to equal int - assert 3.0 in s + # contains + # coerce to equal int + assert 3.0 in obj def test_scalar_float(self): From f8521ffcd781fa0c83c1fedef81be5d78572985f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 19:27:59 -0800 Subject: [PATCH 3/9] parametrize --- pandas/tests/indexing/test_floats.py | 217 ++++++++++++++------------- 1 file changed, 109 insertions(+), 108 deletions(-) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index e891670001709..9aba5a20af717 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -328,76 +328,74 @@ def test_scalar_float(self): with pytest.raises(TypeError, match=msg): s2.iloc[3.0] = 0 - def test_slice_non_numeric(self): - - # GH 4892 - # float_indexers should raise exceptions - # on appropriate Index types & accessors - - for index in [ + @pytest.mark.parametrize( + "index_func", + [ tm.makeStringIndex, tm.makeUnicodeIndex, tm.makeDateIndex, tm.makeTimedeltaIndex, tm.makePeriodIndex, + ], + ) + def test_slice_non_numeric(self, index_func): + + # GH 4892 + # float_indexers should raise exceptions + # on appropriate Index types & accessors + + index = index_func(5) + for s in [ + Series(range(5), index=index), + DataFrame(np.random.randn(5, 2), index=index), ]: - index = index(5) - for s in [ - Series(range(5), index=index), - DataFrame(np.random.randn(5, 2), index=index), - ]: + # getitem + for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + + msg = ( + "cannot do slice indexing " + r"on {klass} with these indexers \[(3|4)\.0\] of " + "{kind}".format(klass=type(index), kind=str(float)) + ) + with pytest.raises(TypeError, match=msg): + s.iloc[l] - # getitem - for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: msg = ( "cannot do slice indexing " - r"on {klass} with these indexers \[(3|4)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + r"on {klass} with these indexers " + r"\[(3|4)(\.0)?\] " + r"of ({kind_float}|{kind_int})".format( + klass=type(index), kind_float=str(float), kind_int=str(int), + ) ) with pytest.raises(TypeError, match=msg): - s.iloc[l] - - for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: - - msg = ( - "cannot do slice indexing " - r"on {klass} with these indexers " - r"\[(3|4)(\.0)?\] " - r"of ({kind_float}|{kind_int})".format( - klass=type(index), - kind_float=str(float), - kind_int=str(int), - ) - ) - with pytest.raises(TypeError, match=msg): - idxr(s)[l] + idxr(s)[l] - # setitem - for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + # setitem + for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + msg = ( + "cannot do slice indexing " + r"on {klass} with these indexers \[(3|4)\.0\] of " + "{kind}".format(klass=type(index), kind=str(float)) + ) + with pytest.raises(TypeError, match=msg): + s.iloc[l] = 0 + + for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: msg = ( "cannot do slice indexing " - r"on {klass} with these indexers \[(3|4)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + r"on {klass} with these indexers " + r"\[(3|4)(\.0)?\] " + r"of ({kind_float}|{kind_int})".format( + klass=type(index), kind_float=str(float), kind_int=str(int), + ) ) with pytest.raises(TypeError, match=msg): - s.iloc[l] = 0 - - for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: - msg = ( - "cannot do slice indexing " - r"on {klass} with these indexers " - r"\[(3|4)(\.0)?\] " - r"of ({kind_float}|{kind_int})".format( - klass=type(index), - kind_float=str(float), - kind_int=str(int), - ) - ) - with pytest.raises(TypeError, match=msg): - idxr(s)[l] = 0 + idxr(s)[l] = 0 def test_slice_integer(self): @@ -530,83 +528,86 @@ def test_integer_positional_indexing(self): with pytest.raises(TypeError, match=msg): idxr(s)[l] - def test_slice_integer_frame_getitem(self): + @pytest.mark.parametrize( + "index_func", [tm.makeIntIndex, tm.makeRangeIndex], + ) + def test_slice_integer_frame_getitem(self, index_func): # similar to above, but on the getitem dim (of a DataFrame) - for index in [Int64Index(range(5)), RangeIndex(5)]: + index = index_func(5) - s = DataFrame(np.random.randn(5, 2), index=index) + s = DataFrame(np.random.randn(5, 2), index=index) - def f(idxr): + def f(idxr): - # getitem - for l in [slice(0.0, 1), slice(0, 1.0), slice(0.0, 1.0)]: - - result = idxr(s)[l] - indexer = slice(0, 2) - self.check(result, s, indexer, False) - - # positional indexing - msg = ( - "cannot do slice indexing " - r"on {klass} with these indexers \[(0|1)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) - ) - with pytest.raises(TypeError, match=msg): - s[l] + # getitem + for l in [slice(0.0, 1), slice(0, 1.0), slice(0.0, 1.0)]: - # getitem out-of-bounds - for l in [slice(-10, 10), slice(-10.0, 10.0)]: - - result = idxr(s)[l] - self.check(result, s, slice(-10, 10), True) + result = idxr(s)[l] + indexer = slice(0, 2) + self.check(result, s, indexer, False) # positional indexing msg = ( "cannot do slice indexing " - r"on {klass} with these indexers \[-10\.0\] of " + r"on {klass} with these indexers \[(0|1)\.0\] of " "{kind}".format(klass=type(index), kind=str(float)) ) with pytest.raises(TypeError, match=msg): - s[slice(-10.0, 10.0)] + s[l] - # getitem odd floats - for l, res in [ - (slice(0.5, 1), slice(1, 2)), - (slice(0, 0.5), slice(0, 1)), - (slice(0.5, 1.5), slice(1, 2)), - ]: + # getitem out-of-bounds + for l in [slice(-10, 10), slice(-10.0, 10.0)]: - result = idxr(s)[l] - self.check(result, s, res, False) + result = idxr(s)[l] + self.check(result, s, slice(-10, 10), True) - # positional indexing - msg = ( - "cannot do slice indexing " - r"on {klass} with these indexers \[0\.5\] of " - "{kind}".format(klass=type(index), kind=str(float)) - ) - with pytest.raises(TypeError, match=msg): - s[l] + # positional indexing + msg = ( + "cannot do slice indexing " + r"on {klass} with these indexers \[-10\.0\] of " + "{kind}".format(klass=type(index), kind=str(float)) + ) + with pytest.raises(TypeError, match=msg): + s[slice(-10.0, 10.0)] - # setitem - for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + # getitem odd floats + for l, res in [ + (slice(0.5, 1), slice(1, 2)), + (slice(0, 0.5), slice(0, 1)), + (slice(0.5, 1.5), slice(1, 2)), + ]: - sc = s.copy() - idxr(sc)[l] = 0 - result = idxr(sc)[l].values.ravel() - assert (result == 0).all() + result = idxr(s)[l] + self.check(result, s, res, False) - # positional indexing - msg = ( - "cannot do slice indexing " - r"on {klass} with these indexers \[(3|4)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) - ) - with pytest.raises(TypeError, match=msg): - s[l] = 0 + # positional indexing + msg = ( + "cannot do slice indexing " + r"on {klass} with these indexers \[0\.5\] of " + "{kind}".format(klass=type(index), kind=str(float)) + ) + with pytest.raises(TypeError, match=msg): + s[l] + + # setitem + for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + + sc = s.copy() + idxr(sc)[l] = 0 + result = idxr(sc)[l].values.ravel() + assert (result == 0).all() + + # positional indexing + msg = ( + "cannot do slice indexing " + r"on {klass} with these indexers \[(3|4)\.0\] of " + "{kind}".format(klass=type(index), kind=str(float)) + ) + with pytest.raises(TypeError, match=msg): + s[l] = 0 - f(lambda x: x.loc) + f(lambda x: x.loc) def test_slice_float(self): From 118ac4d3d34f16ba067f940ca1ec850662200174 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 7 Feb 2020 08:08:14 -0800 Subject: [PATCH 4/9] blackify --- pandas/tests/indexing/test_scalar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index 55cbe66cf2ec3..789e6022746d6 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -9,7 +9,6 @@ class TestScalar(Base): - @pytest.mark.parametrize("kind", ["series", "frame"]) def test_at_and_iat_get(self, kind): def _check(f, func, values=False): From 7322c853b33f36aa89fe94366af53801ae77143c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 8 Feb 2020 15:21:39 -0800 Subject: [PATCH 5/9] un-rebase --- pandas/tests/indexing/test_floats.py | 435 +++++++++++++-------------- pandas/tests/indexing/test_scalar.py | 66 ++-- 2 files changed, 246 insertions(+), 255 deletions(-) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index 6cc18a3989266..8bb88cd9fd63a 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -59,117 +59,115 @@ def test_scalar_error(self, index_func): with pytest.raises(TypeError, match=msg): s.iloc[3.0] = 0 - @pytest.mark.parametrize( - "index_func", - [ + def test_scalar_non_numeric(self): + + # GH 4892 + # float_indexers should raise exceptions + # on appropriate Index types & accessors + + for index in [ tm.makeStringIndex, tm.makeUnicodeIndex, tm.makeCategoricalIndex, tm.makeDateIndex, tm.makeTimedeltaIndex, tm.makePeriodIndex, - ], - ) - def test_scalar_non_numeric(self, index_func): - - # GH 4892 - # float_indexers should raise exceptions - # on appropriate Index types & accessors + ]: - i = index_func(5) + i = index(5) - for s in [ - Series(np.arange(len(i)), index=i), - DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i), - ]: + for s in [ + Series(np.arange(len(i)), index=i), + DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i), + ]: - # getting - for idxr, getitem in [(lambda x: x.iloc, False), (lambda x: x, True)]: + # getting + for idxr, getitem in [(lambda x: x.iloc, False), (lambda x: x, True)]: - # gettitem on a DataFrame is a KeyError as it is indexing - # via labels on the columns - if getitem and isinstance(s, DataFrame): + # gettitem on a DataFrame is a KeyError as it is indexing + # via labels on the columns + if getitem and isinstance(s, DataFrame): + error = KeyError + msg = r"^3(\.0)?$" + else: + error = TypeError + msg = ( + r"cannot do (label|positional) indexing " + r"on {klass} with these indexers \[3\.0\] of " + r"type float|" + "Cannot index by location index with a " + "non-integer key".format(klass=type(i).__name__) + ) + with pytest.raises(error, match=msg): + idxr(s)[3.0] + + # label based can be a TypeError or KeyError + if s.index.inferred_type in { + "categorical", + "string", + "unicode", + "mixed", + }: error = KeyError - msg = r"^3(\.0)?$" + msg = r"^3\.0$" else: error = TypeError msg = ( - r"cannot do (label|positional) indexing " + r"cannot do label indexing " r"on {klass} with these indexers \[3\.0\] of " - r"type float|" - "Cannot index by location index with a " - "non-integer key".format(klass=type(i).__name__) + r"type float".format(klass=type(i).__name__) ) with pytest.raises(error, match=msg): - idxr(s)[3.0] - - # label based can be a TypeError or KeyError - if s.index.inferred_type in { - "categorical", - "string", - "unicode", - "mixed", - }: - error = KeyError - msg = r"^3\.0$" - else: - error = TypeError + s.loc[3.0] + + # contains + assert 3.0 not in s + + # setting with a float fails with iloc msg = ( r"cannot do (label|positional) indexing " r"on {klass} with these indexers \[3\.0\] of " r"type float".format(klass=type(i).__name__) ) - with pytest.raises(error, match=msg): - s.loc[3.0] + with pytest.raises(TypeError, match=msg): + s.iloc[3.0] = 0 + + # setting with an indexer + if s.index.inferred_type in ["categorical"]: + # Value or Type Error + pass + elif s.index.inferred_type in ["datetime64", "timedelta64", "period"]: + + # these should prob work + # and are inconsistent between series/dataframe ATM + # for idxr in [lambda x: x]: + # s2 = s.copy() + # + # with pytest.raises(TypeError): + # idxr(s2)[3.0] = 0 + pass - # contains - assert 3.0 not in s + else: - # setting with a float fails with iloc + s2 = s.copy() + s2.loc[3.0] = 10 + assert s2.index.is_object() + + for idxr in [lambda x: x]: + s2 = s.copy() + idxr(s2)[3.0] = 0 + assert s2.index.is_object() + + # fallsback to position selection, series only + s = Series(np.arange(len(i)), index=i) + s[3] msg = ( - r"cannot do (label|positional) indexing " + r"cannot do label indexing " r"on {klass} with these indexers \[3\.0\] of " r"type float".format(klass=type(i).__name__) ) with pytest.raises(TypeError, match=msg): - s.iloc[3.0] = 0 - - # setting with an indexer - if s.index.inferred_type in ["categorical"]: - # Value or Type Error - pass - elif s.index.inferred_type in ["datetime64", "timedelta64", "period"]: - - # these should prob work - # and are inconsistent between series/dataframe ATM - # for idxr in [lambda x: x]: - # s2 = s.copy() - # - # with pytest.raises(TypeError): - # idxr(s2)[3.0] = 0 - pass - - else: - - s2 = s.copy() - s2.loc[3.0] = 10 - assert s2.index.is_object() - - for idxr in [lambda x: x]: - s2 = s.copy() - idxr(s2)[3.0] = 0 - assert s2.index.is_object() - - # fallsback to position selection, series only - s = Series(np.arange(len(i)), index=i) - s[3] - msg = ( - r"cannot do (label|positional) indexing " - r"on {klass} with these indexers \[3\.0\] of " - r"type float".format(klass=type(i).__name__) - ) - with pytest.raises(TypeError, match=msg): - s[3.0] + s[3.0] def test_scalar_with_mixed(self): @@ -224,56 +222,52 @@ def test_scalar_with_mixed(self): expected = 3 assert result == expected - @pytest.mark.parametrize( - "index_func", [tm.makeIntIndex, tm.makeRangeIndex], - ) - @pytest.mark.parametrize("klass", [Series, DataFrame]) - def test_scalar_integer(self, index_func, klass): + def test_scalar_integer(self): # test how scalar float indexers work on int indexes # integer index - i = index_func(5) + for i in [Int64Index(range(5)), RangeIndex(5)]: - if klass is Series: - obj = Series(np.arange(len(i))) - else: - obj = DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i) + for s in [ + Series(np.arange(len(i))), + DataFrame(np.random.randn(len(i), len(i)), index=i, columns=i), + ]: - # coerce to equal int - for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: + # coerce to equal int + for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: - result = idxr(obj)[3.0] - self.check(result, obj, 3, getitem) + result = idxr(s)[3.0] + self.check(result, s, 3, getitem) - # coerce to equal int - for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: + # coerce to equal int + for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]: - if isinstance(obj, Series): + if isinstance(s, Series): - def compare(x, y): - assert x == y + def compare(x, y): + assert x == y - expected = 100 - else: - compare = tm.assert_series_equal - if getitem: - expected = Series(100, index=range(len(obj)), name=3) - else: - expected = Series(100.0, index=range(len(obj)), name=3) + expected = 100 + else: + compare = tm.assert_series_equal + if getitem: + expected = Series(100, index=range(len(s)), name=3) + else: + expected = Series(100.0, index=range(len(s)), name=3) - s2 = obj.copy() - idxr(s2)[3.0] = 100 + s2 = s.copy() + idxr(s2)[3.0] = 100 - result = idxr(s2)[3.0] - compare(result, expected) + result = idxr(s2)[3.0] + compare(result, expected) - result = idxr(s2)[3] - compare(result, expected) + result = idxr(s2)[3] + compare(result, expected) - # contains - # coerce to equal int - assert 3.0 in obj + # contains + # coerce to equal int + assert 3.0 in s def test_scalar_float(self): @@ -328,70 +322,68 @@ def test_scalar_float(self): with pytest.raises(TypeError, match=msg): s2.iloc[3.0] = 0 - @pytest.mark.parametrize( - "index_func", - [ - tm.makeStringIndex, - tm.makeUnicodeIndex, - tm.makeDateIndex, - tm.makeTimedeltaIndex, - tm.makePeriodIndex, - ], - ) - def test_slice_non_numeric(self, index_func): + def test_slice_non_numeric(self): # GH 4892 # float_indexers should raise exceptions # on appropriate Index types & accessors - index = index_func(5) - for s in [ - Series(range(5), index=index), - DataFrame(np.random.randn(5, 2), index=index), + for index in [ + tm.makeStringIndex, + tm.makeUnicodeIndex, + tm.makeDateIndex, + tm.makeTimedeltaIndex, + tm.makePeriodIndex, ]: - # getitem - for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: - - msg = ( - "cannot do positional indexing " - r"on {klass} with these indexers \[(3|4)\.0\] of " - "type float".format(klass=type(index).__name__) - ) - with pytest.raises(TypeError, match=msg): - s.iloc[l] + index = index(5) + for s in [ + Series(range(5), index=index), + DataFrame(np.random.randn(5, 2), index=index), + ]: - for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: + # getitem + for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: msg = ( - "cannot do (slice|positional) indexing " - r"on {klass} with these indexers " - r"\[(3|4)(\.0)?\] " - r"of type (float|int)".format(klass=type(index).__name__) + "cannot do positional indexing " + r"on {klass} with these indexers \[(3|4)\.0\] of " + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): - idxr(s)[l] + s.iloc[l] - # setitem - for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: - msg = ( - "cannot do positional indexing " - r"on {klass} with these indexers \[(3|4)\.0\] of " - "type float".format(klass=type(index).__name__) - ) - with pytest.raises(TypeError, match=msg): - s.iloc[l] = 0 + msg = ( + "cannot do (slice|positional) indexing " + r"on {klass} with these indexers " + r"\[(3|4)(\.0)?\] " + r"of type (float|int)".format(klass=type(index).__name__) + ) + with pytest.raises(TypeError, match=msg): + idxr(s)[l] + + # setitem + for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: - for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: msg = ( - "cannot do (slice|positional) indexing " - r"on {klass} with these indexers " - r"\[(3|4)(\.0)?\] " - r"of type (float|int)".format(klass=type(index).__name__) + "cannot do positional indexing " + r"on {klass} with these indexers \[(3|4)\.0\] of " + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): - idxr(s)[l] = 0 + s.iloc[l] = 0 + + for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: + msg = ( + "cannot do (slice|positional) indexing " + r"on {klass} with these indexers " + r"\[(3|4)(\.0)?\] " + r"of type (float|int)".format(klass=type(index).__name__) + ) + with pytest.raises(TypeError, match=msg): + idxr(s)[l] = 0 def test_slice_integer(self): @@ -524,86 +516,83 @@ def test_integer_positional_indexing(self): with pytest.raises(TypeError, match=msg): idxr(s)[l] - @pytest.mark.parametrize( - "index_func", [tm.makeIntIndex, tm.makeRangeIndex], - ) - def test_slice_integer_frame_getitem(self, index_func): + def test_slice_integer_frame_getitem(self): # similar to above, but on the getitem dim (of a DataFrame) - index = index_func(5) - - s = DataFrame(np.random.randn(5, 2), index=index) - - def f(idxr): - - # getitem - for l in [slice(0.0, 1), slice(0, 1.0), slice(0.0, 1.0)]: + for index in [Int64Index(range(5)), RangeIndex(5)]: - result = idxr(s)[l] - indexer = slice(0, 2) - self.check(result, s, indexer, False) + s = DataFrame(np.random.randn(5, 2), index=index) - # positional indexing - msg = ( - "cannot do slice indexing " - r"on {klass} with these indexers \[(0|1)\.0\] of " - "type float".format(klass=type(index).__name__) - ) - with pytest.raises(TypeError, match=msg): - s[l] + def f(idxr): - # getitem out-of-bounds - for l in [slice(-10, 10), slice(-10.0, 10.0)]: + # getitem + for l in [slice(0.0, 1), slice(0, 1.0), slice(0.0, 1.0)]: - result = idxr(s)[l] - self.check(result, s, slice(-10, 10), True) + result = idxr(s)[l] + indexer = slice(0, 2) + self.check(result, s, indexer, False) - # positional indexing - msg = ( - "cannot do slice indexing " - r"on {klass} with these indexers \[-10\.0\] of " - "type float".format(klass=type(index).__name__) - ) - with pytest.raises(TypeError, match=msg): - s[slice(-10.0, 10.0)] + # positional indexing + msg = ( + "cannot do slice indexing " + r"on {klass} with these indexers \[(0|1)\.0\] of " + "type float".format(klass=type(index).__name__) + ) + with pytest.raises(TypeError, match=msg): + s[l] - # getitem odd floats - for l, res in [ - (slice(0.5, 1), slice(1, 2)), - (slice(0, 0.5), slice(0, 1)), - (slice(0.5, 1.5), slice(1, 2)), - ]: + # getitem out-of-bounds + for l in [slice(-10, 10), slice(-10.0, 10.0)]: - result = idxr(s)[l] - self.check(result, s, res, False) + result = idxr(s)[l] + self.check(result, s, slice(-10, 10), True) # positional indexing msg = ( "cannot do slice indexing " - r"on {klass} with these indexers \[0\.5\] of " + r"on {klass} with these indexers \[-10\.0\] of " "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): - s[l] + s[slice(-10.0, 10.0)] - # setitem - for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + # getitem odd floats + for l, res in [ + (slice(0.5, 1), slice(1, 2)), + (slice(0, 0.5), slice(0, 1)), + (slice(0.5, 1.5), slice(1, 2)), + ]: - sc = s.copy() - idxr(sc)[l] = 0 - result = idxr(sc)[l].values.ravel() - assert (result == 0).all() + result = idxr(s)[l] + self.check(result, s, res, False) - # positional indexing - msg = ( - "cannot do slice indexing " - r"on {klass} with these indexers \[(3|4)\.0\] of " - "type float".format(klass=type(index).__name__) - ) - with pytest.raises(TypeError, match=msg): - s[l] = 0 + # positional indexing + msg = ( + "cannot do slice indexing " + r"on {klass} with these indexers \[0\.5\] of " + "type float".format(klass=type(index).__name__) + ) + with pytest.raises(TypeError, match=msg): + s[l] + + # setitem + for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: + + sc = s.copy() + idxr(sc)[l] = 0 + result = idxr(sc)[l].values.ravel() + assert (result == 0).all() + + # positional indexing + msg = ( + "cannot do slice indexing " + r"on {klass} with these indexers \[(3|4)\.0\] of " + "type float".format(klass=type(index).__name__) + ) + with pytest.raises(TypeError, match=msg): + s[l] = 0 - f(lambda x: x.loc) + f(lambda x: x.loc) def test_slice_float(self): diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index 899c58eb5edea..3622b12b853a4 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -9,59 +9,61 @@ class TestScalar(Base): - @pytest.mark.parametrize("kind", ["series", "frame"]) - def test_at_and_iat_get(self, kind): + def test_at_and_iat_get(self): def _check(f, func, values=False): if f is not None: - indices = self.generate_indices(f, values) - for i in indices: + indicies = self.generate_indices(f, values) + for i in indicies: result = getattr(f, func)[i] expected = self.get_value(func, f, i, values) tm.assert_almost_equal(result, expected) - d = getattr(self, kind) + for kind in self._kinds: - # iat - for f in [d["ints"], d["uints"]]: - _check(f, "iat", values=True) + d = getattr(self, kind) - for f in [d["labels"], d["ts"], d["floats"]]: - if f is not None: - msg = "iAt based indexing can only have integer indexers" - with pytest.raises(ValueError, match=msg): - self.check_values(f, "iat") + # iat + for f in [d["ints"], d["uints"]]: + _check(f, "iat", values=True) + + for f in [d["labels"], d["ts"], d["floats"]]: + if f is not None: + msg = "iAt based indexing can only have integer indexers" + with pytest.raises(ValueError, match=msg): + self.check_values(f, "iat") - # at - for f in [d["ints"], d["uints"], d["labels"], d["ts"], d["floats"]]: - _check(f, "at") + # at + for f in [d["ints"], d["uints"], d["labels"], d["ts"], d["floats"]]: + _check(f, "at") - @pytest.mark.parametrize("kind", ["series", "frame"]) - def test_at_and_iat_set(self, kind): + def test_at_and_iat_set(self): def _check(f, func, values=False): if f is not None: - indices = self.generate_indices(f, values) - for i in indices: + indicies = self.generate_indices(f, values) + for i in indicies: getattr(f, func)[i] = 1 expected = self.get_value(func, f, i, values) tm.assert_almost_equal(expected, 1) - d = getattr(self, kind) + for kind in self._kinds: - # iat - for f in [d["ints"], d["uints"]]: - _check(f, "iat", values=True) + d = getattr(self, kind) - for f in [d["labels"], d["ts"], d["floats"]]: - if f is not None: - msg = "iAt based indexing can only have integer indexers" - with pytest.raises(ValueError, match=msg): - _check(f, "iat") + # iat + for f in [d["ints"], d["uints"]]: + _check(f, "iat", values=True) + + for f in [d["labels"], d["ts"], d["floats"]]: + if f is not None: + msg = "iAt based indexing can only have integer indexers" + with pytest.raises(ValueError, match=msg): + _check(f, "iat") - # at - for f in [d["ints"], d["uints"], d["labels"], d["ts"], d["floats"]]: - _check(f, "at") + # at + for f in [d["ints"], d["uints"], d["labels"], d["ts"], d["floats"]]: + _check(f, "at") class TestScalar2: From 22dd733fc5ca3db32eb499aa7e63af18611e7000 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 8 Feb 2020 15:27:30 -0800 Subject: [PATCH 6/9] cleanup leftover ix references --- pandas/tests/indexing/common.py | 26 +++++++++----------------- pandas/tests/indexing/test_loc.py | 10 +++++----- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index 3c027b035c2b8..f71e58cea51ca 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -121,18 +121,12 @@ def get_result(self, obj, method, key, 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] + assert method != "indexer" + assert method != "ix" # 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) + xp = getattr(obj, method).__getitem__(_axify(obj, key, axis)) return xp @@ -172,19 +166,18 @@ def check_values(self, f, func, values=False): def check_result( self, method1, key1, method2, key2, typs=None, axes=None, fails=None, ): + assert method1 == method2 + def _eq(axis, obj, key1, key2): """ compare equal for these 2 keys """ if axis > obj.ndim - 1: return + axified = _axify(obj, key1, axis) try: - rs = getattr(obj, method1).__getitem__(_axify(obj, key1, axis)) + rs = getattr(obj, method1).__getitem__(axified) - try: - xp = self.get_result(obj=obj, method=method2, key=key2, axis=axis) - except (KeyError, IndexError): - # TODO: why is this allowed? - return + xp = self.get_result(obj=obj, method=method2, key=key2, axis=axis) if is_scalar(rs) and is_scalar(xp): assert rs == xp @@ -217,8 +210,7 @@ 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) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 3a726fb9923ee..d59dfaed250e4 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -16,7 +16,7 @@ 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, "loc", 2, typs=["labels"], fails=TypeError) def test_loc_getitem_label(self): @@ -34,7 +34,7 @@ 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("loc", "f", "loc", "f", typs=["floats"], fails=KeyError) self.check_result("loc", "f", "loc", "f", typs=["floats"], fails=KeyError) self.check_result( "loc", 20, "loc", 20, typs=["ints", "uints", "mixed"], fails=KeyError, @@ -55,7 +55,7 @@ def test_loc_getitem_label_list_with_missing(self): self.check_result( "loc", [0, 2, 10], - "ix", + "loc", [0, 2, 10], typs=["ints", "uints", "floats"], axes=0, @@ -65,7 +65,7 @@ def test_loc_getitem_label_list_with_missing(self): self.check_result( "loc", [3, 6, 7], - "ix", + "loc", [3, 6, 7], typs=["ints", "uints", "floats"], axes=1, @@ -76,7 +76,7 @@ def test_loc_getitem_label_list_with_missing(self): self.check_result( "loc", [(1, 3), (1, 4), (2, 5)], - "ix", + "loc", [(1, 3), (1, 4), (2, 5)], typs=["multi"], axes=0, From 101e5b820753d72cee2e01bafb6db9861cbe9731 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 8 Feb 2020 15:31:11 -0800 Subject: [PATCH 7/9] remove duplicate arg --- pandas/tests/indexing/common.py | 13 ++++----- pandas/tests/indexing/test_iloc.py | 3 -- pandas/tests/indexing/test_loc.py | 44 ++++++++---------------------- 3 files changed, 17 insertions(+), 43 deletions(-) diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index f71e58cea51ca..510e9a9521327 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -164,20 +164,16 @@ 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, key1, key2, typs=None, axes=None, fails=None, ): - assert method1 == method2 - def _eq(axis, obj, key1, key2): """ compare equal for these 2 keys """ - if axis > obj.ndim - 1: - return axified = _axify(obj, key1, axis) try: - rs = getattr(obj, method1).__getitem__(axified) + rs = getattr(obj, method).__getitem__(axified) - xp = self.get_result(obj=obj, method=method2, key=key2, axis=axis) + xp = self.get_result(obj=obj, method=method, key=key2, axis=axis) if is_scalar(rs) and is_scalar(xp): assert rs == xp @@ -213,4 +209,5 @@ def _eq(axis, obj, key1, key2): 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, key1=key1, key2=key2) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 08ea4c1579ef8..4944696c6e698 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -20,7 +20,6 @@ def test_iloc_getitem_int(self): self.check_result( "iloc", 2, - "iloc", 2, typs=["labels", "mixed", "ts", "floats", "empty"], fails=IndexError, @@ -31,7 +30,6 @@ def test_iloc_getitem_neg_int(self): self.check_result( "iloc", -1, - "iloc", -1, typs=["labels", "mixed", "ts", "floats", "empty"], fails=IndexError, @@ -41,7 +39,6 @@ def test_iloc_getitem_list_int(self): self.check_result( "iloc", [0, 1, 2], - "iloc", [0, 1, 2], typs=["labels", "mixed", "ts", "floats", "empty"], fails=IndexError, diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index d59dfaed250e4..0309a4c6e417a 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -16,12 +16,12 @@ class TestLoc(Base): def test_loc_getitem_int(self): # int label - self.check_result("loc", 2, "loc", 2, typs=["labels"], fails=TypeError) + self.check_result("loc", 2, 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", "c", typs=["empty"], fails=KeyError) def test_loc_getitem_label_out_of_range(self): @@ -29,19 +29,18 @@ def test_loc_getitem_label_out_of_range(self): self.check_result( "loc", "f", - "loc", "f", typs=["ints", "uints", "labels", "mixed", "ts"], fails=KeyError, ) - self.check_result("loc", "f", "loc", "f", typs=["floats"], fails=KeyError) - self.check_result("loc", "f", "loc", "f", typs=["floats"], fails=KeyError) + self.check_result("loc", "f", "f", typs=["floats"], fails=KeyError) + self.check_result("loc", "f", "f", typs=["floats"], fails=KeyError) self.check_result( - "loc", 20, "loc", 20, typs=["ints", "uints", "mixed"], fails=KeyError, + "loc", 20, 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, 20, typs=["labels"], fails=TypeError) + self.check_result("loc", 20, 20, typs=["ts"], axes=0, fails=TypeError) + self.check_result("loc", 20, 20, typs=["floats"], axes=0, fails=KeyError) def test_loc_getitem_label_list(self): # TODO: test something here? @@ -50,12 +49,11 @@ 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], [0, 1, 2], typs=["empty"], fails=KeyError, ) self.check_result( "loc", [0, 2, 10], - "loc", [0, 2, 10], typs=["ints", "uints", "floats"], axes=0, @@ -65,7 +63,6 @@ def test_loc_getitem_label_list_with_missing(self): self.check_result( "loc", [3, 6, 7], - "loc", [3, 6, 7], typs=["ints", "uints", "floats"], axes=1, @@ -76,7 +73,6 @@ def test_loc_getitem_label_list_with_missing(self): self.check_result( "loc", [(1, 3), (1, 4), (2, 5)], - "loc", [(1, 3), (1, 4), (2, 5)], typs=["multi"], axes=0, @@ -88,7 +84,6 @@ def test_loc_getitem_label_list_fails(self): self.check_result( "loc", [20, 30, 40], - "loc", [20, 30, 40], typs=["ints", "uints"], axes=1, @@ -104,7 +99,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, b, typs=["empty"], fails=IndexError) def test_loc_getitem_label_slice(self): @@ -117,7 +112,6 @@ def test_loc_getitem_label_slice(self): self.check_result( "loc", slice(1, 3), - "loc", slice(1, 3), typs=["labels", "mixed", "empty", "ts", "floats"], fails=TypeError, @@ -126,7 +120,6 @@ def test_loc_getitem_label_slice(self): self.check_result( "loc", slice("20130102", "20130104"), - "loc", slice("20130102", "20130104"), typs=["ts"], axes=1, @@ -134,28 +127,15 @@ def test_loc_getitem_label_slice(self): ) self.check_result( - "loc", - slice(2, 8), - "loc", - slice(2, 8), - typs=["mixed"], - axes=0, - fails=TypeError, + "loc", slice(2, 8), 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), 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, From 5d2ee3215ed25c89aa86292a9bededa440d23a6f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 8 Feb 2020 15:35:46 -0800 Subject: [PATCH 8/9] remove duplicate arg --- pandas/tests/indexing/common.py | 10 ++-- pandas/tests/indexing/test_iloc.py | 3 -- pandas/tests/indexing/test_loc.py | 73 ++++++++---------------------- 3 files changed, 24 insertions(+), 62 deletions(-) diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index 510e9a9521327..a359ec77916b6 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -164,16 +164,16 @@ def check_values(self, f, func, values=False): tm.assert_almost_equal(result, expected) def check_result( - self, method, key1, 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 """ - axified = _axify(obj, key1, axis) + axified = _axify(obj, key, axis) try: rs = getattr(obj, method).__getitem__(axified) - xp = self.get_result(obj=obj, method=method, key=key2, axis=axis) + xp = self.get_result(obj=obj, method=method, key=key, axis=axis) if is_scalar(rs) and is_scalar(xp): assert rs == xp @@ -210,4 +210,4 @@ def _eq(axis, obj, key1, key2): obj = d[typ] if ax < obj.ndim: - _eq(axis=ax, obj=obj, key1=key1, key2=key2) + _eq(axis=ax, obj=obj, key=key) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 4944696c6e698..bc5ba3d9b03e5 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -20,7 +20,6 @@ def test_iloc_getitem_int(self): self.check_result( "iloc", 2, - 2, typs=["labels", "mixed", "ts", "floats", "empty"], fails=IndexError, ) @@ -30,7 +29,6 @@ def test_iloc_getitem_neg_int(self): self.check_result( "iloc", -1, - -1, typs=["labels", "mixed", "ts", "floats", "empty"], fails=IndexError, ) @@ -39,7 +37,6 @@ def test_iloc_getitem_list_int(self): self.check_result( "iloc", [0, 1, 2], - [0, 1, 2], typs=["labels", "mixed", "ts", "floats", "empty"], fails=IndexError, ) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 0309a4c6e417a..02652d993e0f3 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -16,31 +16,27 @@ class TestLoc(Base): def test_loc_getitem_int(self): # int label - self.check_result("loc", 2, 2, typs=["labels"], fails=TypeError) + self.check_result("loc", 2, typs=["labels"], fails=TypeError) def test_loc_getitem_label(self): # label - self.check_result("loc", "c", "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", - "f", - typs=["ints", "uints", "labels", "mixed", "ts"], - fails=KeyError, + "loc", "f", typs=["ints", "uints", "labels", "mixed", "ts"], fails=KeyError, ) - self.check_result("loc", "f", "f", typs=["floats"], fails=KeyError) - self.check_result("loc", "f", "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, 20, typs=["ints", "uints", "mixed"], fails=KeyError, + "loc", 20, typs=["ints", "uints", "mixed"], fails=KeyError, ) - self.check_result("loc", 20, 20, typs=["labels"], fails=TypeError) - self.check_result("loc", 20, 20, typs=["ts"], axes=0, fails=TypeError) - self.check_result("loc", 20, 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? @@ -49,45 +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], [0, 1, 2], typs=["empty"], fails=KeyError, + "loc", [0, 1, 2], typs=["empty"], fails=KeyError, ) self.check_result( - "loc", - [0, 2, 10], - [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], - [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)], - [(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], - [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): @@ -99,7 +75,7 @@ def test_loc_getitem_bool(self): # boolean indexers b = [True, False, True, False] - self.check_result("loc", b, b, typs=["empty"], fails=IndexError) + self.check_result("loc", b, typs=["empty"], fails=IndexError) def test_loc_getitem_label_slice(self): @@ -112,34 +88,23 @@ def test_loc_getitem_label_slice(self): self.check_result( "loc", slice(1, 3), - slice(1, 3), typs=["labels", "mixed", "empty", "ts", "floats"], fails=TypeError, ) self.check_result( - "loc", - slice("20130102", "20130104"), - 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), 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), 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), - slice(2, 4, 2), - typs=["mixed"], - axes=0, - fails=TypeError, + "loc", slice(2, 4, 2), typs=["mixed"], axes=0, fails=TypeError, ) From 55ebf6f38dfbfa79cc77aac976217c7ea8b51898 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 8 Feb 2020 15:41:28 -0800 Subject: [PATCH 9/9] prune unnecessary --- pandas/tests/indexing/common.py | 36 ++++----------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index a359ec77916b6..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,21 +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] - - assert method != "indexer" - assert method != "ix" - - # in case we actually want 0 index slicing - with catch_warnings(record=True): - xp = getattr(obj, method).__getitem__(_axify(obj, key, axis)) - - return xp - def get_value(self, name, f, i, values=False): """ return the value for the location i """ @@ -171,33 +153,23 @@ def _eq(axis, obj, key): axified = _axify(obj, key, axis) try: - rs = getattr(obj, method).__getitem__(axified) - - xp = self.get_result(obj=obj, method=method, key=key, axis=axis) - - 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