diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 76aa426cc1cee..c1d9b2744b27e 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -637,6 +637,7 @@ Deprecations - :meth:`Index.is_floating` has been deprecated. Use :func:`pandas.api.types.is_float_dtype` instead (:issue:`50042`) - :meth:`Index.holds_integer` has been deprecated. Use :func:`pandas.api.types.infer_dtype` instead (:issue:`50243`) - :meth:`Index.is_categorical` has been deprecated. Use :func:`pandas.api.types.is_categorical_dtype` instead (:issue:`50042`) +- :meth:`Index.is_object` has been deprecated. Use :func:`pandas.api.types.is_object_dtype` instead (:issue:`50042`) - :meth:`Index.is_interval` has been deprecated. Use :func:`pandas.api.types.is_intterval_dtype` instead (:issue:`50042`) - diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index dbd70b8f4db62..5250ff19552ec 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1348,7 +1348,7 @@ def _format_native_types( return formatter.get_result_as_array() mask = isna(self) - if not self.is_object() and not quoting: + if not is_object_dtype(self) and not quoting: values = np.asarray(self).astype(str) else: values = np.array(self, dtype=object, copy=True) @@ -2258,7 +2258,7 @@ def is_boolean(self) -> bool: 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. + is_object : Check if the Index is of the object dtype (deprecated). is_categorical : Check if the Index holds categorical data. is_interval : Check if the Index holds Interval objects (deprecated). @@ -2302,7 +2302,7 @@ def is_integer(self) -> bool: is_boolean : Check if the Index only consists of booleans (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. + is_object : Check if the Index is of the object dtype. (deprecated). is_categorical : Check if the Index holds categorical data (deprecated). is_interval : Check if the Index holds Interval objects (deprecated). @@ -2350,7 +2350,7 @@ def is_floating(self) -> bool: is_boolean : Check if the Index only consists of booleans (deprecated). 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_object : Check if the Index is of the object dtype. (deprecated). is_categorical : Check if the Index holds categorical data (deprecated). is_interval : Check if the Index holds Interval objects (deprecated). @@ -2395,7 +2395,7 @@ def is_numeric(self) -> bool: is_boolean : Check if the Index only consists of booleans (deprecated). 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_object : Check if the Index is of the object dtype. (deprecated). is_categorical : Check if the Index holds categorical data (deprecated). is_interval : Check if the Index holds Interval objects (deprecated). @@ -2428,6 +2428,9 @@ def is_object(self) -> bool: """ Check if the Index is of the object dtype. + .. deprecated:: 2.0.0 + Use `pandas.api.types.is_object_dtype` instead. + Returns ------- bool @@ -2461,6 +2464,12 @@ def is_object(self) -> bool: >>> idx.is_object() False """ + warnings.warn( + f"{type(self).__name__}.is_object is deprecated." + "Use pandas.api.types.is_object_dtype instead", + FutureWarning, + stacklevel=find_stack_level(), + ) return is_object_dtype(self.dtype) @final @@ -2483,7 +2492,7 @@ def is_categorical(self) -> bool: 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. + is_object : Check if the Index is of the object dtype. (deprecated). is_interval : Check if the Index holds Interval objects (deprecated). Examples @@ -2536,7 +2545,7 @@ def is_interval(self) -> bool: 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. + is_object : Check if the Index is of the object dtype. (deprecated). is_categorical : Check if the Index holds categorical data (deprecated). Examples @@ -5018,7 +5027,7 @@ def _is_memory_usage_qualified(self) -> bool: """ Return a boolean if we need a qualified .info display. """ - return self.is_object() + return is_object_dtype(self.dtype) def __contains__(self, key: Any) -> bool: """ @@ -5132,7 +5141,7 @@ def _can_hold_identifiers_and_holds_name(self, name) -> bool: https://github.com/pandas-dev/pandas/issues/19764 """ if ( - self.is_object() + is_object_dtype(self.dtype) or is_string_dtype(self.dtype) or is_categorical_dtype(self.dtype) ): diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index efc7392eb00ba..55be16e0787c8 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -72,6 +72,7 @@ is_extension_array_dtype, is_integer_dtype, is_list_like, + is_object_dtype, is_string_dtype, is_timedelta64_dtype, needs_i8_conversion, @@ -2560,7 +2561,7 @@ class DataIndexableCol(DataCol): is_data_indexable = True def validate_names(self) -> None: - if not Index(self.values).is_object(): + if not is_object_dtype(Index(self.values)): # TODO: should the message here be more specifically non-str? raise ValueError("cannot have non-object label DataIndexableCol") diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index cd4cf9dd61b97..870d99ce67a40 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -829,6 +829,12 @@ def test_is_interval_is_deprecated(self, simple_index): with tm.assert_produces_warning(FutureWarning): idx.is_interval() + def test_is_object_is_deprecated(self, simple_index): + # GH50042 + idx = simple_index + with tm.assert_produces_warning(FutureWarning): + idx.is_object() + class NumericBase(Base): """ diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 1012734bab234..92a3cbd46266c 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -18,7 +18,10 @@ ) from pandas.util._test_decorators import async_mark -from pandas.core.dtypes.common import is_numeric_dtype +from pandas.core.dtypes.common import ( + is_numeric_dtype, + is_object_dtype, +) import pandas as pd from pandas import ( @@ -677,7 +680,7 @@ def test_is_numeric(self, index, expected): indirect=["index"], ) def test_is_object(self, index, expected): - assert index.is_object() is expected + assert is_object_dtype(index) is expected def test_summary(self, index): index._summary() diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index 2bd11fbb85d13..230605d2f3235 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -13,6 +13,7 @@ from pandas.core.dtypes.common import ( is_float_dtype, is_integer_dtype, + is_object_dtype, ) import pandas as pd @@ -618,7 +619,7 @@ def test_index_type_coercion(self, indexer): s2 = s.copy() indexer(s2)["0"] = 0 - assert s2.index.is_object() + assert is_object_dtype(s2.index) for s in [Series(range(5), index=np.arange(5.0))]: @@ -635,7 +636,7 @@ def test_index_type_coercion(self, indexer): s2 = s.copy() indexer(s2)["0"] = 0 - assert s2.index.is_object() + assert is_object_dtype(s2.index) class TestMisc: diff --git a/pandas/tests/series/methods/test_drop.py b/pandas/tests/series/methods/test_drop.py index dc4a11fd881fb..83d257605c487 100644 --- a/pandas/tests/series/methods/test_drop.py +++ b/pandas/tests/series/methods/test_drop.py @@ -5,6 +5,7 @@ Series, ) import pandas._testing as tm +from pandas.api.types import is_bool_dtype @pytest.mark.parametrize( @@ -57,7 +58,7 @@ def test_drop_with_ignore_errors(): # GH 8522 ser = Series([2, 3], index=[True, False]) - assert not ser.index.is_object() + assert is_bool_dtype(ser.index) assert ser.index.dtype == bool result = ser.drop(True) expected = Series([3], index=[False])