Skip to content
New issue

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

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

Already on GitHub? # to your account

DEPR: deprecate Index.is_numeric #50769

Merged
merged 35 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f065ca4
warm user is_numeric will be deprecated
ABCPAN-rank Jan 16, 2023
d82ce43
pre-commit imporve
topper-123 Jan 16, 2023
af94182
use is_numeric_dtype(idx) instead of idx.is_numeric()
ABCPAN-rank Jan 16, 2023
e6c4422
fix error first
ABCPAN-rank Jan 16, 2023
69e434f
fix error first
ABCPAN-rank Jan 16, 2023
6bc69f1
fix error second
ABCPAN-rank Jan 16, 2023
710f5fa
Merge branch 'main' into deprecate_index
ABCPAN-rank Jan 16, 2023
32eb278
fix error second
ABCPAN-rank Jan 16, 2023
f9cc4df
fix error third
ABCPAN-rank Jan 16, 2023
d44191c
fix error third
ABCPAN-rank Jan 16, 2023
11e2986
fix pre-commit error
ABCPAN-rank Jan 17, 2023
472e5cd
fix pre-commit error
ABCPAN-rank Jan 17, 2023
da44e5f
Merge branch 'main' into deprecate_index
ABCPAN-rank Jan 17, 2023
a2c608d
fix pre-commit error
ABCPAN-rank Jan 17, 2023
cb6e4f6
Merge branch 'main' into deprecate_index
ABCPAN-rank Jan 17, 2023
ec090fc
Merge branch 'pandas-dev:main' into deprecate_index
ABCPAN-rank Jan 17, 2023
1e16096
Merge branch 'main' into deprecate_index
ABCPAN-rank Jan 19, 2023
8aeb09e
fix pre-commit error
ABCPAN-rank Jan 25, 2023
2446021
Merge remote-tracking branch 'origin/deprecate_index' into deprecate_…
ABCPAN-rank Jan 25, 2023
d84c1a9
Merge branch 'main' into deprecate_index
ABCPAN-rank Jan 25, 2023
e375cce
fix
ABCPAN-rank Jan 25, 2023
32bda7f
Merge remote-tracking branch 'origin/deprecate_index' into deprecate_…
ABCPAN-rank Jan 25, 2023
dbade04
Merge branch 'main' into deprecate_index
ABCPAN-rank Jan 26, 2023
18dd072
Merge branch 'main' into deprecate_index
ABCPAN-rank Jan 27, 2023
1bc4d16
Merge branch 'main' into deprecate_index
ABCPAN-rank Jan 29, 2023
7107716
fix
ABCPAN-rank Feb 1, 2023
e9286b2
fix
ABCPAN-rank Feb 1, 2023
e36edeb
Merge branch 'main' into deprecate_index
ABCPAN-rank Feb 1, 2023
25a7919
Merge branch 'main' into deprecate_index
ABCPAN-rank Feb 1, 2023
a1627cc
fix
ABCPAN-rank Feb 2, 2023
4ab6447
Merge remote-tracking branch 'origin/deprecate_index' into deprecate_…
ABCPAN-rank Feb 2, 2023
9ec384a
Merge branch 'main' into deprecate_index
ABCPAN-rank Feb 2, 2023
dd0b94a
fix
ABCPAN-rank Feb 2, 2023
48f94bc
Merge remote-tracking branch 'origin/deprecate_index' into deprecate_…
ABCPAN-rank Feb 2, 2023
45f4765
Merge branch 'main' into deprecate_index
ABCPAN-rank Feb 2, 2023
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
3 changes: 3 additions & 0 deletions doc/source/user_guide/scale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()
Expand Down
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 @@ -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`)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand Down
28 changes: 19 additions & 9 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
ensure_int64,
ensure_object,
ensure_platform_int,
is_any_numeric_dtype,
is_bool_dtype,
is_categorical_dtype,
is_dtype_equal,
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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).

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

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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexes/multi/test_equivalence.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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():
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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",
Expand Down