Skip to content
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

BUG: pandas-1.4.0 .loc "shallow" copies do not update #45743

Closed
3 tasks done
slackline opened this issue Jan 31, 2022 · 2 comments
Closed
3 tasks done

BUG: pandas-1.4.0 .loc "shallow" copies do not update #45743

slackline opened this issue Jan 31, 2022 · 2 comments

Comments

@slackline
Copy link

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"]are2.340512and0.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

pd.show_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

@slackline slackline added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 31, 2022
@jbrockmendel
Copy link
Member

The .loc views are behaving correctly. The change is that df["a"] = df["a"] * 2 is now creating a new array instead of writing into the existing array. See https://pandas.pydata.org/pandas-docs/stable/whatsnew/v1.4.0.html#bug-fixes Bug in DataFrame.__setitem__() incorrectly writing into an existing column’s array rather than setting a new array when the new dtype and the old dtype match (GH43406)

Setting with df.loc[:, "a"] = ... should be inplace. Trouble is that behavior is inconsistent until #45333.

@slackline
Copy link
Author

slackline commented Jan 31, 2022

Cool, thanks for the quick response and my apologies for not linking it to #43406 and other associated issues.

I did search through the bug-fixes of 1.4.0 and issues but was looking for mention of .loc and .copy() behaviour and didn't make the connection to DataFrame.__setitem__(), again my apologies.

Glad to see everything is in hand, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants