Skip to content

BUG: Series[bool].__setitem__(scalar, non_bool) #39478

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

Merged
merged 6 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1904,12 +1904,15 @@ def validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None:
):
raise ValueError("Cannot assign nan to integer series")

if dtype.kind in ["i", "u", "f", "c"]:
elif dtype.kind in ["i", "u", "f", "c"]:
if is_bool(value) or isinstance(value, np.timedelta64):
# numpy will cast td64 to integer if we're not careful
raise ValueError(
f"Cannot assign {type(value).__name__} to float/integer series"
)
elif dtype.kind == "b":
if is_scalar(value) and not is_bool(value):
raise ValueError(f"Cannot assign {type(value).__name__} to bool series")


def can_hold_element(dtype: np.dtype, element: Any) -> bool:
Expand Down
29 changes: 7 additions & 22 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,34 +161,19 @@ def test_setitem_series_complex128(self, val, exp_dtype):
@pytest.mark.parametrize(
"val,exp_dtype",
[
(1, np.int64),
(3, np.int64),
(1.1, np.float64),
(1 + 1j, np.complex128),
(1, object),
("3", object),
(3, object),
(1.1, object),
(1 + 1j, object),
(True, np.bool_),
],
)
def test_setitem_series_bool(self, val, exp_dtype, request):
def test_setitem_series_bool(self, val, exp_dtype):
obj = pd.Series([True, False, True, False])
assert obj.dtype == np.bool_

mark = None
if exp_dtype is np.int64:
exp = pd.Series([True, True, True, False])
self._assert_setitem_series_conversion(obj, val, exp, np.bool_)
mark = pytest.mark.xfail(reason="TODO_GH12747 The result must be int")
elif exp_dtype is np.float64:
exp = pd.Series([True, True, True, False])
self._assert_setitem_series_conversion(obj, val, exp, np.bool_)
mark = pytest.mark.xfail(reason="TODO_GH12747 The result must be float")
elif exp_dtype is np.complex128:
exp = pd.Series([True, True, True, False])
self._assert_setitem_series_conversion(obj, val, exp, np.bool_)
mark = pytest.mark.xfail(reason="TODO_GH12747 The result must be complex")
if mark is not None:
request.node.add_marker(mark)

exp = pd.Series([True, val, True, False])
exp = pd.Series([True, val, True, False], dtype=exp_dtype)
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)

@pytest.mark.parametrize(
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ def test_setitem_dt64_into_int_series(self, dtype):
ser[:-1] = np.array([val, val])
tm.assert_series_equal(ser, expected)

@pytest.mark.parametrize("unique", [True, False])
@pytest.mark.parametrize("val", [3, 3.0, "3"], ids=type)
def test_setitem_non_bool_into_bool(self, val, indexer_sli, unique):
# dont cast these 3-like values to bool
ser = Series([True, False])
if not unique:
ser.index = [1, 1]

indexer_sli(ser)[1] = val
assert type(ser.iloc[1]) == type(val)

expected = Series([True, val], dtype=object, index=ser.index)
if not unique and indexer_sli is not tm.iloc:
expected = Series([val, val], dtype=object, index=[1, 1])
tm.assert_series_equal(ser, expected)


class SetitemCastingEquivalents:
"""
Expand Down