Here's my example:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
In [3]: df1 = df.rename(columns={'b': 'b'}, copy=False)
In [4]: df1.isetitem(1, [7,8,9])
In [5]: df
Out[5]:
a b
0 1 4
1 2 5
2 3 6
In [6]: df1
Out[6]:
a b
0 1 7
1 2 8
2 3 9
If df1 was derived from df with copy=False, then I'd have expected an in-place modification of df1 to also affect df. But it doesn't. Why?
I'm using pandas version 2.2.1, with no options (e.g. copy-on-write) enabled
copy=False means that the underlying data (numpy) is shared.
If we modify one item of the underlying numpy array, it is reflected since the data is shared:
df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
df1 = df.rename(columns={'b': 'c'}, copy=False)
df1.values[0, 1] = 999
print(df)
a b
0 1 999
1 2 5
2 3 6
print(df1)
a c
0 1 999
1 2 5
2 3 6
print(id(df.values), id(df1.values))
# 140335530654768 140335530654768
The DataFrame objects (containers, indices, column names…) are however not identical.
print(id(df), id(df1))
# 140335696175520 140335696171536
By setting a new column with df1.isetitem(1, [7,8,9]) you just add a new Series and force a copy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With