From ebf45b2813606471da0a9b7fc3050e58c512ac24 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 28 Feb 2020 09:32:57 +0000 Subject: [PATCH 1/2] TST/CLN: Follow-up to #31867 --- pandas/tests/indexing/test_floats.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index c966962a7c87d..743018bd1f28a 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -190,16 +190,12 @@ def test_scalar_with_mixed(self): s2 = Series([1, 2, 3], index=["a", "b", "c"]) s3 = Series([1, 2, 3], index=["a", "b", 1.5]) - # lookup in a pure stringstr - # with an invalid indexer - msg = ( - r"cannot do label indexing " - r"on Index with these indexers \[1\.0\] of " - r"type float|" - "Cannot index by location index with a non-integer key" - ) + # lookup in a pure string index with an invalid indexer + with pytest.raises(KeyError, match="^1.0$"): s2[1.0] + + msg = "Cannot index by location index with a non-integer key" with pytest.raises(TypeError, match=msg): s2.iloc[1.0] From 05eff1e3a4eca2cf747d4ae22802029f5bd76b37 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 28 Feb 2020 09:47:56 +0000 Subject: [PATCH 2/2] test_scalar_non_numeric --- pandas/tests/indexing/test_floats.py | 43 +++++----------------------- 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index 743018bd1f28a..fff5ca03e80f4 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -95,43 +95,14 @@ def test_scalar_non_numeric(self, index_func, klass): s = gen_obj(klass, i) # getting - for idxr, getitem in [(lambda x: x.iloc, False), (lambda x: x, True)]: + with pytest.raises(KeyError, match="^3.0$"): + s[3.0] - if getitem: - error = KeyError - msg = r"^3\.0?$" - else: - error = TypeError - msg = ( - r"cannot do (label|positional) indexing " - fr"on {type(i).__name__} with these indexers \[3\.0\] of " - r"type float|" - "Cannot index by location index with a " - "non-integer key" - ) - 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", - "period", - "timedelta64", - "datetime64", - }: - error = KeyError - msg = r"^3\.0$" - else: - error = TypeError - msg = ( - r"cannot do (label|positional) indexing " - fr"on {type(i).__name__} with these indexers \[3\.0\] of " - "type float" - ) - with pytest.raises(error, match=msg): + msg = "Cannot index by location index with a non-integer key" + with pytest.raises(TypeError, match=msg): + s.iloc[3.0] + + with pytest.raises(KeyError, match="^3.0$"): s.loc[3.0] # contains