Skip to content

Commit

Permalink
BUG: is_[int|float]_dtypes functions not recognizing ArrowDtype (#50672)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored Jan 11, 2023
1 parent aa789ea commit 2797b84
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
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 @@ -1051,6 +1051,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`` 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

0 comments on commit 2797b84

Please sign in to comment.