diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 6c8dd2f8da938..033f47f0c994d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -576,6 +576,7 @@ Deprecations - :meth:`Index.is_boolean` has been deprecated. Use :func:`pandas.api.types.is_bool_dtype` instead (:issue:`50042`) - :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`) +- :meth:`Index.holds_integer` has been deprecated. Use :func:`pandas.api.types.infer_dtype` instead (:issue:`50243`) .. --------------------------------------------------------------------------- .. _whatsnew_200.prior_deprecations: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 59af8f2bbcd51..7d5a7ac5945d6 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2487,12 +2487,28 @@ def is_interval(self) -> bool: return self.inferred_type in ["interval"] @final - def holds_integer(self) -> bool: + def _holds_integer(self) -> bool: """ Whether the type is an integer type. """ return self.inferred_type in ["integer", "mixed-integer"] + @final + def holds_integer(self) -> bool: + """ + Whether the type is an integer type. + + .. deprecated:: 2.0.0 + Use `pandas.api.types.infer_dtype` instead + """ + warnings.warn( + f"{type(self).__name__}.holds_integer is deprecated. " + "Use pandas.api.types.infer_dtype instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + return self._holds_integer() + @cache_readonly def inferred_type(self) -> str_t: """ @@ -5537,7 +5553,7 @@ def _should_fallback_to_positional(self) -> bool: """ Should an integer key be treated as positional? """ - return not self.holds_integer() + return not self._holds_integer() _index_shared_docs[ "get_indexer_non_unique" diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 2dc236a033415..fbd1eef138792 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -940,7 +940,7 @@ def __call__(self, *args, **kwargs): f"{kind} requires either y column or 'subplots=True'" ) if y is not None: - if is_integer(y) and not data.columns.holds_integer(): + if is_integer(y) and not data.columns._holds_integer(): y = data.columns[y] # converted to series actually. copy to not modify data = data[y].copy() @@ -948,7 +948,7 @@ def __call__(self, *args, **kwargs): elif isinstance(data, ABCDataFrame): data_cols = data.columns if x is not None: - if is_integer(x) and not data.columns.holds_integer(): + if is_integer(x) and not data.columns._holds_integer(): x = data_cols[x] elif not isinstance(data[x], ABCSeries): raise ValueError("x must be a label or position") @@ -957,7 +957,7 @@ def __call__(self, *args, **kwargs): # check if we have y as int or list of ints int_ylist = is_list_like(y) and all(is_integer(c) for c in y) int_y_arg = is_integer(y) or int_ylist - if int_y_arg and not data.columns.holds_integer(): + if int_y_arg and not data.columns._holds_integer(): y = data_cols[y] label_kw = kwargs["label"] if "label" in kwargs else False diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 3277ec06df650..fd17e57a16c4b 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1137,9 +1137,9 @@ def __init__(self, data, x, y, **kwargs) -> None: MPLPlot.__init__(self, data, **kwargs) if x is None or y is None: raise ValueError(self._kind + " requires an x and y column") - if is_integer(x) and not self.data.columns.holds_integer(): + if is_integer(x) and not self.data.columns._holds_integer(): x = self.data.columns[x] - if is_integer(y) and not self.data.columns.holds_integer(): + if is_integer(y) and not self.data.columns._holds_integer(): y = self.data.columns[y] # Scatter plot allows to plot objects data @@ -1196,7 +1196,7 @@ def __init__(self, data, x, y, s=None, c=None, **kwargs) -> None: elif is_hashable(s) and s in data.columns: s = data[s] super().__init__(data, x, y, s=s, **kwargs) - if is_integer(c) and not self.data.columns.holds_integer(): + if is_integer(c) and not self.data.columns._holds_integer(): c = self.data.columns[c] self.c = c @@ -1291,7 +1291,7 @@ def _kind(self) -> Literal["hexbin"]: def __init__(self, data, x, y, C=None, **kwargs) -> None: super().__init__(data, x, y, **kwargs) - if is_integer(C) and not self.data.columns.holds_integer(): + if is_integer(C) and not self.data.columns._holds_integer(): C = self.data.columns[C] self.C = C diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 6b0046dbe619c..ed8eb350234d1 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -815,6 +815,13 @@ def test_is_integer_is_deprecated(self, simple_index): with tm.assert_produces_warning(FutureWarning): idx.is_integer() + def test_holds_integer_deprecated(self, simple_index): + # GH50243 + idx = simple_index + msg = f"{type(idx).__name__}.holds_integer is deprecated. " + with tm.assert_produces_warning(FutureWarning, match=msg): + idx.holds_integer() + class NumericBase(Base): """