Skip to content

Commit caf3d66

Browse files
committed
Merge pull request #5426 from jreback/indexing_cache
BUG: not clearing the cache when reindexing issue when partial setting (GH5424)
2 parents 8828dbf + 70d2882 commit caf3d66

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

doc/source/release.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ Bug Fixes
660660
the ``by`` argument was passed (:issue:`4112`, :issue:`4113`).
661661
- Fixed a bug in ``convert_objects`` for > 2 ndims (:issue:`4937`)
662662
- Fixed a bug in DataFrame/Panel cache insertion and subsequent indexing
663-
(:issue:`4939`)
663+
(:issue:`4939`, :issue:`5424`)
664664
- Fixed string methods for ``FrozenNDArray`` and ``FrozenList``
665665
(:issue:`4929`)
666666
- Fixed a bug with setting invalid or out-of-range values in indexing

pandas/core/indexing.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,13 @@ def _setitem_with_indexer(self, indexer, value):
189189
return self.obj
190190

191191
# reindex the axis
192+
# make sure to clear the cache because we are
193+
# just replacing the block manager here
194+
# so the object is the same
192195
index = self.obj._get_axis(i)
193196
labels = _safe_append_to_index(index, key)
194197
self.obj._data = self.obj.reindex_axis(labels,i)._data
198+
self.obj._maybe_update_cacher(clear=True)
195199

196200
if isinstance(labels,MultiIndex):
197201
self.obj.sortlevel(inplace=True)
@@ -223,7 +227,8 @@ def _setitem_with_indexer(self, indexer, value):
223227
if len(self.obj.values):
224228
new_values = np.concatenate([self.obj.values, new_values])
225229

226-
self.obj._data = self.obj._constructor(new_values, index=new_index, name=self.obj.name)
230+
self.obj._data = self.obj._constructor(new_values,
231+
index=new_index, name=self.obj.name)._data
227232
self.obj._maybe_update_cacher(clear=True)
228233
return self.obj
229234

pandas/tests/test_frame.py

-18
Original file line numberDiff line numberDiff line change
@@ -3539,24 +3539,6 @@ def test_operators_timedelta64(self):
35393539
self.assertTrue(df['off1'].dtype == 'timedelta64[ns]')
35403540
self.assertTrue(df['off2'].dtype == 'timedelta64[ns]')
35413541

3542-
def test__slice_consolidate_invalidate_item_cache(self):
3543-
# #3970
3544-
df = DataFrame({ "aa":lrange(5), "bb":[2.2]*5})
3545-
3546-
# Creates a second float block
3547-
df["cc"] = 0.0
3548-
3549-
# caches a reference to the 'bb' series
3550-
df["bb"]
3551-
3552-
# repr machinery triggers consolidation
3553-
repr(df)
3554-
3555-
# Assignment to wrong series
3556-
df['bb'].iloc[0] = 0.17
3557-
df._clear_item_cache()
3558-
self.assertAlmostEqual(df['bb'][0], 0.17)
3559-
35603542
def test_new_empty_index(self):
35613543
df1 = DataFrame(randn(0, 3))
35623544
df2 = DataFrame(randn(0, 3))

pandas/tests/test_indexing.py

+35
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,41 @@ def test_cache_updating(self):
16451645
result = df.loc[(0,0),'z']
16461646
self.assert_(result == 2)
16471647

1648+
def test_slice_consolidate_invalidate_item_cache(self):
1649+
# #3970
1650+
df = DataFrame({ "aa":lrange(5), "bb":[2.2]*5})
1651+
1652+
# Creates a second float block
1653+
df["cc"] = 0.0
1654+
1655+
# caches a reference to the 'bb' series
1656+
df["bb"]
1657+
1658+
# repr machinery triggers consolidation
1659+
repr(df)
1660+
1661+
# Assignment to wrong series
1662+
df['bb'].iloc[0] = 0.17
1663+
df._clear_item_cache()
1664+
self.assertAlmostEqual(df['bb'][0], 0.17)
1665+
1666+
def test_setitem_cache_updating(self):
1667+
# GH 5424
1668+
cont = ['one', 'two','three', 'four', 'five', 'six', 'seven']
1669+
1670+
for do_ref in [False,False]:
1671+
df = DataFrame({'a' : cont, "b":cont[3:]+cont[:3] ,'c' : np.arange(7)})
1672+
1673+
# ref the cache
1674+
if do_ref:
1675+
df.ix[0,"c"]
1676+
1677+
# set it
1678+
df.ix[7,'c'] = 1
1679+
1680+
self.assert_(df.ix[0,'c'] == 0.0)
1681+
self.assert_(df.ix[7,'c'] == 1.0)
1682+
16481683
def test_floating_index_doc_example(self):
16491684

16501685
index = Index([1.5, 2, 3, 4.5, 5])

0 commit comments

Comments
 (0)