Under the copy-on-write semantics of pandas 2.x, df[df.a > 0]['b'] = 1 is executed. What is the effect?
Correct answer
B. Nothing changes in df, because the chained assignment writes to a temporary copy
15 views
B. Nothing changes in df, because the chained assignment writes to a temporary copy
15 views
Copy-on-write makes every derived object a lazy view, so assigning into a temporary such as df[mask]['b'] modifies a copy that is then discarded and the parent stays untouched. Chained assignment is therefore never a valid way to update data; use df.loc[df.a > 0, 'b'] = 1. In pandas 3.0 the behaviour is the default, with no warning to alert you.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is B? Ask here and get an explanation.