Skip to content

BUG: KeyError when a series popped from data frame with bool indexer #42644

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 5 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ def _maybe_update_cacher(
if ref is None:
del self._cacher
else:
if len(self) == len(ref):
if len(self) == len(ref) and self.name in ref.columns: # GH#42530
Copy link
Contributor

Choose a reason for hiding this comment

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

don't put the comment after the line, makes this very hard to read. you can add in the if block with an explanation.

would be ok making this an if/elif/else block

# otherwise, either self or ref has swapped in new arrays
ref._maybe_cache_changed(cacher[0], self)
else:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pandas import (
Categorical,
DataFrame,
DatetimeIndex,
Index,
IntervalIndex,
Expand Down Expand Up @@ -945,3 +946,17 @@ def test_setitem_int_as_positional_fallback_deprecation():
with tm.assert_produces_warning(FutureWarning, match=msg):
ser3[4] = 99
tm.assert_series_equal(ser3, expected3)


def test_setitem_with_bool_indexer():
# GH#42530

df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
result = df.pop("b")
result[[True, False, False]] = 9
expected = Series(data=[9, 5, 6], index=range(3), name="b")
tm.assert_series_equal(result, expected)

df["a"][[True, False, False]] = 10
expected_df = DataFrame({"a": [10, 2, 3]})
tm.assert_frame_equal(df, expected_df)