Skip to content

BUG: Setting data of same dtype as existing column in shallow copy DF causes change to propagate to original DF #43965

Closed
@Finndersen

Description

@Finndersen

  • 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

# Setting column data with different dtype on shallow copy causes original to be unchanged
original = pd.DataFrame({'a': ['initial string data']})
shallow = original.copy(deep=False)
shallow['a'] = 5   # Set column to integer dtype
original['a'][0]
>> 'initial string data'

# Whereas if new data has same dtype as existing, original DF is updated too
original = pd.DataFrame({'a': ['initial string data']})
shallow = original.copy(deep=False)
shallow['a'] = 'new string data'  # Set column to different str/object value
original['a'][0]
>> 'new string data'

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closing CandidateMay be closeable, needs more eyeballsIndexingRelated to indexing on series/frames, not to indexes themselvesUsage Question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions