diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index e431d0bcf7e9b..e8ad2bef099a1 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3158,9 +3158,9 @@ def _convert_slice_indexer(self, key: slice, kind=None): # validate iloc if kind == "iloc": - self._validate_indexer("slice", key.start, "iloc") - self._validate_indexer("slice", key.stop, "iloc") - self._validate_indexer("slice", key.step, "iloc") + self._validate_indexer("positional", key.start, "iloc") + self._validate_indexer("positional", key.stop, "iloc") + self._validate_indexer("positional", key.step, "iloc") return key # potentially cast the bounds to integers @@ -3285,8 +3285,8 @@ def _invalid_indexer(self, form: str_t, key): Consistent invalid indexer message. """ raise TypeError( - f"cannot do {form} indexing on {type(self)} with these " - f"indexers [{key}] of {type(key)}" + f"cannot do {form} indexing on {type(self).__name__} with these " + f"indexers [{key}] of type {type(key).__name__}" ) # -------------------------------------------------------------------- diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index b143ff0aa9c02..d06d0d499ef47 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -406,9 +406,9 @@ def _convert_scalar_indexer(self, key, kind: str): is_int = is_integer(key) is_flt = is_float(key) if kind == "loc" and (is_int or is_flt): - self._invalid_indexer("index", key) + self._invalid_indexer("label", key) elif kind == "getitem" and is_flt: - self._invalid_indexer("index", key) + self._invalid_indexer("label", key) return super()._convert_scalar_indexer(key, kind=kind) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index ca4d1ff067f3d..6fc8c0e9ad459 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1050,9 +1050,8 @@ def test_getitem_setitem_float_labels(self): # positional slicing only via iloc! msg = ( - "cannot do slice indexing on " - r" with " - r"these indexers \[1.0\] of " + "cannot do positional indexing on Float64Index with " + r"these indexers \[1.0\] of type float" ) with pytest.raises(TypeError, match=msg): df.iloc[1.0:5] diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 7b1a9d8ff6ae3..5f4c78449f71d 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1860,9 +1860,8 @@ def check(df): # No NaN found -> error if len(indexer) == 0: msg = ( - "cannot do label indexing on " - r" " - r"with these indexers \[nan\] of " + "cannot do label indexing on RangeIndex " + r"with these indexers \[nan\] of type float" ) with pytest.raises(TypeError, match=msg): df.loc[:, np.nan] diff --git a/pandas/tests/indexing/test_categorical.py b/pandas/tests/indexing/test_categorical.py index 8c8dece53277e..da935b1c911d0 100644 --- a/pandas/tests/indexing/test_categorical.py +++ b/pandas/tests/indexing/test_categorical.py @@ -83,8 +83,8 @@ def test_loc_scalar(self): df.loc["d", "C"] = 10 msg = ( - r"cannot do label indexing on with these indexers \[1\] of " + "cannot do label indexing on CategoricalIndex with these " + r"indexers \[1\] of type int" ) with pytest.raises(TypeError, match=msg): df.loc[1] diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index 199d9e1013e23..8bb88cd9fd63a 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -54,7 +54,7 @@ def test_scalar_error(self, index_func): msg = ( "cannot do positional indexing on {klass} with these " - r"indexers \[3\.0\] of {kind}".format(klass=type(i), kind=str(float)) + r"indexers \[3\.0\] of type float".format(klass=type(i).__name__) ) with pytest.raises(TypeError, match=msg): s.iloc[3.0] = 0 @@ -92,11 +92,11 @@ def test_scalar_non_numeric(self): else: error = TypeError msg = ( - r"cannot do (label|index|positional) indexing " + r"cannot do (label|positional) indexing " r"on {klass} with these indexers \[3\.0\] of " - r"{kind}|" + r"type float|" "Cannot index by location index with a " - "non-integer key".format(klass=type(i), kind=str(float)) + "non-integer key".format(klass=type(i).__name__) ) with pytest.raises(error, match=msg): idxr(s)[3.0] @@ -113,9 +113,9 @@ def test_scalar_non_numeric(self): else: error = TypeError msg = ( - r"cannot do (label|index) indexing " + r"cannot do label indexing " r"on {klass} with these indexers \[3\.0\] of " - r"{kind}".format(klass=type(i), kind=str(float)) + r"type float".format(klass=type(i).__name__) ) with pytest.raises(error, match=msg): s.loc[3.0] @@ -125,9 +125,9 @@ def test_scalar_non_numeric(self): # setting with a float fails with iloc msg = ( - r"cannot do (label|index|positional) indexing " + r"cannot do (label|positional) indexing " r"on {klass} with these indexers \[3\.0\] of " - r"{kind}".format(klass=type(i), kind=str(float)) + r"type float".format(klass=type(i).__name__) ) with pytest.raises(TypeError, match=msg): s.iloc[3.0] = 0 @@ -162,9 +162,9 @@ def test_scalar_non_numeric(self): s = Series(np.arange(len(i)), index=i) s[3] msg = ( - r"cannot do (label|index) indexing " + r"cannot do label indexing " r"on {klass} with these indexers \[3\.0\] of " - r"{kind}".format(klass=type(i), kind=str(float)) + r"type float".format(klass=type(i).__name__) ) with pytest.raises(TypeError, match=msg): s[3.0] @@ -181,9 +181,9 @@ def test_scalar_with_mixed(self): msg = ( r"cannot do label indexing " r"on {klass} with these indexers \[1\.0\] of " - r"{kind}|" + r"type float|" "Cannot index by location index with a non-integer key".format( - klass=str(Index), kind=str(float) + klass=Index.__name__ ) ) with pytest.raises(TypeError, match=msg): @@ -203,7 +203,7 @@ def test_scalar_with_mixed(self): msg = ( r"cannot do label indexing " r"on {klass} with these indexers \[1\.0\] of " - r"{kind}".format(klass=str(Index), kind=str(float)) + r"type float".format(klass=Index.__name__) ) with pytest.raises(TypeError, match=msg): idxr(s3)[1.0] @@ -317,7 +317,7 @@ def test_scalar_float(self): msg = ( r"cannot do positional indexing " r"on {klass} with these indexers \[3\.0\] of " - r"{kind}".format(klass=str(Float64Index), kind=str(float)) + r"type float".format(klass=Float64Index.__name__) ) with pytest.raises(TypeError, match=msg): s2.iloc[3.0] = 0 @@ -346,9 +346,9 @@ def test_slice_non_numeric(self): for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: msg = ( - "cannot do slice indexing " + "cannot do positional indexing " r"on {klass} with these indexers \[(3|4)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s.iloc[l] @@ -356,14 +356,10 @@ def test_slice_non_numeric(self): for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]: msg = ( - "cannot do slice indexing " + "cannot do (slice|positional) 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), - ) + r"of type (float|int)".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): idxr(s)[l] @@ -372,23 +368,19 @@ def test_slice_non_numeric(self): for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]: msg = ( - "cannot do slice indexing " + "cannot do positional indexing " r"on {klass} with these indexers \[(3|4)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) 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 " + "cannot do (slice|positional) 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), - ) + r"of type (float|int)".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): idxr(s)[l] = 0 @@ -428,7 +420,7 @@ def test_slice_integer(self): msg = ( "cannot do slice indexing " r"on {klass} with these indexers \[(3|4)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s[l] @@ -452,7 +444,7 @@ def test_slice_integer(self): msg = ( "cannot do slice indexing " r"on {klass} with these indexers \[-6\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s[slice(-6.0, 6.0)] @@ -478,7 +470,7 @@ def test_slice_integer(self): msg = ( "cannot do slice indexing " r"on {klass} with these indexers \[(2|3)\.5\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s[l] @@ -496,7 +488,7 @@ def test_slice_integer(self): msg = ( "cannot do slice indexing " r"on {klass} with these indexers \[(3|4)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s[l] = 0 @@ -517,9 +509,9 @@ def test_integer_positional_indexing(self): klass = RangeIndex msg = ( - "cannot do slice indexing " + "cannot do (slice|positional) indexing " r"on {klass} with these indexers \[(2|4)\.0\] of " - "{kind}".format(klass=str(klass), kind=str(float)) + "type float".format(klass=klass.__name__) ) with pytest.raises(TypeError, match=msg): idxr(s)[l] @@ -544,7 +536,7 @@ def f(idxr): msg = ( "cannot do slice indexing " r"on {klass} with these indexers \[(0|1)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s[l] @@ -559,7 +551,7 @@ def f(idxr): msg = ( "cannot do slice indexing " r"on {klass} with these indexers \[-10\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s[slice(-10.0, 10.0)] @@ -578,7 +570,7 @@ def f(idxr): msg = ( "cannot do slice indexing " r"on {klass} with these indexers \[0\.5\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s[l] @@ -595,7 +587,7 @@ def f(idxr): msg = ( "cannot do slice indexing " r"on {klass} with these indexers \[(3|4)\.0\] of " - "{kind}".format(klass=type(index), kind=str(float)) + "type float".format(klass=type(index).__name__) ) with pytest.raises(TypeError, match=msg): s[l] = 0 diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index 312a0c6531cfb..3622b12b853a4 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -140,8 +140,8 @@ def test_series_at_raises_type_error(self): assert result == 1 msg = ( - "cannot do label indexing on " - r"with these indexers \[0\] of " + "cannot do label indexing on Index " + r"with these indexers \[0\] of type int" ) with pytest.raises(TypeError, match=msg): ser.at[0] @@ -157,8 +157,8 @@ def test_frame_raises_type_error(self): assert result == 1 msg = ( - "cannot do label indexing on " - r"with these indexers \[0\] of " + "cannot do label indexing on Index " + r"with these indexers \[0\] of type int" ) with pytest.raises(TypeError, match=msg): df.at["a", 0] diff --git a/pandas/tests/series/indexing/test_numeric.py b/pandas/tests/series/indexing/test_numeric.py index 0af574bc39d83..7e73e6366438b 100644 --- a/pandas/tests/series/indexing/test_numeric.py +++ b/pandas/tests/series/indexing/test_numeric.py @@ -128,9 +128,8 @@ def test_setitem_float_labels(): def test_slice_float_get_set(datetime_series): msg = ( - r"cannot do slice indexing on with these indexers \[{key}\] " - r"of " + "cannot do slice indexing on DatetimeIndex with these indexers " + r"\[{key}\] of type float" ) with pytest.raises(TypeError, match=msg.format(key=r"4\.0")): datetime_series[4.0:10.0]