Skip to content

Commit

Permalink
ENH: inplace keyword to DataFrame.rename #207
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang She authored and wesm committed May 21, 2012
1 parent 89485cf commit eb80a1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2777,7 +2777,7 @@ def _replace_both_dict(self, to_replace, value, inplace):
#----------------------------------------------------------------------
# Rename

def rename(self, index=None, columns=None, copy=True):
def rename(self, index=None, columns=None, copy=True, inplace=False):
"""
Alter index and / or columns using input function or
functions. Function / dict values must be unique (1-to-1). Labels not
Expand All @@ -2791,6 +2791,9 @@ def rename(self, index=None, columns=None, copy=True):
Transformation to apply to column values
copy : boolean, default True
Also copy underlying data
inplace : boolean, default False
Whether to return a new DataFrame. If True then value of copy is
ignored.
See also
--------
Expand All @@ -2810,7 +2813,7 @@ def rename(self, index=None, columns=None, copy=True):

self._consolidate_inplace()

result = self.copy(deep=copy)
result = self if inplace else self.copy(deep=copy)

if index is not None:
result._rename_index_inplace(index_f)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4094,6 +4094,15 @@ def test_rename_nocopy(self):
renamed['foo'] = 1.
self.assert_((self.frame['C'] == 1.).all())

def test_rename_inplace(self):
self.frame.rename(columns={'C' : 'foo'})
self.assert_('C' in self.frame)
self.assert_('foo' not in self.frame)

self.frame.rename(columns={'C' : 'foo'}, inplace=True)
self.assert_('C' not in self.frame)
self.assert_('foo' in self.frame)

#----------------------------------------------------------------------
# Time series related

Expand Down

0 comments on commit eb80a1c

Please sign in to comment.