From f065ca48c14e3ca6e9c732aa1a78be7d246f5b03 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Mon, 16 Jan 2023 12:31:03 +0800 Subject: [PATCH 01/18] warm user is_numeric will be deprecated --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/core/indexes/base.py | 21 +++++++++++++++------ pandas/tests/indexes/common.py | 5 +++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 033f47f0c994d..37a079d99bc0d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -577,6 +577,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`) .. --------------------------------------------------------------------------- .. _whatsnew_200.prior_deprecations: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 7d5a7ac5945d6..9442b62bfb13d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2204,7 +2204,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. is_categorical : Check if the Index holds categorical data. is_interval : Check if the Index holds Interval objects. @@ -2248,7 +2248,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. is_categorical : Check if the Index holds categorical data. is_interval : Check if the Index holds Interval objects. @@ -2296,7 +2296,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. is_categorical : Check if the Index holds categorical data. is_interval : Check if the Index holds Interval objects. @@ -2332,6 +2332,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 @@ -2368,6 +2371,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 @@ -2385,7 +2394,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. is_interval : Check if the Index holds Interval objects. @@ -2426,7 +2435,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 date (deprecated). is_object : Check if the Index is of the object dtype. is_interval : Check if the Index holds Interval objects. @@ -2469,7 +2478,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. is_categorical : Check if the Index holds categorical data. diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index ed8eb350234d1..62216dd700404 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -822,6 +822,11 @@ def test_holds_integer_deprecated(self, simple_index): with tm.assert_produces_warning(FutureWarning, match=msg): idx.holds_integer() + def test_numeric_is_deprecated(self, simple_index): + # GH50042 + idx = simple_index + with tm.assert_produces_warning(FutureWarning): + idx.is_numeric() class NumericBase(Base): """ From d82ce4398beb0df9ed308d790dbc6749ec634673 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Mon, 16 Jan 2023 14:53:11 +0800 Subject: [PATCH 02/18] pre-commit imporve --- pandas/core/indexes/base.py | 2 +- pandas/tests/indexes/common.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9442b62bfb13d..7aa1700be7bc4 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2435,7 +2435,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 date (deprecated). + is_numeric : Check if the Index only consists of numeric data (deprecated). is_object : Check if the Index is of the object dtype. is_interval : Check if the Index holds Interval objects. diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 62216dd700404..7e7694fc44da3 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -828,6 +828,7 @@ def test_numeric_is_deprecated(self, simple_index): with tm.assert_produces_warning(FutureWarning): idx.is_numeric() + class NumericBase(Base): """ Base class for numeric index (incl. RangeIndex) sub-class tests. From af94182b00c1c8dcfa75702e580376f6ec460216 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Mon, 16 Jan 2023 19:42:40 +0800 Subject: [PATCH 03/18] use is_numeric_dtype(idx) instead of idx.is_numeric() --- pandas/core/indexes/base.py | 6 ++++-- pandas/tests/indexes/multi/test_equivalence.py | 9 ++++++++- pandas/tests/indexes/test_base.py | 11 ++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 7aa1700be7bc4..cbd195afed445 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5908,8 +5908,10 @@ 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_numeric_dtype(self) and not is_bool_dtype(self) + ) or ( + is_bool_dtype(self) and is_numeric_dtype(other) and not is_bool_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/tests/indexes/multi/test_equivalence.py b/pandas/tests/indexes/multi/test_equivalence.py index c51b9386d7ec6..bedd8ac9716cf 100644 --- a/pandas/tests/indexes/multi/test_equivalence.py +++ b/pandas/tests/indexes/multi/test_equivalence.py @@ -8,6 +8,11 @@ Series, ) import pandas._testing as tm +from pandas.api.types import ( + is_bool_dtype, + is_complex_dtype, + is_numeric_dtype, +) def test_equals(idx): @@ -253,7 +258,9 @@ def test_is_all_dates(idx): def test_is_numeric(idx): # MultiIndex is never numeric - assert not idx.is_numeric() + assert not ( + is_numeric_dtype(idx) and not is_bool_dtype(idx) and not is_complex_dtype(idx) + ) def test_multiindex_compare(): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index af15cbc2f7929..0bfd5fca4cfcf 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -32,6 +32,11 @@ period_range, ) import pandas._testing as tm +from pandas.api.types import ( + is_bool_dtype, + is_complex_dtype, + is_numeric_dtype, +) from pandas.core.api import NumericIndex from pandas.core.indexes.api import ( Index, @@ -657,7 +662,11 @@ 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_numeric_dtype(index) + and not is_bool_dtype(index) + and not is_complex_dtype(index) + ) is expected @pytest.mark.parametrize( "index, expected", From e6c4422cebebe8f9d0f715b620168f2a15a539fd Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Mon, 16 Jan 2023 21:05:32 +0800 Subject: [PATCH 04/18] fix error first --- pandas/core/indexes/base.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index cbd195afed445..94e31297dc2a8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3256,7 +3256,11 @@ 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) + and not is_complex_dtype(self) + and not is_complex_dtype(self) + ): # This is faster, because Index.unique() checks for uniqueness # before calculating the unique values. res = algos.unique1d(res_indexer) From 69e434f4e32f17b2669b4e21971e8da783343d8b Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Mon, 16 Jan 2023 21:20:27 +0800 Subject: [PATCH 05/18] fix error first --- pandas/core/indexes/base.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 94e31297dc2a8..a2ed2d4760260 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5913,9 +5913,15 @@ def _should_compare(self, other: Index) -> bool: """ if ( - is_bool_dtype(other) and is_numeric_dtype(self) and not is_bool_dtype(self) + is_bool_dtype(other) + and is_numeric_dtype(self) + and not is_bool_dtype(self) + and not is_complex_dtype(self) ) or ( - is_bool_dtype(self) and is_numeric_dtype(other) and not is_bool_dtype(other) + is_bool_dtype(self) + and is_numeric_dtype(other) + and not is_bool_dtype(other) + and not is_complex_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 From 6bc69f1a696f21d47ef003726761e84035f2b4b5 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Mon, 16 Jan 2023 22:46:44 +0800 Subject: [PATCH 06/18] fix error second --- pandas/core/arrays/categorical.py | 10 ++++++++-- pandas/plotting/_matplotlib/core.py | 8 +++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 14f334d72dbb1..707f4cd607a13 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -49,6 +49,7 @@ ensure_platform_int, is_bool_dtype, is_categorical_dtype, + is_complex_dtype, is_datetime64_dtype, is_dict_like, is_dtype_equal, @@ -56,6 +57,7 @@ is_hashable, is_integer_dtype, is_list_like, + is_numeric_dtype, is_scalar, is_timedelta64_dtype, needs_i8_conversion, @@ -595,7 +597,7 @@ def _from_inferred_categories( if known_categories: # Convert to a specialized type with `dtype` if specified. - if dtype.categories.is_numeric(): + if dtype.categories: cats = to_numeric(inferred_categories, errors="coerce") elif is_datetime64_dtype(dtype.categories): cats = to_datetime(inferred_categories, errors="coerce") @@ -1763,7 +1765,11 @@ def _values_for_rank(self): if mask.any(): values = values.astype("float64") values[mask] = np.nan - elif self.categories.is_numeric(): + elif ( + is_numeric_dtype(self.categories) + and not is_complex_dtype(self.categories) + and not is_bool_dtype(self.categories) + ): values = np.array(self) else: # reorder the categories (so rank can use the float codes) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index fd17e57a16c4b..fc735f8dfb397 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -27,7 +27,9 @@ from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( + is_bool_dtype, is_categorical_dtype, + is_complex_dtype, is_extension_array_dtype, is_float, is_float_dtype, @@ -840,7 +842,11 @@ 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_numeric_dtype(index) + and not is_bool_dtype(index) + and not is_complex_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 From 32eb2787db95c8192698b27686b98bf16960d58d Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Tue, 17 Jan 2023 00:55:25 +0800 Subject: [PATCH 07/18] fix error second --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9049e645b609c..6ed78b3592d89 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2372,7 +2372,7 @@ def is_numeric(self) -> bool: False """ warnings.warn( - f"{type(self).__name__}.is_numeric is deprecated." + f"{type(self).__name__}.is_numeric is deprecated. " "Use pandas.api.types.is_numeric_dtype instead", FutureWarning, stacklevel=find_stack_level(), From f9cc4df9c6e366d19740222feed143b55b8ca668 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Tue, 17 Jan 2023 01:01:49 +0800 Subject: [PATCH 08/18] fix error third --- pandas/core/arrays/categorical.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 707f4cd607a13..1c80409d99ea5 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -429,7 +429,11 @@ def __init__( codes, categories = factorize(values, sort=True) except TypeError as err: codes, categories = factorize(values, sort=False) - if dtype.ordered: + if ( + is_numeric_dtype(dtype.categories) + and not is_bool_dtype(dtype.categories) + and not is_complex_dtype(dtype.categories) + ): # raise, as we don't have a sortable data structure and so # the user should give us one by specifying categories raise TypeError( From d44191c957de9df8f021cea32cd9059a44ec9f07 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Tue, 17 Jan 2023 01:06:46 +0800 Subject: [PATCH 09/18] fix error third --- pandas/core/arrays/categorical.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 1c80409d99ea5..97428d2860faa 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -429,11 +429,7 @@ def __init__( codes, categories = factorize(values, sort=True) except TypeError as err: codes, categories = factorize(values, sort=False) - if ( - is_numeric_dtype(dtype.categories) - and not is_bool_dtype(dtype.categories) - and not is_complex_dtype(dtype.categories) - ): + if dtype.ordered: # raise, as we don't have a sortable data structure and so # the user should give us one by specifying categories raise TypeError( @@ -601,7 +597,11 @@ def _from_inferred_categories( if known_categories: # Convert to a specialized type with `dtype` if specified. - if dtype.categories: + if ( + is_numeric_dtype(dtype.categories) + and not is_bool_dtype(dtype.categories) + and not is_complex_dtype(dtype.categories) + ): cats = to_numeric(inferred_categories, errors="coerce") elif is_datetime64_dtype(dtype.categories): cats = to_datetime(inferred_categories, errors="coerce") From 11e29863784cb5fc283e6faffd37c645a7ea730d Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Tue, 17 Jan 2023 10:56:54 +0800 Subject: [PATCH 10/18] fix pre-commit error --- pandas/tests/indexes/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 591b85f0b157b..eb1c81076968b 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -822,12 +822,12 @@ def test_holds_integer_deprecated(self, simple_index): with tm.assert_produces_warning(FutureWarning, match=msg): idx.holds_integer() - def test_numeric_is_deprecated(self, simple_index): + def test_is_numeric_is_deprecated(self, simple_index): # GH50042 idx = simple_index with tm.assert_produces_warning(FutureWarning): idx.is_numeric() - + def test_is_categorical_is_deprecated(self, simple_index): # GH50042 idx = simple_index From 472e5cde7413c33ce3ff9cff0fcfc85292e3ad24 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Tue, 17 Jan 2023 11:25:55 +0800 Subject: [PATCH 11/18] fix pre-commit error --- pandas/tests/indexes/multi/test_equivalence.py | 11 ++++++----- pandas/tests/indexes/test_base.py | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pandas/tests/indexes/multi/test_equivalence.py b/pandas/tests/indexes/multi/test_equivalence.py index bedd8ac9716cf..fafc0cbc0197b 100644 --- a/pandas/tests/indexes/multi/test_equivalence.py +++ b/pandas/tests/indexes/multi/test_equivalence.py @@ -1,6 +1,12 @@ import numpy as np import pytest +from pandas.core.dtypes.common import ( + is_bool_dtype, + is_complex_dtype, + is_numeric_dtype, +) + import pandas as pd from pandas import ( Index, @@ -8,11 +14,6 @@ Series, ) import pandas._testing as tm -from pandas.api.types import ( - is_bool_dtype, - is_complex_dtype, - is_numeric_dtype, -) def test_equals(idx): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 0bfd5fca4cfcf..c9117c26057b0 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -18,6 +18,12 @@ ) from pandas.util._test_decorators import async_mark +from pandas.core.dtypes.common import ( + is_bool_dtype, + is_complex_dtype, + is_numeric_dtype, +) + import pandas as pd from pandas import ( CategoricalIndex, @@ -32,11 +38,6 @@ period_range, ) import pandas._testing as tm -from pandas.api.types import ( - is_bool_dtype, - is_complex_dtype, - is_numeric_dtype, -) from pandas.core.api import NumericIndex from pandas.core.indexes.api import ( Index, From a2c608dbb3577b5c4c4787caa1588c393164e74f Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Tue, 17 Jan 2023 12:42:14 +0800 Subject: [PATCH 12/18] fix pre-commit error --- pandas/core/indexes/base.py | 1 + pandas/tests/indexes/test_base.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 925cae648badd..f7b038746fa9d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -90,6 +90,7 @@ ensure_platform_int, is_bool_dtype, is_categorical_dtype, + is_complex_dtype, is_dtype_equal, is_ea_or_datetimelike_dtype, is_extension_array_dtype, diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 84fc453c4a4ba..2d1cbbfc7de08 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -18,7 +18,6 @@ ) from pandas.util._test_decorators import async_mark - from pandas.core.dtypes.common import ( is_bool_dtype, is_complex_dtype, From 8aeb09e0dac9cd92e95b1a8eac439c641ce419c0 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Wed, 25 Jan 2023 14:37:49 +0800 Subject: [PATCH 13/18] fix pre-commit error --- pandas/core/arrays/categorical.py | 15 ++------ pandas/core/dtypes/common.py | 35 +++++++++++++++++++ pandas/core/indexes/base.py | 20 +++-------- pandas/plotting/_matplotlib/core.py | 8 +---- .../tests/indexes/multi/test_equivalence.py | 10 ++---- pandas/tests/indexes/test_base.py | 9 ++--- 6 files changed, 47 insertions(+), 50 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 97428d2860faa..8b705aa268685 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -47,9 +47,9 @@ from pandas.core.dtypes.common import ( ensure_int64, ensure_platform_int, + is_any_numeric_dtype, is_bool_dtype, is_categorical_dtype, - is_complex_dtype, is_datetime64_dtype, is_dict_like, is_dtype_equal, @@ -57,7 +57,6 @@ is_hashable, is_integer_dtype, is_list_like, - is_numeric_dtype, is_scalar, is_timedelta64_dtype, needs_i8_conversion, @@ -597,11 +596,7 @@ def _from_inferred_categories( if known_categories: # Convert to a specialized type with `dtype` if specified. - if ( - is_numeric_dtype(dtype.categories) - and not is_bool_dtype(dtype.categories) - and not is_complex_dtype(dtype.categories) - ): + 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") @@ -1769,11 +1764,7 @@ def _values_for_rank(self): if mask.any(): values = values.astype("float64") values[mask] = np.nan - elif ( - is_numeric_dtype(self.categories) - and not is_complex_dtype(self.categories) - and not is_bool_dtype(self.categories) - ): + 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 f7b038746fa9d..22390abeac674 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -88,9 +88,9 @@ ensure_int64, ensure_object, ensure_platform_int, + is_any_numeric_dtype, is_bool_dtype, is_categorical_dtype, - is_complex_dtype, is_dtype_equal, is_ea_or_datetimelike_dtype, is_extension_array_dtype, @@ -3276,11 +3276,7 @@ def _intersection(self, other: Index, sort: bool = False): pass else: # TODO: algos.unique1d should preserve DTA/TDA - if ( - is_numeric_dtype(self) - and not is_complex_dtype(self) - and not is_complex_dtype(self) - ): + if is_numeric_dtype(self): # This is faster, because Index.unique() checks for uniqueness # before calculating the unique values. res = algos.unique1d(res_indexer) @@ -5932,16 +5928,8 @@ def _should_compare(self, other: Index) -> bool: Check if `self == other` can ever have non-False entries. """ - if ( - is_bool_dtype(other) - and is_numeric_dtype(self) - and not is_bool_dtype(self) - and not is_complex_dtype(self) - ) or ( - is_bool_dtype(self) - and is_numeric_dtype(other) - and not is_bool_dtype(other) - and not is_complex_dtype(other) + 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 fc735f8dfb397..8c950359bf84c 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -27,9 +27,7 @@ from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( - is_bool_dtype, is_categorical_dtype, - is_complex_dtype, is_extension_array_dtype, is_float, is_float_dtype, @@ -842,11 +840,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 ( - is_numeric_dtype(index) - and not is_bool_dtype(index) - and not is_complex_dtype(index) - ): + elif is_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/multi/test_equivalence.py b/pandas/tests/indexes/multi/test_equivalence.py index fafc0cbc0197b..7e1b198d4c176 100644 --- a/pandas/tests/indexes/multi/test_equivalence.py +++ b/pandas/tests/indexes/multi/test_equivalence.py @@ -1,11 +1,7 @@ import numpy as np import pytest -from pandas.core.dtypes.common import ( - is_bool_dtype, - is_complex_dtype, - is_numeric_dtype, -) +from pandas.core.dtypes.common import is_numeric_dtype import pandas as pd from pandas import ( @@ -259,9 +255,7 @@ def test_is_all_dates(idx): def test_is_numeric(idx): # MultiIndex is never numeric - assert not ( - is_numeric_dtype(idx) and not is_bool_dtype(idx) and not is_complex_dtype(idx) - ) + assert not is_numeric_dtype(idx) def test_multiindex_compare(): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 2d1cbbfc7de08..383db1b478991 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -19,8 +19,7 @@ from pandas.util._test_decorators import async_mark from pandas.core.dtypes.common import ( - is_bool_dtype, - is_complex_dtype, + is_any_numeric_dtype, is_numeric_dtype, ) @@ -661,11 +660,7 @@ def test_append_empty_preserve_name(self, name, expected): indirect=["index"], ) def test_is_numeric(self, index, expected): - assert ( - is_numeric_dtype(index) - and not is_bool_dtype(index) - and not is_complex_dtype(index) - ) is expected + assert is_any_numeric_dtype(index) is expected @pytest.mark.parametrize( "index, expected", From e375ccef666b5d775cb8f996814ad0a2b1929f8c Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Wed, 25 Jan 2023 19:41:08 +0800 Subject: [PATCH 14/18] fix --- pandas/tests/indexes/common.py | 5 ++++- pandas/tests/indexes/multi/test_equivalence.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 0116ec474c89d..eafe3b06c2d44 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -821,7 +821,10 @@ def test_holds_integer_deprecated(self, simple_index): def test_is_numeric_is_deprecated(self, simple_index): # GH50042 idx = simple_index - with tm.assert_produces_warning(FutureWarning): + 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): diff --git a/pandas/tests/indexes/multi/test_equivalence.py b/pandas/tests/indexes/multi/test_equivalence.py index 7e1b198d4c176..3861d74cee092 100644 --- a/pandas/tests/indexes/multi/test_equivalence.py +++ b/pandas/tests/indexes/multi/test_equivalence.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from pandas.core.dtypes.common import is_numeric_dtype +from pandas.core.dtypes.common import is_any_numeric_dtype import pandas as pd from pandas import ( @@ -255,7 +255,7 @@ def test_is_all_dates(idx): def test_is_numeric(idx): # MultiIndex is never numeric - assert not is_numeric_dtype(idx) + assert not is_any_numeric_dtype(idx) def test_multiindex_compare(): From 7107716261da7234731d4713f4fba879945c7939 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Wed, 1 Feb 2023 19:17:09 +0800 Subject: [PATCH 15/18] fix --- pandas/core/indexes/base.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index e845bdd640ab0..4eb3ad8454c66 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2258,10 +2258,8 @@ 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 (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). From e9286b284f344dd3e1c3126b6b64013f60fff285 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Wed, 1 Feb 2023 19:34:31 +0800 Subject: [PATCH 16/18] fix --- doc/source/user_guide/scale.rst | 3 +++ 1 file changed, 3 insertions(+) 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() From a1627ccdc4f2e7f50f2bc154695a75b0a02985d0 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Thu, 2 Feb 2023 10:54:48 +0800 Subject: [PATCH 17/18] fix --- pandas/plotting/_matplotlib/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 8c950359bf84c..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 is_numeric_dtype(index): + 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 From dd0b94acd940da3a52d612d15132b84be8169359 Mon Sep 17 00:00:00 2001 From: ABCPAN-rank <1047068900@qq.com> Date: Thu, 2 Feb 2023 19:17:23 +0800 Subject: [PATCH 18/18] fix --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9ff1ee22946b5..02b852f14abc8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2408,7 +2408,7 @@ 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 + Use `pandas.api.types.is_numeric_dtype` instead. Returns -------