-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: DataFrame.__setitem__ sometimes operating inplace #43406
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make a user note of when this is changed, its not obvious at all.
cc @phofl |
@jorisvandenbossche @phofl gentle ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lgtm in general. I think we should add tests to cover this:
@pytest.mark.parametrize("indexer", ["a", ["a"], [True, False]])
@pytest.mark.parametrize(
"value, set_value",
[
(1, 5),
(1.0, 5.0),
(Timestamp("2020-12-31"), Timestamp("2021-12-31")),
("a", "b"),
],
)
def test_setitem_not_operating_inplace(value, set_value, indexer):
df = DataFrame({"a": value}, index=[0, 1])
expected = df.copy()
view = df[:]
df[indexer] = set_value
tm.assert_frame_equal(view, expected)
It looks like as the boolean indexer case is still operating inplace
@jorisvandenbossche gentle ping |
@jreback gentle ping, doesn't sound like the others are weighing in |
I think this looks good. Have to figure out the boolean case in the future, but otherwise is good |
let me look |
id like to get this in before tackling #43996 |
gentle ping; has a bearing on #40380 |
cool, ideally can simplify the caching checks at some point (e.g. the maybe_update_cacher is getting more complicated). |
Lines 4841 to 4845 in 965ceca
Did this change intend to modify |
@SimeonStoykovQC no, |
@SimeonStoykovQC I misunderstood what you meant. You meant to use |
No, the assign change is intended. |
It caused a 10x performance regression in some of our code. We will try to come up with a reproducer. |
In the meantime, could you elaborate? What's wrong with in-place allocations if |
Yeah I know, but this was fixed for 2.0, we had a nasty performance bottleneck in 1.5 unfortunately. Everything is back to normal with 2.0 |
The fixed bug is the currently-xfailed test test_setitem_same_dtype_not_inplace (see #39510, which has its own subsection in the 1.3.0 release notes). Pretty much all the other altered tests are changed because those tests rely on the incorrect behavior.
The main non-test change is the addition of an
inplace
check inBlockManager.iset
. We want inplace to be False when this is reached viaDataFrame.__setitem__
and True when reached vialoc/iloc.__setitem__