Skip to content

BUG: Bug in clearing the cache on DataFrame.pop and a subsequent inplace op #10912 #10922

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
Aug 28, 2015
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/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down