Skip to content

Commit 9aeceb0

Browse files
mroeschkemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#48169: BUG: DataFrame.select_dtypes(include=number) still included BooleanDtype
1 parent 75d152c commit 9aeceb0

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v1.4.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717
- Fixed regression in taking NULL :class:`objects` from a :class:`DataFrame` causing a segmentation violation. These NULL values are created by :meth:`numpy.empty_like` (:issue:`46848`)
1818
- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`)
1919
- Fixed regression in :func:`cut` using a ``datetime64`` IntervalIndex as bins (:issue:`46218`)
20+
- Fixed regression in :meth:`DataFrame.select_dtypes` where ``include="number"`` included :class:`BooleanDtype` (:issue:`46870`)
2021
- Fixed regression in :meth:`DataFrame.loc` not updating the cache correctly after values were set (:issue:`47867`)
2122
- Fixed regression in :meth:`DataFrame.loc` not aligning index in some cases when setting a :class:`DataFrame` (:issue:`47578`)
2223
- Fixed regression in :meth:`DataFrame.loc` setting a length-1 array like value to a single value in the DataFrame (:issue:`46268`)

pandas/core/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4359,8 +4359,11 @@ def check_int_infer_dtype(dtypes):
43594359
raise ValueError(f"include and exclude overlap on {(include & exclude)}")
43604360

43614361
def dtype_predicate(dtype: DtypeObj, dtypes_set) -> bool:
4362+
# GH 46870: BooleanDtype._is_numeric == True but should be excluded
43624363
return issubclass(dtype.type, tuple(dtypes_set)) or (
4363-
np.number in dtypes_set and getattr(dtype, "_is_numeric", False)
4364+
np.number in dtypes_set
4365+
and getattr(dtype, "_is_numeric", False)
4366+
and not is_bool_dtype(dtype)
43644367
)
43654368

43664369
def predicate(arr: ArrayLike) -> bool:

pandas/tests/frame/methods/test_select_dtypes.py

+15
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,18 @@ def test_select_dtypes_float_dtype(self, expected, float_dtypes):
441441
df = df.astype(dtype_dict)
442442
result = df.select_dtypes(include=float_dtypes)
443443
tm.assert_frame_equal(result, expected)
444+
445+
def test_np_bool_ea_boolean_include_number(self):
446+
# GH 46870
447+
df = DataFrame(
448+
{
449+
"a": [1, 2, 3],
450+
"b": pd.Series([True, False, True], dtype="boolean"),
451+
"c": np.array([True, False, True]),
452+
"d": pd.Categorical([True, False, True]),
453+
"e": pd.arrays.SparseArray([True, False, True]),
454+
}
455+
)
456+
result = df.select_dtypes(include="number")
457+
expected = DataFrame({"a": [1, 2, 3]})
458+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)