diff --git a/doc/source/user_guide/scale.rst b/doc/source/user_guide/scale.rst index a974af4ffe1c5..65ed82d9d2cf5 100644 --- a/doc/source/user_guide/scale.rst +++ b/doc/source/user_guide/scale.rst @@ -333,6 +333,7 @@ Dask implements the most used parts of the pandas API. For example, we can do a familiar groupby aggregation. .. ipython:: python + :okwarning: %time ddf.groupby("name")[["x", "y"]].mean().compute().head() @@ -356,6 +357,7 @@ we need to supply the divisions manually. Now we can do things like fast random access with ``.loc``. .. ipython:: python + :okwarning: ddf.loc["2002-01-01 12:01":"2002-01-01 12:05"].compute() @@ -369,6 +371,7 @@ results will fit in memory, so we can safely call ``compute`` without running out of memory. At that point it's just a regular pandas object. .. ipython:: python + :okwarning: @savefig dask_resample.png ddf[["x", "y"]].resample("1D").mean().cumsum().compute().plot() diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index e8b8d071928ea..5c02e6997586b 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -749,6 +749,7 @@ Deprecations - :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`) +- :meth:`Index.is_numeric` has been deprecated. Use :func:`pandas.api.types.is_numeric_dtype` instead (:issue:`50042`) - :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/arrays/categorical.py b/pandas/core/arrays/categorical.py index f2e54185c11ff..648472d378fe1 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -47,6 +47,7 @@ from pandas.core.dtypes.common import ( ensure_int64, ensure_platform_int, + is_any_numeric_dtype, is_bool_dtype, is_categorical_dtype, is_datetime64_dtype, @@ -595,7 +596,7 @@ def _from_inferred_categories( if known_categories: # Convert to a specialized type with `dtype` if specified. - if dtype.categories.is_numeric(): + if is_any_numeric_dtype(dtype.categories): cats = to_numeric(inferred_categories, errors="coerce") elif is_datetime64_dtype(dtype.categories): cats = to_datetime(inferred_categories, errors="coerce") @@ -1758,7 +1759,7 @@ def _values_for_rank(self): if mask.any(): values = values.astype("float64") values[mask] = np.nan - elif self.categories.is_numeric(): + elif is_any_numeric_dtype(self.categories): values = np.array(self) else: # reorder the categories (so rank can use the float codes) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index aa3cdcd65b344..fb9817de2b69b 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1219,6 +1219,40 @@ def is_numeric_dtype(arr_or_dtype) -> bool: ) +def is_any_numeric_dtype(arr_or_dtype) -> bool: + """ + Check whether the provided array or dtype is of a real number dtype + + Parameters + ---------- + arr_or_dtype : array-like or dtype + The array or dtype to check. + + Returns + ------- + boolean + Whether or not the array or dtype is of a real number dtype + + Examples + ------- + >>> is_any_numeric_dtype(str) + False + >>> is_any_numeric_dtype(int) + True + >>> is_any_numeric_dtype(float) + True + >>> is_any_numeric_dtype(complex(1,2)) + False + >>> is_any_numeric_dtype(bool) + False + """ + return ( + is_numeric_dtype(arr_or_dtype) + and not is_complex_dtype(arr_or_dtype) + and not is_bool_dtype(arr_or_dtype) + ) + + def is_float_dtype(arr_or_dtype) -> bool: """ Check whether the provided array or dtype is of a float dtype. @@ -1774,6 +1808,7 @@ def is_all_strings(value: ArrayLike) -> bool: "is_nested_list_like", "is_number", "is_numeric_dtype", + "is_any_numeric_dtype", "is_numeric_v_string_like", "is_object_dtype", "is_period_dtype", diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 881e83313ced5..02b852f14abc8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -88,6 +88,7 @@ ensure_int64, ensure_object, ensure_platform_int, + is_any_numeric_dtype, is_bool_dtype, is_categorical_dtype, is_dtype_equal, @@ -2278,7 +2279,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_numeric : Check if the Index only consists of numeric data (deprecated). 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). @@ -2322,7 +2323,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_numeric : Check if the Index only consists of numeric data (deprecated). 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). @@ -2370,7 +2371,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_numeric : Check if the Index only consists of numeric data (deprecated). 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). @@ -2406,6 +2407,9 @@ def is_numeric(self) -> bool: """ Check if the Index only consists of numeric data. + .. deprecated:: 2.0.0 + Use `pandas.api.types.is_numeric_dtype` instead. + Returns ------- bool @@ -2442,6 +2446,12 @@ def is_numeric(self) -> bool: >>> idx.is_numeric() False """ + warnings.warn( + f"{type(self).__name__}.is_numeric is deprecated. " + "Use pandas.api.types.is_numeric_dtype instead", + FutureWarning, + stacklevel=find_stack_level(), + ) return self.inferred_type in ["integer", "floating"] @final @@ -2462,7 +2472,7 @@ def is_object(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_numeric : Check if the Index only consists of numeric data. + is_numeric : Check if the Index only consists of numeric data (deprecated). is_categorical : Check if the Index holds categorical data (deprecated). is_interval : Check if the Index holds Interval objects (deprecated). @@ -2512,7 +2522,7 @@ def is_categorical(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_numeric : Check if the Index only consists of numeric data. + is_numeric : Check if the Index only consists of numeric data (deprecated). is_object : Check if the Index is of the object dtype. (deprecated). is_interval : Check if the Index holds Interval objects (deprecated). @@ -2565,7 +2575,7 @@ def is_interval(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_numeric : Check if the Index only consists of numeric data. + is_numeric : Check if the Index only consists of numeric data (deprecated). is_object : Check if the Index is of the object dtype. (deprecated). is_categorical : Check if the Index holds categorical data (deprecated). @@ -3360,7 +3370,7 @@ def _intersection(self, other: Index, sort: bool = False): pass else: # TODO: algos.unique1d should preserve DTA/TDA - if self.is_numeric(): + if is_numeric_dtype(self): # This is faster, because Index.unique() checks for uniqueness # before calculating the unique values. res = algos.unique1d(res_indexer) @@ -6035,8 +6045,8 @@ def _should_compare(self, other: Index) -> bool: Check if `self == other` can ever have non-False entries. """ - if (is_bool_dtype(other) and self.is_numeric()) or ( - is_bool_dtype(self) and other.is_numeric() + if (is_bool_dtype(other) and is_any_numeric_dtype(self)) or ( + is_bool_dtype(self) and is_any_numeric_dtype(other) ): # GH#16877 Treat boolean labels passed to a numeric index as not # found. Without this fix False and True would be treated as 0 and 1 diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index fd17e57a16c4b..49b92e0984713 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -27,6 +27,7 @@ from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( + is_any_numeric_dtype, is_categorical_dtype, is_extension_array_dtype, is_float, @@ -840,7 +841,7 @@ def _get_xticks(self, convert_period: bool = False): if convert_period and isinstance(index, ABCPeriodIndex): self.data = self.data.reindex(index=index.sort_values()) x = self.data.index.to_timestamp()._mpl_repr() - elif index.is_numeric(): + elif is_any_numeric_dtype(index): # Matplotlib supports numeric values or datetime objects as # xaxis values. Taking LBYL approach here, by the time # matplotlib raises exception when using non numeric/datetime diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index c203792be2694..f36118e245eb4 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -813,6 +813,15 @@ def test_holds_integer_deprecated(self, simple_index): with tm.assert_produces_warning(FutureWarning, match=msg): idx.holds_integer() + def test_is_numeric_is_deprecated(self, simple_index): + # GH50042 + idx = simple_index + with tm.assert_produces_warning( + FutureWarning, + match=f"{type(idx).__name__}.is_numeric is deprecated. ", + ): + idx.is_numeric() + def test_is_categorical_is_deprecated(self, simple_index): # GH50042 idx = simple_index diff --git a/pandas/tests/indexes/multi/test_equivalence.py b/pandas/tests/indexes/multi/test_equivalence.py index c51b9386d7ec6..3861d74cee092 100644 --- a/pandas/tests/indexes/multi/test_equivalence.py +++ b/pandas/tests/indexes/multi/test_equivalence.py @@ -1,6 +1,8 @@ import numpy as np import pytest +from pandas.core.dtypes.common import is_any_numeric_dtype + import pandas as pd from pandas import ( Index, @@ -253,7 +255,7 @@ def test_is_all_dates(idx): def test_is_numeric(idx): # MultiIndex is never numeric - assert not idx.is_numeric() + assert not is_any_numeric_dtype(idx) def test_multiindex_compare(): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index d78da49c967ab..6a736dfcf8687 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -19,6 +19,7 @@ from pandas.util._test_decorators import async_mark from pandas.core.dtypes.common import ( + is_any_numeric_dtype, is_numeric_dtype, is_object_dtype, ) @@ -659,7 +660,7 @@ def test_append_empty_preserve_name(self, name, expected): indirect=["index"], ) def test_is_numeric(self, index, expected): - assert index.is_numeric() is expected + assert is_any_numeric_dtype(index) is expected @pytest.mark.parametrize( "index, expected",