Skip to content

Commit 2797b84

Browse files
authored
BUG: is_[int|float]_dtypes functions not recognizing ArrowDtype (#50672)
1 parent aa789ea commit 2797b84

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ ExtensionArray
10511051
- Bug when concatenating an empty DataFrame with an ExtensionDtype to another DataFrame with the same ExtensionDtype, the resulting dtype turned into object (:issue:`48510`)
10521052
- Bug in :meth:`array.PandasArray.to_numpy` raising with ``NA`` value when ``na_value`` is specified (:issue:`40638`)
10531053
- Bug in :meth:`api.types.is_numeric_dtype` where a custom :class:`ExtensionDtype` would not return ``True`` if ``_is_numeric`` returned ``True`` (:issue:`50563`)
1054+
- 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`)
10541055

10551056
Styler
10561057
^^^^^^

pandas/core/dtypes/common.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,11 @@ def is_integer_dtype(arr_or_dtype) -> bool:
690690
>>> is_integer_dtype(pd.Index([1, 2.])) # float
691691
False
692692
"""
693-
return _is_dtype_type(arr_or_dtype, classes_and_not_datetimelike(np.integer))
693+
return _is_dtype_type(
694+
arr_or_dtype, classes_and_not_datetimelike(np.integer)
695+
) or _is_dtype(
696+
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ.kind in "iu"
697+
)
694698

695699

696700
def is_signed_integer_dtype(arr_or_dtype) -> bool:
@@ -744,7 +748,11 @@ def is_signed_integer_dtype(arr_or_dtype) -> bool:
744748
>>> is_signed_integer_dtype(np.array([1, 2], dtype=np.uint32)) # unsigned
745749
False
746750
"""
747-
return _is_dtype_type(arr_or_dtype, classes_and_not_datetimelike(np.signedinteger))
751+
return _is_dtype_type(
752+
arr_or_dtype, classes_and_not_datetimelike(np.signedinteger)
753+
) or _is_dtype(
754+
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ.kind == "i"
755+
)
748756

749757

750758
def is_unsigned_integer_dtype(arr_or_dtype) -> bool:
@@ -791,6 +799,8 @@ def is_unsigned_integer_dtype(arr_or_dtype) -> bool:
791799
"""
792800
return _is_dtype_type(
793801
arr_or_dtype, classes_and_not_datetimelike(np.unsignedinteger)
802+
) or _is_dtype(
803+
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ.kind == "u"
794804
)
795805

796806

@@ -1234,7 +1244,9 @@ def is_float_dtype(arr_or_dtype) -> bool:
12341244
>>> is_float_dtype(pd.Index([1, 2.]))
12351245
True
12361246
"""
1237-
return _is_dtype_type(arr_or_dtype, classes(np.floating))
1247+
return _is_dtype_type(arr_or_dtype, classes(np.floating)) or _is_dtype(
1248+
arr_or_dtype, lambda typ: isinstance(typ, ExtensionDtype) and typ.kind in "f"
1249+
)
12381250

12391251

12401252
def is_bool_dtype(arr_or_dtype) -> bool:

pandas/tests/extension/test_arrow.py

+37
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
import pandas._testing as tm
4040
from pandas.api.types import (
4141
is_bool_dtype,
42+
is_float_dtype,
43+
is_integer_dtype,
4244
is_numeric_dtype,
45+
is_signed_integer_dtype,
46+
is_unsigned_integer_dtype,
4347
)
4448
from pandas.tests.extension import base
4549

@@ -1446,6 +1450,39 @@ def test_is_numeric_dtype(data):
14461450
assert not is_numeric_dtype(data)
14471451

14481452

1453+
def test_is_integer_dtype(data):
1454+
# GH 50667
1455+
pa_type = data.dtype.pyarrow_dtype
1456+
if pa.types.is_integer(pa_type):
1457+
assert is_integer_dtype(data)
1458+
else:
1459+
assert not is_integer_dtype(data)
1460+
1461+
1462+
def test_is_signed_integer_dtype(data):
1463+
pa_type = data.dtype.pyarrow_dtype
1464+
if pa.types.is_signed_integer(pa_type):
1465+
assert is_signed_integer_dtype(data)
1466+
else:
1467+
assert not is_signed_integer_dtype(data)
1468+
1469+
1470+
def test_is_unsigned_integer_dtype(data):
1471+
pa_type = data.dtype.pyarrow_dtype
1472+
if pa.types.is_unsigned_integer(pa_type):
1473+
assert is_unsigned_integer_dtype(data)
1474+
else:
1475+
assert not is_unsigned_integer_dtype(data)
1476+
1477+
1478+
def test_is_float_dtype(data):
1479+
pa_type = data.dtype.pyarrow_dtype
1480+
if pa.types.is_floating(pa_type):
1481+
assert is_float_dtype(data)
1482+
else:
1483+
assert not is_float_dtype(data)
1484+
1485+
14491486
def test_pickle_roundtrip(data):
14501487
# GH 42600
14511488
expected = pd.Series(data)

0 commit comments

Comments
 (0)