Skip to content

BUG: Assigning boolean series with logical indexer #60127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
7 changes: 6 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
TimedeltaArray,
)


_int8_max = np.iinfo(np.int8).max
_int16_max = np.iinfo(np.int16).max
_int32_max = np.iinfo(np.int32).max
Expand Down Expand Up @@ -1918,7 +1917,13 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
# i.e. there are pd.NA elements
raise LossySetitemError
return element
# GH 57338
# Check boolean array set as object type
comp = [lib.is_bool(e) for e in np.array([element]).ravel()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this implementation can be very slow. What is this trying to achieve?

Copy link
Contributor Author

@SpoopyPillow SpoopyPillow Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'm trying to avoid sending a LossySetitemError when I want to cast an array of booleans that is set as an object type. So if I passed in array([True, False, True], dtype=object) to cast to a boolean dtype, the original code would raise a LossySetitemError. I basically just go through the array and check make sure each element is a boolean.

I had originally tried doing something similar to the other if statements (for different types) where they did casted = dtype.type(element) and directly checked using casted == element, but that doesn't work for boolean arrays because (array([1, 1, 1]) == array([True, True, True])).all() is True

if all(comp):
return element.astype("bool")
raise LossySetitemError

if lib.is_bool(element):
return element
raise LossySetitemError
Expand Down
Loading