Skip to content

BUG: is_[int|float]_dtypes functions not recognizing ArrowDtype #50672

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 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1049,6 +1049,7 @@ ExtensionArray
- 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`)
- Bug in :meth:`api.types.is_integer_dtype`, :meth:`api.types.is_unsigned_integer_dtype`, :meth:`api.types.is_signed_integer_dtype`, :meth:`api.types.is_float_dtype` where a custom :class:`ExtensionDtype` would not return ``True`` if ``kind`` did returned the corresponding NumPy type (:issue:`50667`)

Styler
^^^^^^
Expand Down
18 changes: 15 additions & 3 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,11 @@ def is_integer_dtype(arr_or_dtype) -> bool:
>>> is_integer_dtype(pd.Index([1, 2.])) # float
False
"""
return _is_dtype_type(arr_or_dtype, classes_and_not_datetimelike(np.integer))
return _is_dtype_type(
arr_or_dtype, classes_and_not_datetimelike(np.integer)
) or _is_dtype(
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ.kind in "iu"
)


def is_signed_integer_dtype(arr_or_dtype) -> bool:
Expand Down Expand Up @@ -744,7 +748,11 @@ def is_signed_integer_dtype(arr_or_dtype) -> bool:
>>> is_signed_integer_dtype(np.array([1, 2], dtype=np.uint32)) # unsigned
False
"""
return _is_dtype_type(arr_or_dtype, classes_and_not_datetimelike(np.signedinteger))
return _is_dtype_type(
arr_or_dtype, classes_and_not_datetimelike(np.signedinteger)
) or _is_dtype(
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ.kind == "i"
)


def is_unsigned_integer_dtype(arr_or_dtype) -> bool:
Expand Down Expand Up @@ -791,6 +799,8 @@ def is_unsigned_integer_dtype(arr_or_dtype) -> bool:
"""
return _is_dtype_type(
arr_or_dtype, classes_and_not_datetimelike(np.unsignedinteger)
) or _is_dtype(
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ.kind == "u"
)


Expand Down Expand Up @@ -1234,7 +1244,9 @@ def is_float_dtype(arr_or_dtype) -> bool:
>>> is_float_dtype(pd.Index([1, 2.]))
True
"""
return _is_dtype_type(arr_or_dtype, classes(np.floating))
return _is_dtype_type(arr_or_dtype, classes(np.floating)) or _is_dtype(
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ.kind in "f"
)


def is_bool_dtype(arr_or_dtype) -> bool:
Expand Down
37 changes: 37 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
import pandas._testing as tm
from pandas.api.types import (
is_bool_dtype,
is_float_dtype,
is_integer_dtype,
is_numeric_dtype,
is_signed_integer_dtype,
is_unsigned_integer_dtype,
)
from pandas.tests.extension import base

Expand Down Expand Up @@ -1446,6 +1450,39 @@ def test_is_numeric_dtype(data):
assert not is_numeric_dtype(data)


def test_is_integer_dtype(data):
# GH 50667
pa_type = data.dtype.pyarrow_dtype
if pa.types.is_integer(pa_type):
assert is_integer_dtype(data)
else:
assert not is_integer_dtype(data)


def test_is_signed_integer_dtype(data):
pa_type = data.dtype.pyarrow_dtype
if pa.types.is_signed_integer(pa_type):
assert is_signed_integer_dtype(data)
else:
assert not is_signed_integer_dtype(data)


def test_is_unsigned_integer_dtype(data):
pa_type = data.dtype.pyarrow_dtype
if pa.types.is_unsigned_integer(pa_type):
assert is_unsigned_integer_dtype(data)
else:
assert not is_unsigned_integer_dtype(data)


def test_is_float_dtype(data):
pa_type = data.dtype.pyarrow_dtype
if pa.types.is_floating(pa_type):
assert is_float_dtype(data)
else:
assert not is_float_dtype(data)


def test_pickle_roundtrip(data):
# GH 42600
expected = pd.Series(data)
Expand Down