Skip to content

BUG: DataFrame._item_cache not cleared on on .copy() #31784

Closed
@tommontroy

Description

@tommontroy

Code Sample, a copy-pastable example if possible

import copy
import pandas as pd
import platform

print('Python Version', platform.python_version())
print('Pandas Version', pd.version)

a = [1.]

d = {}
d['a'] = a
df = pd.DataFrame(d)

print('Initial data frame')
print(df)
print('------')

# This statement must be before the print statement
# Removing this causes the bug to disappear
df['x'] = 0.0

# Removing this causes the bug to disappear
print("df['a'].values=", df['a'].values)
print('-----')

# Removing the copy causes the bug to disappear
# copy.deepcopy(df) or df.copy() leads to the same result
#df1 = copy.deepcopy(df)
df1 = df.copy()

# set the first element of 'a' to value
value = -1.0
df['a'].values[0] = value

print("After setting df['a'].values[0] = %f" % (value))
print(df)
print('-------')

print("df['a'].values=", df['a'].values)
print('-------')

# Removing this causes the bug to disappear
df['y'] = 0.0

print('Final')
print(df)
print('-------')

print("df['a'].values=", df['a'].values)

Problem description

First, we build a dataframe from a dictionary containing a list.

After that, we add a column, do add some print statements, copy the dataframe. The addition of the column and print statement are crucial to the bug showing up. The fact that the print statement matters leads me to believe that it's memory issue.

The critical point is that we set df['a'].values[0] = -1.0 which is somehow not permanent.

Lastly we add another column. This column addition is necessary for the issue to occur.

In short the issue looks like this:

a = [1.0]

df = pd.DataFrame({'a': a})
df['x'] = 0.0 # necessary for bug
print(df['a'].values) # necessary for bug
df1 = df.copy()
df['a'].values[0] = -1.0 # this is the statement which is ignored
print(df['a'].values)
print(df)
df['y'] = 0.0 # necessary for bug
print(df['a'].values)

At this point, we'd expect df['a'].values = [-1.0], but in fact df['a'].values == 1.0

I've tested this issue on a wide variety of versions. The last version where it works as expected is 0.16.2 and the version with the unexpected result is 0.17.1. I've tried this with python 3.6 / pandas=1.0.0 on Linux and python 3.7 / pandas 1.0.1 on OS X and get the same result.

INSTALLED VERSIONS ------------------ commit : None python : 3.7.0.final.0 python-bits : 64 OS : Darwin OS-release : 17.7.0 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.15.1
pytz : 2019.3
dateutil : 2.7.3
pip : 18.0
setuptools : 40.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10
IPython : 6.5.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None
None

Metadata

Metadata

Assignees

Labels

BugIndexingRelated to indexing on series/frames, not to indexes themselvesReshapingConcat, Merge/Join, Stack/Unstack, Explode

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions