Skip to content

Commit 75125e1

Browse files
committed
Merge pull request #10922 from jreback/pop
BUG: Bug in clearing the cache on DataFrame.pop and a subsequent inplace op #10912
2 parents 027817b + 2332481 commit 75125e1

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v0.17.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ Bug Fixes
750750
- Bug in ``DataFrame.reset_index`` when index contains `NaT`. (:issue:`10388`)
751751
- Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`)
752752
- Bug in ``BinGrouper.group_info`` where returned values are not compatible with base class (:issue:`10914`)
753-
753+
- Bug in clearing the cache on ``DataFrame.pop`` and a subsequent inplace op (:issue:`10912`)
754754

755755
- Bug causing ``DataFrame.where`` to not respect the ``axis`` parameter when the frame has a symmetric shape. (:issue:`9736`)
756756

pandas/core/generic.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,11 @@ def pop(self, item):
501501
"""
502502
result = self[item]
503503
del self[item]
504+
try:
505+
result._reset_cacher()
506+
except AttributeError:
507+
pass
508+
504509
return result
505510

506511
def squeeze(self):
@@ -1094,6 +1099,11 @@ def _set_as_cached(self, item, cacher):
10941099
a weakref to cacher """
10951100
self._cacher = (item, weakref.ref(cacher))
10961101

1102+
def _reset_cacher(self):
1103+
""" reset the cacher """
1104+
if hasattr(self,'_cacher'):
1105+
del self._cacher
1106+
10971107
def _iget_item_cache(self, item):
10981108
""" return the cached item, item represents a positional indexer """
10991109
ax = self._info_axis
@@ -1330,6 +1340,7 @@ def __delitem__(self, key):
13301340
# exception:
13311341
self._data.delete(key)
13321342

1343+
# delete from the caches
13331344
try:
13341345
del self._item_cache[key]
13351346
except KeyError:

pandas/tests/test_frame.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5177,6 +5177,20 @@ def test_pop(self):
51775177
self.assertNotIn('foo', self.frame)
51785178
# TODO self.assertEqual(self.frame.columns.name, 'baz')
51795179

5180+
# 10912
5181+
# inplace ops cause caching issue
5182+
a = DataFrame([[1,2,3],[4,5,6]], columns=['A','B','C'], index=['X','Y'])
5183+
b = a.pop('B')
5184+
b += 1
5185+
5186+
# original frame
5187+
expected = DataFrame([[1,3],[4,6]], columns=['A','C'], index=['X','Y'])
5188+
assert_frame_equal(a, expected)
5189+
5190+
# result
5191+
expected = Series([2,5],index=['X','Y'],name='B')+1
5192+
assert_series_equal(b, expected)
5193+
51805194
def test_pop_non_unique_cols(self):
51815195
df = DataFrame({0: [0, 1], 1: [0, 1], 2: [4, 5]})
51825196
df.columns = ["a", "b", "a"]

0 commit comments

Comments
 (0)