Skip to content

Commit 0dd5bd4

Browse files
committed
BUG: bug in cache updating when consolidating pandas-dev#10264
1 parent bc7d48f commit 0dd5bd4

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

doc/source/whatsnew/v0.16.2.txt

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Bug Fixes
5959
- Bug in ``Categorical`` repr with ``display.width`` of ``None`` in Python 3 (:issue:`10087`)
6060

6161
- Bug in groupby.apply aggregation for Categorical not preserving categories (:issue:`10138`)
62+
63+
- Bug in cache updating when consolidating (:issue:`10264`)
64+
6265
- Bug in ``mean()`` where integer dtypes can overflow (:issue:`10172`)
6366
- Bug where Panel.from_dict does not set dtype when specified (:issue:`10058`)
6467
- Bug in ``Index.union`` raises ``AttributeError`` when passing array-likes. (:issue:`10149`)

pandas/core/generic.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,7 @@ def take(self, indices, axis=0, convert=True, is_copy=True):
13521352
taken : type of caller
13531353
"""
13541354

1355+
self._consolidate_inplace()
13551356
new_data = self._data.take(indices,
13561357
axis=self._get_block_manager_axis(axis),
13571358
convert=True, verify=True)
@@ -2128,8 +2129,10 @@ def _protect_consolidate(self, f):
21282129
return result
21292130

21302131
def _consolidate_inplace(self):
2131-
f = lambda: self._data.consolidate()
2132-
self._data = self._protect_consolidate(f)
2132+
""" we are inplace consolidating; return None """
2133+
def f():
2134+
self._data = self._data.consolidate()
2135+
self._protect_consolidate(f)
21332136

21342137
def consolidate(self, inplace=False):
21352138
"""

pandas/core/indexing.py

+3
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ def _setitem_with_indexer(self, indexer, value):
369369
# we can directly set the series here
370370
# as we select a slice indexer on the mi
371371
idx = index._convert_slice_indexer(idx)
372+
obj._consolidate_inplace()
372373
obj = obj.copy()
373374
obj._data = obj._data.setitem(indexer=tuple([idx]), value=value)
374375
self.obj[item] = obj
@@ -396,6 +397,7 @@ def setter(item, v):
396397
s = v
397398
else:
398399
# set the item, possibly having a dtype change
400+
s._consolidate_inplace()
399401
s = s.copy()
400402
s._data = s._data.setitem(indexer=pi, value=v)
401403
s._maybe_update_cacher(clear=True)
@@ -492,6 +494,7 @@ def can_do_equal_len():
492494
self.obj._check_is_chained_assignment_possible()
493495

494496
# actually do the set
497+
self.obj._consolidate_inplace()
495498
self.obj._data = self.obj._data.setitem(indexer=indexer, value=value)
496499
self.obj._maybe_update_cacher(clear=True)
497500

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2830,13 +2830,13 @@ def consolidate(self):
28302830
return self
28312831

28322832
bm = self.__class__(self.blocks, self.axes)
2833+
bm._is_consolidated = False
28332834
bm._consolidate_inplace()
28342835
return bm
28352836

28362837
def _consolidate_inplace(self):
28372838
if not self.is_consolidated():
28382839
self.blocks = tuple(_consolidate(self.blocks))
2839-
28402840
self._is_consolidated = True
28412841
self._known_consolidated = True
28422842
self._rebuild_blknos_and_blklocs()

pandas/tests/test_indexing.py

+12
Original file line numberDiff line numberDiff line change
@@ -3527,6 +3527,18 @@ def test_cache_updating(self):
35273527
result = df.loc[(0,0),'z']
35283528
self.assertEqual(result, 2)
35293529

3530+
# 10264
3531+
df = DataFrame(np.zeros((5,5),dtype='int64'),columns=['a','b','c','d','e'],index=range(5))
3532+
df['f'] = 0
3533+
df.f.values[3] = 1
3534+
y = df.iloc[np.arange(2,len(df))]
3535+
df.f.values[3] = 2
3536+
expected = DataFrame(np.zeros((5,6),dtype='int64'),columns=['a','b','c','d','e','f'],index=range(5))
3537+
expected.at[3,'f'] = 2
3538+
assert_frame_equal(df, expected)
3539+
expected = Series([0,0,0,2,0],name='f')
3540+
assert_series_equal(df.f, expected)
3541+
35303542
def test_slice_consolidate_invalidate_item_cache(self):
35313543

35323544
# this is chained assignment, but will 'work'

0 commit comments

Comments
 (0)