Skip to content

BUG: Fixed a bug in DataFrame/Panel cache insertion and subsequent indexing GH4939 #4942

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
Sep 23, 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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ Bug Fixes
- Fixed a bug in ``Series.hist`` where two figures were being created when
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`)

pandas 0.12.0
-------------
Expand Down
14 changes: 10 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,14 +825,20 @@ def _maybe_cache_changed(self, item, value):
maybe it has changed """
self._data.set(item, value)

def _maybe_update_cacher(self):
""" see if we need to update our parent cacher """
def _maybe_update_cacher(self, clear=False):
""" see if we need to update our parent cacher
if clear, then clear our cache """
cacher = getattr(self,'_cacher',None)
if cacher is not None:
cacher[1]()._maybe_cache_changed(cacher[0],self)
if clear:
self._clear_item_cache()

def _clear_item_cache(self):
self._item_cache.clear()
def _clear_item_cache(self, i=None):
if i is not None:
self._item_cache.pop(i,None)
else:
self._item_cache.clear()

def _set_item(self, key, value):
self._data.set(key, value)
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,14 @@ def _setitem_with_indexer(self, indexer, value):

new_values = np.concatenate([self.obj.values, [value]])
self.obj._data = self.obj._constructor(new_values, index=new_index, name=self.obj.name)
self.obj._maybe_update_cacher(clear=True)
return self.obj

elif self.ndim == 2:
index = self.obj._get_axis(0)
labels = _safe_append_to_index(index, indexer)
self.obj._data = self.obj.reindex_axis(labels,0)._data
self.obj._maybe_update_cacher(clear=True)
return getattr(self.obj,self.name).__setitem__(indexer,value)

# set using setitem (Panel and > dims)
Expand Down Expand Up @@ -255,6 +257,7 @@ def setter(item, v):
# set the item, possibly having a dtype change
s = s.copy()
s._data = s._data.setitem(pi,v)
s._maybe_update_cacher(clear=True)
self.obj[item] = s

def can_do_equal_len():
Expand Down Expand Up @@ -327,6 +330,7 @@ def can_do_equal_len():
value = self._align_panel(indexer, value)

self.obj._data = self.obj._data.setitem(indexer,value)
self.obj._maybe_update_cacher(clear=True)

def _align_series(self, indexer, ser):
# indexer to assign Series can be tuple or scalar
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,21 @@ def test_series_partial_set(self):
result = ser.iloc[[1,1,0,0]]
assert_series_equal(result, expected)

def test_cache_updating(self):
# GH 4939, make sure to update the cache on setitem

df = tm.makeDataFrame()
df['A'] # cache series
df.ix["Hello Friend"] = df.ix[0]
self.assert_("Hello Friend" in df['A'].index)
self.assert_("Hello Friend" in df['B'].index)

panel = tm.makePanel()
panel.ix[0] # get first item into cache
panel.ix[:, :, 'A+1'] = panel.ix[:, :, 'A'] + 1
self.assert_("A+1" in panel.ix[0].columns)
self.assert_("A+1" in panel.ix[1].columns)

if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down