Skip to content

Commit ac5bc67

Browse files
authored
DEPR: deprecate Index.is_numeric (#50769)
1 parent 9277f93 commit ac5bc67

File tree

9 files changed

+77
-14
lines changed

9 files changed

+77
-14
lines changed

doc/source/user_guide/scale.rst

+3
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ Dask implements the most used parts of the pandas API. For example, we can do
333333
a familiar groupby aggregation.
334334

335335
.. ipython:: python
336+
:okwarning:
336337
337338
%time ddf.groupby("name")[["x", "y"]].mean().compute().head()
338339
@@ -356,6 +357,7 @@ we need to supply the divisions manually.
356357
Now we can do things like fast random access with ``.loc``.
357358

358359
.. ipython:: python
360+
:okwarning:
359361
360362
ddf.loc["2002-01-01 12:01":"2002-01-01 12:05"].compute()
361363
@@ -369,6 +371,7 @@ results will fit in memory, so we can safely call ``compute`` without running
369371
out of memory. At that point it's just a regular pandas object.
370372

371373
.. ipython:: python
374+
:okwarning:
372375
373376
@savefig dask_resample.png
374377
ddf[["x", "y"]].resample("1D").mean().cumsum().compute().plot()

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ Deprecations
752752
- :meth:`Index.is_integer` has been deprecated. Use :func:`pandas.api.types.is_integer_dtype` instead (:issue:`50042`)
753753
- :meth:`Index.is_floating` has been deprecated. Use :func:`pandas.api.types.is_float_dtype` instead (:issue:`50042`)
754754
- :meth:`Index.holds_integer` has been deprecated. Use :func:`pandas.api.types.infer_dtype` instead (:issue:`50243`)
755+
- :meth:`Index.is_numeric` has been deprecated. Use :func:`pandas.api.types.is_numeric_dtype` instead (:issue:`50042`)
755756
- :meth:`Index.is_categorical` has been deprecated. Use :func:`pandas.api.types.is_categorical_dtype` instead (:issue:`50042`)
756757
- :meth:`Index.is_object` has been deprecated. Use :func:`pandas.api.types.is_object_dtype` instead (:issue:`50042`)
757758
- :meth:`Index.is_interval` has been deprecated. Use :func:`pandas.api.types.is_intterval_dtype` instead (:issue:`50042`)

pandas/core/arrays/categorical.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from pandas.core.dtypes.common import (
4848
ensure_int64,
4949
ensure_platform_int,
50+
is_any_numeric_dtype,
5051
is_bool_dtype,
5152
is_categorical_dtype,
5253
is_datetime64_dtype,
@@ -595,7 +596,7 @@ def _from_inferred_categories(
595596

596597
if known_categories:
597598
# Convert to a specialized type with `dtype` if specified.
598-
if dtype.categories.is_numeric():
599+
if is_any_numeric_dtype(dtype.categories):
599600
cats = to_numeric(inferred_categories, errors="coerce")
600601
elif is_datetime64_dtype(dtype.categories):
601602
cats = to_datetime(inferred_categories, errors="coerce")
@@ -1758,7 +1759,7 @@ def _values_for_rank(self):
17581759
if mask.any():
17591760
values = values.astype("float64")
17601761
values[mask] = np.nan
1761-
elif self.categories.is_numeric():
1762+
elif is_any_numeric_dtype(self.categories):
17621763
values = np.array(self)
17631764
else:
17641765
# reorder the categories (so rank can use the float codes)

pandas/core/dtypes/common.py

+35
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,40 @@ def is_numeric_dtype(arr_or_dtype) -> bool:
12191219
)
12201220

12211221

1222+
def is_any_numeric_dtype(arr_or_dtype) -> bool:
1223+
"""
1224+
Check whether the provided array or dtype is of a real number dtype
1225+
1226+
Parameters
1227+
----------
1228+
arr_or_dtype : array-like or dtype
1229+
The array or dtype to check.
1230+
1231+
Returns
1232+
-------
1233+
boolean
1234+
Whether or not the array or dtype is of a real number dtype
1235+
1236+
Examples
1237+
-------
1238+
>>> is_any_numeric_dtype(str)
1239+
False
1240+
>>> is_any_numeric_dtype(int)
1241+
True
1242+
>>> is_any_numeric_dtype(float)
1243+
True
1244+
>>> is_any_numeric_dtype(complex(1,2))
1245+
False
1246+
>>> is_any_numeric_dtype(bool)
1247+
False
1248+
"""
1249+
return (
1250+
is_numeric_dtype(arr_or_dtype)
1251+
and not is_complex_dtype(arr_or_dtype)
1252+
and not is_bool_dtype(arr_or_dtype)
1253+
)
1254+
1255+
12221256
def is_float_dtype(arr_or_dtype) -> bool:
12231257
"""
12241258
Check whether the provided array or dtype is of a float dtype.
@@ -1774,6 +1808,7 @@ def is_all_strings(value: ArrayLike) -> bool:
17741808
"is_nested_list_like",
17751809
"is_number",
17761810
"is_numeric_dtype",
1811+
"is_any_numeric_dtype",
17771812
"is_numeric_v_string_like",
17781813
"is_object_dtype",
17791814
"is_period_dtype",

pandas/core/indexes/base.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
ensure_int64,
8989
ensure_object,
9090
ensure_platform_int,
91+
is_any_numeric_dtype,
9192
is_bool_dtype,
9293
is_categorical_dtype,
9394
is_dtype_equal,
@@ -2278,7 +2279,7 @@ def is_boolean(self) -> bool:
22782279
--------
22792280
is_integer : Check if the Index only consists of integers (deprecated).
22802281
is_floating : Check if the Index is a floating type (deprecated).
2281-
is_numeric : Check if the Index only consists of numeric data.
2282+
is_numeric : Check if the Index only consists of numeric data (deprecated).
22822283
is_object : Check if the Index is of the object dtype (deprecated).
22832284
is_categorical : Check if the Index holds categorical data.
22842285
is_interval : Check if the Index holds Interval objects (deprecated).
@@ -2322,7 +2323,7 @@ def is_integer(self) -> bool:
23222323
--------
23232324
is_boolean : Check if the Index only consists of booleans (deprecated).
23242325
is_floating : Check if the Index is a floating type (deprecated).
2325-
is_numeric : Check if the Index only consists of numeric data.
2326+
is_numeric : Check if the Index only consists of numeric data (deprecated).
23262327
is_object : Check if the Index is of the object dtype. (deprecated).
23272328
is_categorical : Check if the Index holds categorical data (deprecated).
23282329
is_interval : Check if the Index holds Interval objects (deprecated).
@@ -2370,7 +2371,7 @@ def is_floating(self) -> bool:
23702371
--------
23712372
is_boolean : Check if the Index only consists of booleans (deprecated).
23722373
is_integer : Check if the Index only consists of integers (deprecated).
2373-
is_numeric : Check if the Index only consists of numeric data.
2374+
is_numeric : Check if the Index only consists of numeric data (deprecated).
23742375
is_object : Check if the Index is of the object dtype. (deprecated).
23752376
is_categorical : Check if the Index holds categorical data (deprecated).
23762377
is_interval : Check if the Index holds Interval objects (deprecated).
@@ -2406,6 +2407,9 @@ def is_numeric(self) -> bool:
24062407
"""
24072408
Check if the Index only consists of numeric data.
24082409
2410+
.. deprecated:: 2.0.0
2411+
Use `pandas.api.types.is_numeric_dtype` instead.
2412+
24092413
Returns
24102414
-------
24112415
bool
@@ -2442,6 +2446,12 @@ def is_numeric(self) -> bool:
24422446
>>> idx.is_numeric()
24432447
False
24442448
"""
2449+
warnings.warn(
2450+
f"{type(self).__name__}.is_numeric is deprecated. "
2451+
"Use pandas.api.types.is_numeric_dtype instead",
2452+
FutureWarning,
2453+
stacklevel=find_stack_level(),
2454+
)
24452455
return self.inferred_type in ["integer", "floating"]
24462456

24472457
@final
@@ -2462,7 +2472,7 @@ def is_object(self) -> bool:
24622472
is_boolean : Check if the Index only consists of booleans (deprecated).
24632473
is_integer : Check if the Index only consists of integers (deprecated).
24642474
is_floating : Check if the Index is a floating type (deprecated).
2465-
is_numeric : Check if the Index only consists of numeric data.
2475+
is_numeric : Check if the Index only consists of numeric data (deprecated).
24662476
is_categorical : Check if the Index holds categorical data (deprecated).
24672477
is_interval : Check if the Index holds Interval objects (deprecated).
24682478
@@ -2512,7 +2522,7 @@ def is_categorical(self) -> bool:
25122522
is_boolean : Check if the Index only consists of booleans (deprecated).
25132523
is_integer : Check if the Index only consists of integers (deprecated).
25142524
is_floating : Check if the Index is a floating type (deprecated).
2515-
is_numeric : Check if the Index only consists of numeric data.
2525+
is_numeric : Check if the Index only consists of numeric data (deprecated).
25162526
is_object : Check if the Index is of the object dtype. (deprecated).
25172527
is_interval : Check if the Index holds Interval objects (deprecated).
25182528
@@ -2565,7 +2575,7 @@ def is_interval(self) -> bool:
25652575
is_boolean : Check if the Index only consists of booleans (deprecated).
25662576
is_integer : Check if the Index only consists of integers (deprecated).
25672577
is_floating : Check if the Index is a floating type (deprecated).
2568-
is_numeric : Check if the Index only consists of numeric data.
2578+
is_numeric : Check if the Index only consists of numeric data (deprecated).
25692579
is_object : Check if the Index is of the object dtype. (deprecated).
25702580
is_categorical : Check if the Index holds categorical data (deprecated).
25712581
@@ -3360,7 +3370,7 @@ def _intersection(self, other: Index, sort: bool = False):
33603370
pass
33613371
else:
33623372
# TODO: algos.unique1d should preserve DTA/TDA
3363-
if self.is_numeric():
3373+
if is_numeric_dtype(self):
33643374
# This is faster, because Index.unique() checks for uniqueness
33653375
# before calculating the unique values.
33663376
res = algos.unique1d(res_indexer)
@@ -6037,8 +6047,8 @@ def _should_compare(self, other: Index) -> bool:
60376047
Check if `self == other` can ever have non-False entries.
60386048
"""
60396049

6040-
if (is_bool_dtype(other) and self.is_numeric()) or (
6041-
is_bool_dtype(self) and other.is_numeric()
6050+
if (is_bool_dtype(other) and is_any_numeric_dtype(self)) or (
6051+
is_bool_dtype(self) and is_any_numeric_dtype(other)
60426052
):
60436053
# GH#16877 Treat boolean labels passed to a numeric index as not
60446054
# found. Without this fix False and True would be treated as 0 and 1

pandas/plotting/_matplotlib/core.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from pandas.util._exceptions import find_stack_level
2828

2929
from pandas.core.dtypes.common import (
30+
is_any_numeric_dtype,
3031
is_categorical_dtype,
3132
is_extension_array_dtype,
3233
is_float,
@@ -840,7 +841,7 @@ def _get_xticks(self, convert_period: bool = False):
840841
if convert_period and isinstance(index, ABCPeriodIndex):
841842
self.data = self.data.reindex(index=index.sort_values())
842843
x = self.data.index.to_timestamp()._mpl_repr()
843-
elif index.is_numeric():
844+
elif is_any_numeric_dtype(index):
844845
# Matplotlib supports numeric values or datetime objects as
845846
# xaxis values. Taking LBYL approach here, by the time
846847
# matplotlib raises exception when using non numeric/datetime

pandas/tests/indexes/common.py

+9
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,15 @@ def test_holds_integer_deprecated(self, simple_index):
813813
with tm.assert_produces_warning(FutureWarning, match=msg):
814814
idx.holds_integer()
815815

816+
def test_is_numeric_is_deprecated(self, simple_index):
817+
# GH50042
818+
idx = simple_index
819+
with tm.assert_produces_warning(
820+
FutureWarning,
821+
match=f"{type(idx).__name__}.is_numeric is deprecated. ",
822+
):
823+
idx.is_numeric()
824+
816825
def test_is_categorical_is_deprecated(self, simple_index):
817826
# GH50042
818827
idx = simple_index

pandas/tests/indexes/multi/test_equivalence.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.core.dtypes.common import is_any_numeric_dtype
5+
46
import pandas as pd
57
from pandas import (
68
Index,
@@ -253,7 +255,7 @@ def test_is_all_dates(idx):
253255

254256
def test_is_numeric(idx):
255257
# MultiIndex is never numeric
256-
assert not idx.is_numeric()
258+
assert not is_any_numeric_dtype(idx)
257259

258260

259261
def test_multiindex_compare():

pandas/tests/indexes/test_base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pandas.util._test_decorators import async_mark
2020

2121
from pandas.core.dtypes.common import (
22+
is_any_numeric_dtype,
2223
is_numeric_dtype,
2324
is_object_dtype,
2425
)
@@ -659,7 +660,7 @@ def test_append_empty_preserve_name(self, name, expected):
659660
indirect=["index"],
660661
)
661662
def test_is_numeric(self, index, expected):
662-
assert index.is_numeric() is expected
663+
assert is_any_numeric_dtype(index) is expected
663664

664665
@pytest.mark.parametrize(
665666
"index, expected",

0 commit comments

Comments
 (0)