Skip to content

BUG: is_numeric_dtype(ArrowDtype[numeric]) not returning True #50572

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -983,6 +983,7 @@ ExtensionArray
- Bug in :meth:`Series.round` for pyarrow-backed dtypes raising ``AttributeError`` (:issue:`50437`)
- Bug when concatenating an empty DataFrame with an ExtensionDtype to another DataFrame with the same ExtensionDtype, the resulting dtype turned into object (:issue:`48510`)
- Bug in :meth:`array.PandasArray.to_numpy` raising with ``NA`` value when ``na_value`` is specified (:issue:`40638`)
- Bug in :meth:`api.types.is_numeric_dtype` where a custom :class:`ExtensionDtype` would not return ``True`` if ``_is_numeric`` returned ``True`` (:issue:`50563`)

Styler
^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,8 @@ def is_numeric_dtype(arr_or_dtype) -> bool:
"""
return _is_dtype_type(
arr_or_dtype, classes_and_not_datetimelike(np.number, np.bool_)
) or _is_dtype(
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ._is_numeric
)


Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,24 @@ def test_is_numeric_dtype():
assert com.is_numeric_dtype(pd.Series([1, 2]))
assert com.is_numeric_dtype(pd.Index([1, 2.0]))

class MyNumericDType(ExtensionDtype):
@property
def type(self):
return str

@property
def name(self):
raise NotImplementedError

@classmethod
def construct_array_type(cls):
raise NotImplementedError

def _is_numeric(self) -> bool:
return True

assert com.is_numeric_dtype(MyNumericDType())


def test_is_float_dtype():
assert not com.is_float_dtype(str)
Expand Down
28 changes: 17 additions & 11 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@

import pandas as pd
import pandas._testing as tm
from pandas.api.types import is_bool_dtype
from pandas.api.types import (
is_bool_dtype,
is_numeric_dtype,
)
from pandas.tests.extension import base

pa = pytest.importorskip("pyarrow", minversion="1.0.1")
Expand Down Expand Up @@ -550,16 +553,6 @@ def test_groupby_extension_apply(
):
super().test_groupby_extension_apply(data_for_grouping, groupby_apply_op)

def test_in_numeric_groupby(self, data_for_grouping, request):
pa_dtype = data_for_grouping.dtype.pyarrow_dtype
if pa.types.is_integer(pa_dtype) or pa.types.is_floating(pa_dtype):
request.node.add_marker(
pytest.mark.xfail(
reason="ArrowExtensionArray doesn't support .sum() yet.",
)
)
super().test_in_numeric_groupby(data_for_grouping)

@pytest.mark.parametrize("as_index", [True, False])
def test_groupby_extension_agg(self, as_index, data_for_grouping, request):
pa_dtype = data_for_grouping.dtype.pyarrow_dtype
Expand Down Expand Up @@ -1446,6 +1439,19 @@ def test_is_bool_dtype():
tm.assert_series_equal(result, expected)


def test_is_numeric_dtype(data):
# GH 50563
pa_type = data.dtype.pyarrow_dtype
if (
pa.types.is_floating(pa_type)
or pa.types.is_integer(pa_type)
or pa.types.is_decimal(pa_type)
):
assert is_numeric_dtype(data)
else:
assert not is_numeric_dtype(data)


def test_pickle_roundtrip(data):
# GH 42600
expected = pd.Series(data)
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/io/json/test_json_table_schema_ext_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_build_table_schema(self):
"fields": [
{"name": "index", "type": "integer"},
{"name": "A", "type": "any", "extDtype": "DateDtype"},
{"name": "B", "type": "any", "extDtype": "decimal"},
{"name": "B", "type": "number", "extDtype": "decimal"},
{"name": "C", "type": "any", "extDtype": "string"},
{"name": "D", "type": "integer", "extDtype": "Int64"},
],
Expand Down Expand Up @@ -82,10 +82,10 @@ def test_as_json_table_type_ext_date_dtype(self):
],
)
def test_as_json_table_type_ext_decimal_array_dtype(self, decimal_data):
assert as_json_table_type(decimal_data.dtype) == "any"
assert as_json_table_type(decimal_data.dtype) == "number"

def test_as_json_table_type_ext_decimal_dtype(self):
assert as_json_table_type(DecimalDtype()) == "any"
assert as_json_table_type(DecimalDtype()) == "number"

@pytest.mark.parametrize(
"string_data",
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_build_decimal_series(self, dc):

fields = [
{"name": "id", "type": "integer"},
{"name": "a", "type": "any", "extDtype": "decimal"},
{"name": "a", "type": "number", "extDtype": "decimal"},
]

schema = {"fields": fields, "primaryKey": ["id"]}
Expand Down Expand Up @@ -257,7 +257,7 @@ def test_to_json(self, df):
fields = [
OrderedDict({"name": "idx", "type": "integer"}),
OrderedDict({"name": "A", "type": "any", "extDtype": "DateDtype"}),
OrderedDict({"name": "B", "type": "any", "extDtype": "decimal"}),
OrderedDict({"name": "B", "type": "number", "extDtype": "decimal"}),
OrderedDict({"name": "C", "type": "any", "extDtype": "string"}),
OrderedDict({"name": "D", "type": "integer", "extDtype": "Int64"}),
]
Expand Down