Skip to content

Commit 6809885

Browse files
committed
BUG: type change breaks BlockManager integrity
1 parent f010229 commit 6809885

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v0.15.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Bug Fixes
7171
- ``Timedelta`` kwargs may now be numpy ints and floats (:issue:`8757`).
7272
- ``sql_schema`` now generates dialect appropriate ``CREATE TABLE`` statements (:issue:`8697`)
7373
- ``slice`` string method now takes step into account (:issue:`8754`)
74+
- Bug in ``BlockManager`` where setting values with different type would break block integrity (:issue:`8850`)
7475
- Fix negative step support for label-based slices (:issue:`8753`)
7576

7677
Old behavior:

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2985,7 +2985,7 @@ def value_getitem(placement):
29852985
loc = [loc]
29862986

29872987
blknos = self._blknos[loc]
2988-
blklocs = self._blklocs[loc]
2988+
blklocs = self._blklocs[loc].copy()
29892989

29902990
unfit_mgr_locs = []
29912991
unfit_val_locs = []

pandas/tests/test_internals.py

+11
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,17 @@ def test_set_change_dtype(self):
430430
mgr2.set('quux', randn(N))
431431
self.assertEqual(mgr2.get('quux').dtype, np.float_)
432432

433+
def test_set_change_dtype_slice(self): # GH8850
434+
cols = MultiIndex.from_tuples([('1st','a'), ('2nd','b'), ('3rd','c')])
435+
df = DataFrame([[1.0, 2, 3], [4.0, 5, 6]], columns=cols)
436+
df['2nd'] = df['2nd'] * 2.0
437+
438+
self.assertEqual(sorted(df.blocks.keys()), ['float64', 'int64'])
439+
assert_frame_equal(df.blocks['float64'],
440+
DataFrame([[1.0, 4.0], [4.0, 10.0]], columns=cols[:2]))
441+
assert_frame_equal(df.blocks['int64'],
442+
DataFrame([[3], [6]], columns=cols[2:]))
443+
433444
def test_copy(self):
434445
shallow = self.mgr.copy(deep=False)
435446

0 commit comments

Comments
 (0)