Skip to content

Commit b8a5b36

Browse files
Backport PR #38427: REGR: Assigning label with registered EA dtype raises (#38464)
Co-authored-by: Richard Shadrach <[email protected]>
1 parent afa9ebd commit b8a5b36

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ Other
858858
- Bug in :meth:`Index.drop` raising ``InvalidIndexError`` when index has duplicates (:issue:`38051`)
859859
- Bug in :meth:`RangeIndex.difference` returning :class:`Int64Index` in some cases where it should return :class:`RangeIndex` (:issue:`38028`)
860860
- Fixed bug in :func:`assert_series_equal` when comparing a datetime-like array with an equivalent non extension dtype array (:issue:`37609`)
861-
861+
- Bug in :func:`.is_bool_dtype` would raise when passed a valid string such as ``"boolean"`` (:issue:`38386`)
862862

863863

864864
.. ---------------------------------------------------------------------------

pandas/core/dtypes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ def is_bool_dtype(arr_or_dtype) -> bool:
13971397
# guess this
13981398
return arr_or_dtype.is_object and arr_or_dtype.inferred_type == "boolean"
13991399
elif is_extension_array_dtype(arr_or_dtype):
1400-
return getattr(arr_or_dtype, "dtype", arr_or_dtype)._is_boolean
1400+
return getattr(dtype, "_is_boolean", False)
14011401

14021402
return issubclass(dtype.type, np.bool_)
14031403

pandas/io/parsers.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -1689,9 +1689,8 @@ def _convert_to_ndarrays(
16891689
values, set(col_na_values) | col_na_fvalues, try_num_bool=False
16901690
)
16911691
else:
1692-
is_str_or_ea_dtype = is_string_dtype(
1693-
cast_type
1694-
) or is_extension_array_dtype(cast_type)
1692+
is_ea = is_extension_array_dtype(cast_type)
1693+
is_str_or_ea_dtype = is_ea or is_string_dtype(cast_type)
16951694
# skip inference if specified dtype is object
16961695
# or casting to an EA
16971696
try_num_bool = not (cast_type and is_str_or_ea_dtype)
@@ -1706,16 +1705,15 @@ def _convert_to_ndarrays(
17061705
not is_dtype_equal(cvals, cast_type)
17071706
or is_extension_array_dtype(cast_type)
17081707
):
1709-
try:
1710-
if (
1711-
is_bool_dtype(cast_type)
1712-
and not is_categorical_dtype(cast_type)
1713-
and na_count > 0
1714-
):
1715-
raise ValueError(f"Bool column has NA values in column {c}")
1716-
except (AttributeError, TypeError):
1717-
# invalid input to is_bool_dtype
1718-
pass
1708+
if not is_ea and na_count > 0:
1709+
try:
1710+
if is_bool_dtype(cast_type):
1711+
raise ValueError(
1712+
f"Bool column has NA values in column {c}"
1713+
)
1714+
except (AttributeError, TypeError):
1715+
# invalid input to is_bool_dtype
1716+
pass
17191717
cvals = self._cast_types(cvals, cast_type, c)
17201718

17211719
result[c] = cvals

pandas/tests/dtypes/test_common.py

+2
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ def test_is_bool_dtype():
545545
assert not com.is_bool_dtype(pd.Series([1, 2]))
546546
assert not com.is_bool_dtype(np.array(["a", "b"]))
547547
assert not com.is_bool_dtype(pd.Index(["a", "b"]))
548+
assert not com.is_bool_dtype("Int64")
548549

549550
assert com.is_bool_dtype(bool)
550551
assert com.is_bool_dtype(np.bool_)
@@ -553,6 +554,7 @@ def test_is_bool_dtype():
553554

554555
assert com.is_bool_dtype(pd.BooleanDtype())
555556
assert com.is_bool_dtype(pd.array([True, False, None], dtype="boolean"))
557+
assert com.is_bool_dtype("boolean")
556558

557559

558560
@pytest.mark.filterwarnings("ignore:'is_extension_type' is deprecated:FutureWarning")

pandas/tests/frame/indexing/test_setitem.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.core.dtypes.base import registry as ea_registry
45
from pandas.core.dtypes.dtypes import DatetimeTZDtype, IntervalDtype, PeriodDtype
56

67
from pandas import (
@@ -197,6 +198,25 @@ def test_setitem_extension_types(self, obj, dtype):
197198

198199
tm.assert_frame_equal(df, expected)
199200

201+
@pytest.mark.parametrize(
202+
"ea_name",
203+
[
204+
dtype.name
205+
for dtype in ea_registry.dtypes
206+
# property would require instantiation
207+
if not isinstance(dtype.name, property)
208+
]
209+
# mypy doesn't allow adding lists of different types
210+
# https://github.com/python/mypy/issues/5492
211+
+ ["datetime64[ns, UTC]", "period[D]"], # type: ignore[list-item]
212+
)
213+
def test_setitem_with_ea_name(self, ea_name):
214+
# GH 38386
215+
result = DataFrame([0])
216+
result[ea_name] = [1]
217+
expected = DataFrame({0: [0], ea_name: [1]})
218+
tm.assert_frame_equal(result, expected)
219+
200220
def test_setitem_dt64_ndarray_with_NaT_and_diff_time_units(self):
201221
# GH#7492
202222
data_ns = np.array([1, "nat"], dtype="datetime64[ns]")

0 commit comments

Comments
 (0)