-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Setting data of same dtype as existing column in shallow copy DF causes change to propagate to original DF #43965
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
Comments
This behaves exactly as documented:
|
I understand the intended behaviour of a shallow copy. If the underlying data of the original / shallow copy is mutated, it will be reflected in both. |
Setitem operates inplace if dtype is the same and makes a copy if not. There is an open pr addressing this. It should always make a copy |
Yes exactly, thats the issue I'm raising |
Yeah but this is independent of shallow copy then. Was/is a setitem bug, but should be fixed in 1.4 then |
I guess shallow copy brings the behaviour to light, otherwise with a standalone DF it is not obvious to see/know whether the setitem is changing data in place or changing reference to new data |
You can do something like this:
The statement |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the master branch of pandas.
Reproducible Example
Issue Description
Setting data of same dtype as existing column in shallow DF copy causes change to propagate to original DF. However this does not happen if new data has different dtype.
Expected Behavior
Assigning column data in DF shallow copy should not also cause change in original DF (regardless of dtype)
Installed Versions
INSTALLED VERSIONS
commit : f2c8480
python : 3.9.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Australia.1252
pandas : 1.2.3
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 49.2.1
Cython : 0.29.24
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.25.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.54.0
The text was updated successfully, but these errors were encountered: