Skip to content

Commit 5dba4cc

Browse files
committed
extra check when casting to boolean
1 parent e3a3a4a commit 5dba4cc

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pandas/core/dtypes/cast.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,17 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
19181918
# i.e. there are pd.NA elements
19191919
raise LossySetitemError
19201920
return element
1921+
with np.errstate(invalid="ignore"):
1922+
# We check afterwards if cast was losslessly, so no need to show
1923+
# the warning
1924+
casted = element.astype(dtype)
1925+
comp = casted == element
1926+
if comp.all():
1927+
# Return the casted values bc they can be passed to
1928+
# np.putmask, whereas the raw values cannot.
1929+
return casted
19211930
raise LossySetitemError
1931+
19221932
if lib.is_bool(element):
19231933
return element
19241934
raise LossySetitemError

0 commit comments

Comments
 (0)