Skip to content

Commit 5ac7078

Browse files
committed
BUG: Bug when renaming then set_index on a DataFrame (GH5344)
1 parent be7c4c0 commit 5ac7078

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ Bug Fixes
642642
related (:issue:`5312`)
643643
- Fix return value/type signature of ``initObjToJSON()`` to be compatible
644644
with numpy's ``import_array()`` (:issue:`5334`, :issue:`5326`)
645+
- Bug when renaming then set_index on a DataFrame (:issue:`5344`)
645646

646647
pandas 0.12.0
647648
-------------

pandas/core/internals.py

+3
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,9 @@ def _set_ref_locs(self, labels=None, do_refs=False):
19921992
self._ref_locs = rl
19931993
return rl
19941994

1995+
elif do_refs:
1996+
self._reset_ref_locs()
1997+
19951998
# return our cached _ref_locs (or will compute again
19961999
# when we recreate the block manager if needed
19972000
return getattr(self, '_ref_locs', None)

pandas/tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -8551,6 +8551,19 @@ def test_rename_inplace(self):
85518551
self.assert_('foo' in frame)
85528552
self.assert_(id(frame['foo']) != c_id)
85538553

8554+
def test_rename_bug(self):
8555+
# GH 5344
8556+
# rename set ref_locs, and set_index was not resetting
8557+
df = DataFrame({ 0 : ['foo','bar'], 1 : ['bah','bas'], 2 : [1,2]})
8558+
df = df.rename(columns={0 : 'a'})
8559+
df = df.rename(columns={1 : 'b'})
8560+
df = df.set_index(['a','b'])
8561+
df.columns = ['2001-01-01']
8562+
expected = DataFrame([[1],[2]],index=MultiIndex.from_tuples([('foo','bah'),('bar','bas')],
8563+
names=['a','b']),
8564+
columns=['2001-01-01'])
8565+
assert_frame_equal(df,expected)
8566+
85548567
#----------------------------------------------------------------------
85558568
# Time series related
85568569
def test_diff(self):

0 commit comments

Comments
 (0)