From 2332481f03213641a8d6950d2dc2404e9bce1a3e Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Fri, 28 Aug 2015 14:35:00 -0400 Subject: [PATCH] BUG: Bug in clearing the cache on DataFrame.pop and a subsequent inplace op #10912 --- doc/source/whatsnew/v0.17.0.txt | 2 +- pandas/core/generic.py | 11 +++++++++++ pandas/tests/test_frame.py | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 71aac42b17810..0ccfa06fc8844 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -750,7 +750,7 @@ Bug Fixes - Bug in ``DataFrame.reset_index`` when index contains `NaT`. (:issue:`10388`) - Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`) - Bug in ``BinGrouper.group_info`` where returned values are not compatible with base class (:issue:`10914`) - +- Bug in clearing the cache on ``DataFrame.pop`` and a subsequent inplace op (:issue:`10912`) - Bug causing ``DataFrame.where`` to not respect the ``axis`` parameter when the frame has a symmetric shape. (:issue:`9736`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b127fb220569d..fe09e03281b4f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -501,6 +501,11 @@ def pop(self, item): """ result = self[item] del self[item] + try: + result._reset_cacher() + except AttributeError: + pass + return result def squeeze(self): @@ -1094,6 +1099,11 @@ def _set_as_cached(self, item, cacher): a weakref to cacher """ self._cacher = (item, weakref.ref(cacher)) + def _reset_cacher(self): + """ reset the cacher """ + if hasattr(self,'_cacher'): + del self._cacher + def _iget_item_cache(self, item): """ return the cached item, item represents a positional indexer """ ax = self._info_axis @@ -1330,6 +1340,7 @@ def __delitem__(self, key): # exception: self._data.delete(key) + # delete from the caches try: del self._item_cache[key] except KeyError: diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index fe78ff0f79ff3..58c6d15f8ada5 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -5177,6 +5177,20 @@ def test_pop(self): self.assertNotIn('foo', self.frame) # TODO self.assertEqual(self.frame.columns.name, 'baz') + # 10912 + # inplace ops cause caching issue + a = DataFrame([[1,2,3],[4,5,6]], columns=['A','B','C'], index=['X','Y']) + b = a.pop('B') + b += 1 + + # original frame + expected = DataFrame([[1,3],[4,6]], columns=['A','C'], index=['X','Y']) + assert_frame_equal(a, expected) + + # result + expected = Series([2,5],index=['X','Y'],name='B')+1 + assert_series_equal(b, expected) + def test_pop_non_unique_cols(self): df = DataFrame({0: [0, 1], 1: [0, 1], 2: [4, 5]}) df.columns = ["a", "b", "a"]