From 2827637d7fbed9d919cba9e5290d40fbec5dafe5 Mon Sep 17 00:00:00 2001 From: robbie Date: Tue, 25 Jan 2022 12:34:33 +0000 Subject: [PATCH 1/5] Add categorical Series to test_is_bool_dtype --- pandas/tests/dtypes/test_common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 165873186aa5a..95abf1513967e 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -562,6 +562,7 @@ def test_is_bool_dtype(): assert not com.is_bool_dtype(int) assert not com.is_bool_dtype(str) assert not com.is_bool_dtype(pd.Series([1, 2])) + assert not com.is_bool_dtype(pd.Series(["a", "b"], dtype="category")) assert not com.is_bool_dtype(np.array(["a", "b"])) assert not com.is_bool_dtype(pd.Index(["a", "b"])) assert not com.is_bool_dtype("Int64") From d96c7cad5875c091a2f0c55322f388b6ede51be5 Mon Sep 17 00:00:00 2001 From: robbie Date: Tue, 25 Jan 2022 13:57:29 +0000 Subject: [PATCH 2/5] Fix is_bool_dtype to work with categorical Series Categories are taken from dtype.categories instead of trying to get them from series.categories --- pandas/core/dtypes/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 109073c7511d0..6776064342db0 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1319,7 +1319,7 @@ def is_bool_dtype(arr_or_dtype) -> bool: return False if isinstance(dtype, CategoricalDtype): - arr_or_dtype = arr_or_dtype.categories + arr_or_dtype = dtype.categories # now we use the special definition for Index if isinstance(arr_or_dtype, ABCIndex): From c83c22f3222d4e740d1b11abe2b950f5ef3a9502 Mon Sep 17 00:00:00 2001 From: robbie Date: Tue, 25 Jan 2022 14:04:35 +0000 Subject: [PATCH 3/5] Add record of bugfix to v1.4.1 release notes --- doc/source/whatsnew/v1.4.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.1.rst b/doc/source/whatsnew/v1.4.1.rst index 79dae514b77e9..fae068d26f5ec 100644 --- a/doc/source/whatsnew/v1.4.1.rst +++ b/doc/source/whatsnew/v1.4.1.rst @@ -24,7 +24,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Fixed segfault in :meth:``DataFrame.to_json`` when dumping tz-aware datetimes in Python 3.10 (:issue:`42130`) -- +- Bug in :meth:`common.is_bool_dtype` was raising an AttributeError when evaluating a categorical Series (:issue:`45615`) .. --------------------------------------------------------------------------- From 044ed4df54c8cc8e3b219b7e0c6e6c27198150f3 Mon Sep 17 00:00:00 2001 From: robbie Date: Fri, 28 Jan 2022 10:30:02 +0000 Subject: [PATCH 4/5] Add test case using boolean categorical Series for is_bool_dtype --- pandas/tests/dtypes/test_common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 95abf1513967e..a32b37fbdd71b 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -569,6 +569,7 @@ def test_is_bool_dtype(): assert com.is_bool_dtype(bool) assert com.is_bool_dtype(np.bool_) + assert com.is_bool_dtype(pd.Series([True, False], dtype="category")) assert com.is_bool_dtype(np.array([True, False])) assert com.is_bool_dtype(pd.Index([True, False])) From dcf2b46b9f65283432b44c4ba5df150ba93f4deb Mon Sep 17 00:00:00 2001 From: robbie Date: Fri, 28 Jan 2022 10:30:28 +0000 Subject: [PATCH 5/5] Correct function reference in whatsnew docs --- doc/source/whatsnew/v1.4.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.1.rst b/doc/source/whatsnew/v1.4.1.rst index fae068d26f5ec..be205e50f5afa 100644 --- a/doc/source/whatsnew/v1.4.1.rst +++ b/doc/source/whatsnew/v1.4.1.rst @@ -24,7 +24,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Fixed segfault in :meth:``DataFrame.to_json`` when dumping tz-aware datetimes in Python 3.10 (:issue:`42130`) -- Bug in :meth:`common.is_bool_dtype` was raising an AttributeError when evaluating a categorical Series (:issue:`45615`) +- Bug in :meth:`api.types.is_bool_dtype` was raising an AttributeError when evaluating a categorical Series (:issue:`45615`) .. ---------------------------------------------------------------------------