Skip to content

DEPR: deprecate Index.is_object #50227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-

Expand Down
27 changes: 18 additions & 9 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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).

Expand Down Expand Up @@ -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).

Expand Down Expand Up @@ -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).

Expand Down Expand Up @@ -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).

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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)
):
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pandas.core.dtypes.common import (
is_float_dtype,
is_integer_dtype,
is_object_dtype,
)

import pandas as pd
Expand Down Expand Up @@ -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))]:

Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/methods/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Series,
)
import pandas._testing as tm
from pandas.api.types import is_bool_dtype


@pytest.mark.parametrize(
Expand Down Expand Up @@ -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])
Expand Down