Skip to content

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

Closed
@veselyvojtech

Description

@veselyvojtech
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions