diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 1c99ba0b8e412..67359bc13caeb 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -563,8 +563,7 @@ Deprecations ~~~~~~~~~~~~ - Deprecated argument ``infer_datetime_format`` in :func:`to_datetime` and :func:`read_csv`, as a strict version of it is now the default (:issue:`48621`) - Deprecated :func:`pandas.io.sql.execute`(:issue:`50185`) -- - +- :meth:`Index.is_integer` has been deprecated. Use :func:`pandas.api.types.is_integer_dtype` instead (:issue:`50042`) - :meth:`Index.is_floating` has been deprecated. Use :func:`pandas.api.types.is_float_dtype` instead (:issue:`50042`) .. --------------------------------------------------------------------------- diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 276d3019e126f..4bb187367da30 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -15,6 +15,7 @@ is_bool, is_categorical_dtype, is_extension_array_dtype, + is_integer_dtype, is_interval_dtype, is_number, is_numeric_dtype, @@ -1335,7 +1336,7 @@ def assert_indexing_slices_equivalent(ser: Series, l_slc: slice, i_slc: slice) - assert_series_equal(ser.loc[l_slc], expected) - if not ser.index.is_integer(): + if not is_integer_dtype(ser.index): # For integer indices, .loc and plain getitem are position-based. assert_series_equal(ser[l_slc], expected) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 19b6c9db20f11..b9727beba1026 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -98,6 +98,7 @@ is_float_dtype, is_hashable, is_integer, + is_integer_dtype, is_interval_dtype, is_iterator, is_list_like, @@ -2188,7 +2189,7 @@ def is_boolean(self) -> bool: See Also -------- - is_integer : Check if the Index only consists of integers. + is_integer : Check if the Index only consists of integers (deprecated). is_floating : Check if the Index is a floating type (deprecated). is_numeric : Check if the Index only consists of numeric data. is_object : Check if the Index is of the object dtype. @@ -2216,6 +2217,9 @@ def is_integer(self) -> bool: """ Check if the Index only consists of integers. + .. deprecated:: 2.0.0 + Use `pandas.api.types.is_integer_dtype` instead. + Returns ------- bool @@ -2244,6 +2248,12 @@ def is_integer(self) -> bool: >>> idx.is_integer() False """ + warnings.warn( + f"{type(self).__name__}.is_integer is deprecated. " + "Use pandas.api.types.is_integer_dtype instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) return self.inferred_type in ["integer"] @final @@ -2266,7 +2276,7 @@ def is_floating(self) -> bool: See Also -------- is_boolean : Check if the Index only consists of booleans. - is_integer : Check if the Index only consists of integers. + is_integer : Check if the Index only consists of integers (deprecated). is_numeric : Check if the Index only consists of numeric data. is_object : Check if the Index is of the object dtype. is_categorical : Check if the Index holds categorical data. @@ -2311,7 +2321,7 @@ def is_numeric(self) -> bool: See Also -------- is_boolean : Check if the Index only consists of booleans. - is_integer : Check if the Index only consists of integers. + is_integer : Check if the Index only consists of integers (deprecated). is_floating : Check if the Index is a floating type (deprecated). is_object : Check if the Index is of the object dtype. is_categorical : Check if the Index holds categorical data. @@ -2354,7 +2364,7 @@ def is_object(self) -> bool: See Also -------- is_boolean : Check if the Index only consists of booleans. - is_integer : Check if the Index only consists of integers. + is_integer : Check if the Index only consists of integers (deprecated). is_floating : Check if the Index is a floating type (deprecated). is_numeric : Check if the Index only consists of numeric data. is_categorical : Check if the Index holds categorical data. @@ -2395,7 +2405,7 @@ def is_categorical(self) -> bool: -------- CategoricalIndex : Index for categorical data. is_boolean : Check if the Index only consists of booleans. - is_integer : Check if the Index only consists of integers. + is_integer : Check if the Index only consists of integers (deprecated). is_floating : Check if the Index is a floating type (deprecated). is_numeric : Check if the Index only consists of numeric data. is_object : Check if the Index is of the object dtype. @@ -2438,7 +2448,7 @@ def is_interval(self) -> bool: -------- IntervalIndex : Index for Interval objects. is_boolean : Check if the Index only consists of booleans. - is_integer : Check if the Index only consists of integers. + is_integer : Check if the Index only consists of integers (deprecated). is_floating : Check if the Index is a floating type (deprecated). is_numeric : Check if the Index only consists of numeric data. is_object : Check if the Index is of the object dtype. @@ -3877,7 +3887,7 @@ def is_int(v): if kind == "getitem": # called from the getitem slicers, validate that we are in fact integers - if self.is_integer() or is_index_slice: + if is_integer_dtype(self.dtype) or is_index_slice: # Note: these checks are redundant if we know is_index_slice self._validate_indexer("slice", key.start, "getitem") self._validate_indexer("slice", key.stop, "getitem") diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index f5cc2808d3bd8..8ddc2b6349bde 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -803,6 +803,12 @@ def test_is_floating_is_deprecated(self, simple_index): with tm.assert_produces_warning(FutureWarning): idx.is_floating() + def test_is_integer_is_deprecated(self, simple_index): + # GH50042 + idx = simple_index + with tm.assert_produces_warning(FutureWarning): + idx.is_integer() + class NumericBase(Base): """ diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index 052965353231d..03c539d4d4b7c 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -604,7 +604,7 @@ def test_index_type_coercion(self, indexer): # integer indexes for s in [Series(range(5)), Series(range(5), index=range(1, 6))]: - assert s.index.is_integer() + assert is_integer_dtype(s.index) s2 = s.copy() indexer(s2)[0.1] = 0