Skip to content

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

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

Closed
tommontroy opened this issue Feb 7, 2020 · 3 comments · Fixed by #33299
Closed

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

tommontroy opened this issue Feb 7, 2020 · 3 comments · Fixed by #33299
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@tommontroy
Copy link

tommontroy commented Feb 7, 2020

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

@jbrockmendel
Copy link
Member

It looks like the issue is that df._item_cache is not being invalidated correctly after the df.copy() call performs consolidation. A PR to fix this would be welcome.

@jbrockmendel jbrockmendel changed the title BUG: Potential memory leak with column values getting reset indirectly BUG: DataFrame._item_cache not cleared on on .copy() Mar 24, 2020
@neilkg
Copy link
Contributor

neilkg commented Mar 31, 2020

take

@jreback jreback added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Apr 5, 2020
@jreback jreback added this to the 1.1 milestone Apr 5, 2020
@jreback jreback added the Indexing Related to indexing on series/frames, not to indexes themselves label Apr 5, 2020
@tommontroy
Copy link
Author

Thanks for pushing this through.

Best regards,

-Tom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants