Description
Pandas version checks
-
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 main branch of pandas.
Reproducible Example
import pandas as pd
from numpy.random import default_rng
rng = default_rng(20220125)
N = 10
original = pd.DataFrame({'a': rng.standard_normal(N),
'b': rng.integers(low=0, high=100, size=N),
})
print(f'Pandas Version : {pd.__version__}')
print(f'===========================')
print(f'Original :\n{original}\n')
copy_plain = original.loc[1:2]
copy_shallow = original.loc[1:2].copy(deep=False)
copy_deep = original.loc[1:2].copy(deep=True)
def print_df(df, description):
"""Check the data frame."""
print(f'===========================')
print(f'{description} : \n{df}')
print_df(original.loc[1:2], 'Original slice')
print_df(copy_plain, 'Plain')
print_df(copy_shallow, 'Shallow')
print_df(copy_deep, 'Deep')
print(f'Make a change to original and see if it cascades through')
print(f'original["a"] = original["a"]**2')
original['a'] = original['a']**2
print_df(original.loc[1:2], 'Original slice')
print_df(copy_plain, 'Plain')
print_df(copy_shallow, 'Shallow')
print_df(copy_deep, 'Deep')
Issue Description
When taking a "shallow" slice of a dataframe with .loc
subsequent updates should be reflected in the original and vice-versa, this is no longer the case under Pandas 1.4.0 as demonstrated running the reproducible example...
Pandas Version : 1.4.0
===========================
Original :
a b
0 0.778724 58
1 1.529873 40
2 -0.328487 63
3 -0.090816 66
4 0.451966 82
5 1.648603 65
6 1.161088 92
7 0.058186 48
8 0.800209 81
9 1.783765 4
===========================
Original slice :
a b
1 1.529873 40
2 -0.328487 63
===========================
Plain :
a b
1 1.529873 40
2 -0.328487 63
===========================
Shallow :
a b
1 1.529873 40
2 -0.328487 63
===========================
Deep :
a b
1 1.529873 40
2 -0.328487 63
Make a change to original and see if it cascades through
original["a"] = original["a"]**2
===========================
Original slice :
a b
1 2.340512 40
2 0.107903 63
===========================
Plain :
a b
1 1.529873 40
2 -0.328487 63
===========================
Shallow :
a b
1 1.529873 40
2 -0.328487 63
===========================
Deep :
a b
1 1.529873 40
2 -0.328487 63
After original["a"] = original["a"]**2 the values at
.loc[1:2,"a"]are
2.340512and
0.107903 and should be the same under a "
Plain" copy and "
Shallow`" copy as the documentation states only a "deep" copy should take a snapshot of the original and shallow copies share data and index with original..
Expected Behavior
The behaviour under 1.3.5 is as described in the manual, after modifying the original a "plain" and "shallow" copy reflect the modifications...
Pandas Version : 1.3.5
===========================
Original :
a b
0 0.778724 58
1 1.529873 40
2 -0.328487 63
3 -0.090816 66
4 0.451966 82
5 1.648603 65
6 1.161088 92
7 0.058186 48
8 0.800209 81
9 1.783765 4
===========================
Original slice :
a b
1 1.529873 40
2 -0.328487 63
===========================
Plain :
a b
1 1.529873 40
2 -0.328487 63
===========================
Shallow :
a b
1 1.529873 40
2 -0.328487 63
===========================
Deep :
a b
1 1.529873 40
2 -0.328487 63
Make a change to original and see if it cascades through
original["a"] = original["a"]**2
===========================
Original slice :
a b
1 2.340512 40
2 0.107903 63
===========================
Plain :
a b
1 2.340512 40
2 0.107903 63
===========================
Shallow :
a b
1 2.340512 40
2 0.107903 63
===========================
Deep :
a b
1 1.529873 40
2 -0.328487 63
Installed Versions
INSTALLED VERSIONS
commit : bb1f651
python : 3.8.12.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-123-generic
Version : #126-Ubuntu SMP Wed Oct 21 09:40:11 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.4.0
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 60.5.0
Cython : 0.29.26
pytest : 6.2.5
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.7.1
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fastparquet : None
fsspec : 2022.01.0
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : 1.4.31
tables : None
tabulate : 0.8.9
xarray : 0.20.2
xlrd : 2.0.1
xlwt : None
zstandard : None