Skip to content

BUG: Invalid cache invalidation leading to assertion errors later #35739

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
2 of 3 tasks
veselyvojtech opened this issue Aug 15, 2020 · 6 comments
Closed
2 of 3 tasks
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@veselyvojtech
Copy link

veselyvojtech commented Aug 15, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

In [44]: df = pd.DataFrame({"a": [3, 4]})

In [45]: df
Out[45]: 
   a
0  3
1  4

In [46]: mask = df["a"] > 0

In [47]: mask
Out[47]: 
0    True
1    True
Name: a, dtype: bool

In [48]: df_a = df.loc[:, "a"]

In [49]: df_a["a"] = 1

In [50]: df_a
Out[50]: 
0    3
1    4
a    1
Name: a, dtype: int64

In [51]: df.__dict__
Out[51]: 
{'_is_copy': None,
 '_mgr': BlockManager
 Items: Index(['a'], dtype='object')
 Axis 1: RangeIndex(start=0, stop=2, step=1)
 IntBlock: slice(0, 1, 1), 1 x 2, dtype: int64,
 '_item_cache': {'a': 0    3
  1    4
  a    1
  Name: a, dtype: int64},
 '_attrs': {}}

In [52]: mask
Out[52]: 
0    True
1    True
Name: a, dtype: bool

In [53]: df
Out[53]: 
   a
0  3
1  4

In [54]: df.loc[mask, "a"]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-54-e9cf788ed9f4> in <module>
----> 1 df.loc[mask, "a"]

~/miniconda3/envs/conduit-poetry/lib/python3.8/site-packages/pandas/core/indexing.py in __getitem__(self, key)
    871                     # AttributeError for IntervalTree get_value
    872                     pass
--> 873             return self._getitem_tuple(key)
    874         else:
    875             # we by definition only have the 0th axis

~/miniconda3/envs/conduit-poetry/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
   1053             return self._multi_take(tup)
   1054 
-> 1055         return self._getitem_tuple_same_dim(tup)
   1056 
   1057     def _get_label(self, label, axis: int):

~/miniconda3/envs/conduit-poetry/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_tuple_same_dim(self, tup)
    751             # We should never have retval.ndim < self.ndim, as that should
    752             #  be handled by the _getitem_lowerdim call above.
--> 753             assert retval.ndim == self.ndim
    754 
    755         return retval

AssertionError: 

Problem description

By manipulating one column that was created by copying a part of the DataFrame, the cache for this one column is updated in the DataFrame. However, the cached data are not compatible with the DataFrame index and causes problems later, e.g. the AssertionError from the example.

Expected Output

Manipulating separate object would not influence the original object in a way that leads to AssertionError.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : d9fff2792bf16178d4e450fe7384244e50635733
python           : 3.8.3.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.7.14-200.fc32.x86_64
Version          : #1 SMP Fri Aug 7 23:16:37 UTC 2020
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.1.0
numpy            : 1.19.1
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 20.1.1
setuptools       : 49.2.0.post20200714
Cython           : None
pytest           : 6.0.1
hypothesis       : None
sphinx           : None
blosc            : 1.9.1
feather          : None
xlsxwriter       : None
lxml.etree       : 4.5.2
html5lib         : 1.1
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : 7.17.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : 0.8.0
fastparquet      : None
gcsfs            : 0.6.2
matplotlib       : None
numexpr          : 2.7.1
odfpy            : None
openpyxl         : None
pandas_gbq       : 0.11.0
pyarrow          : None
pytables         : None
pyxlsb           : None
s3fs             : None
scipy            : 1.5.2
sqlalchemy       : None
tables           : 3.6.1
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
numba            : None
@veselyvojtech veselyvojtech added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 15, 2020
@jreback
Copy link
Contributor

jreback commented Aug 15, 2020

you are mutating a reference
this could raise a SettingWithCopy warning

show df before you are masking it

@veselyvojtech
Copy link
Author

What seems confusing to me is that the already adjusted DataFrame in the cell 53 does not look affected by the change, as if it contains the same data as the DataFrame in the cell 45.

@TomAugspurger TomAugspurger removed the Needs Triage Issue that has not been reviewed by a pandas team member label Sep 4, 2020
@TomAugspurger
Copy link
Contributor

@veselyvojtech have you been able to investigate this at all?

@TomAugspurger TomAugspurger added the Indexing Related to indexing on series/frames, not to indexes themselves label Sep 4, 2020
@jbrockmendel
Copy link
Member

I'm pretty sure this is closed by #36051

@jreback
Copy link
Contributor

jreback commented Sep 4, 2020

@jbrockmendel if you'd validate and add a test if you think we need.

@veselyvojtech
Copy link
Author

Yes, #36051 solves that. Thanks.

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
Projects
None yet
Development

No branches or pull requests

4 participants