Skip to content

BUG: not clearing the cache when reindexing issue when partial setting (GH5424) #5426

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

Merged
merged 1 commit into from
Nov 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ Bug Fixes
the ``by`` argument was passed (:issue:`4112`, :issue:`4113`).
- Fixed a bug in ``convert_objects`` for > 2 ndims (:issue:`4937`)
- Fixed a bug in DataFrame/Panel cache insertion and subsequent indexing
(:issue:`4939`)
(:issue:`4939`, :issue:`5424`)
- Fixed string methods for ``FrozenNDArray`` and ``FrozenList``
(:issue:`4929`)
- Fixed a bug with setting invalid or out-of-range values in indexing
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,13 @@ def _setitem_with_indexer(self, indexer, value):
return self.obj

# reindex the axis
# make sure to clear the cache because we are
# just replacing the block manager here
# so the object is the same
index = self.obj._get_axis(i)
labels = _safe_append_to_index(index, key)
self.obj._data = self.obj.reindex_axis(labels,i)._data
self.obj._maybe_update_cacher(clear=True)

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

self.obj._data = self.obj._constructor(new_values, index=new_index, name=self.obj.name)
self.obj._data = self.obj._constructor(new_values,
index=new_index, name=self.obj.name)._data
self.obj._maybe_update_cacher(clear=True)
return self.obj

Expand Down
18 changes: 0 additions & 18 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3539,24 +3539,6 @@ def test_operators_timedelta64(self):
self.assertTrue(df['off1'].dtype == 'timedelta64[ns]')
self.assertTrue(df['off2'].dtype == 'timedelta64[ns]')

def test__slice_consolidate_invalidate_item_cache(self):
# #3970
df = DataFrame({ "aa":lrange(5), "bb":[2.2]*5})

# Creates a second float block
df["cc"] = 0.0

# caches a reference to the 'bb' series
df["bb"]

# repr machinery triggers consolidation
repr(df)

# Assignment to wrong series
df['bb'].iloc[0] = 0.17
df._clear_item_cache()
self.assertAlmostEqual(df['bb'][0], 0.17)

def test_new_empty_index(self):
df1 = DataFrame(randn(0, 3))
df2 = DataFrame(randn(0, 3))
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,41 @@ def test_cache_updating(self):
result = df.loc[(0,0),'z']
self.assert_(result == 2)

def test_slice_consolidate_invalidate_item_cache(self):
# #3970
df = DataFrame({ "aa":lrange(5), "bb":[2.2]*5})

# Creates a second float block
df["cc"] = 0.0

# caches a reference to the 'bb' series
df["bb"]

# repr machinery triggers consolidation
repr(df)

# Assignment to wrong series
df['bb'].iloc[0] = 0.17
df._clear_item_cache()
self.assertAlmostEqual(df['bb'][0], 0.17)

def test_setitem_cache_updating(self):
# GH 5424
cont = ['one', 'two','three', 'four', 'five', 'six', 'seven']

for do_ref in [False,False]:
df = DataFrame({'a' : cont, "b":cont[3:]+cont[:3] ,'c' : np.arange(7)})

# ref the cache
if do_ref:
df.ix[0,"c"]

# set it
df.ix[7,'c'] = 1

self.assert_(df.ix[0,'c'] == 0.0)
self.assert_(df.ix[7,'c'] == 1.0)

def test_floating_index_doc_example(self):

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