Skip to content

Commit f59f6e4

Browse files
committed
Merge pull request #5345 from jreback/rename_bug
BUG: Bug when renaming then set_index on a DataFrame (GH5344)
2 parents e96df8e + 5ac7078 commit f59f6e4

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
@@ -8562,6 +8562,19 @@ def test_rename_inplace(self):
85628562
self.assert_('foo' in frame)
85638563
self.assert_(id(frame['foo']) != c_id)
85648564

8565+
def test_rename_bug(self):
8566+
# GH 5344
8567+
# rename set ref_locs, and set_index was not resetting
8568+
df = DataFrame({ 0 : ['foo','bar'], 1 : ['bah','bas'], 2 : [1,2]})
8569+
df = df.rename(columns={0 : 'a'})
8570+
df = df.rename(columns={1 : 'b'})
8571+
df = df.set_index(['a','b'])
8572+
df.columns = ['2001-01-01']
8573+
expected = DataFrame([[1],[2]],index=MultiIndex.from_tuples([('foo','bah'),('bar','bas')],
8574+
names=['a','b']),
8575+
columns=['2001-01-01'])
8576+
assert_frame_equal(df,expected)
8577+
85658578
#----------------------------------------------------------------------
85668579
# Time series related
85678580
def test_diff(self):

0 commit comments

Comments
 (0)