Change column in-place with copy-on-write

203 Views Asked by At

I have a large DataFrame. I have enabled copy_on_write, as is expected to be the default for 3.0. I want to limit the values of some of the columns (in-place). Like this:

import pandas as pd

pd.options.mode.copy_on_write = True
df = pd.DataFrame({'a': '.', 'x': [1, 2, 3]})
df['x'].clip(lower=0, inplace=True)

This gives me a ChainedAssignmentError:

A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
When using the Copy-on-Write mode, such inplace method never works to update the original DataFrame or Series, because the intermediate object on which we are setting values always behaves as a copy.

For example, when doing df[col].method(value, inplace=True), try using df.method({col: value}, inplace=True) instead, to perform the operation inplace on the original object.

How is this supposed to work? I tried variations of the suggestion in the last paragraph and other things, but I keep getting the error.

0

There are 0 best solutions below