Skip to content

Commit eb80a1c

Browse files
Chang Shewesm
Chang She
authored andcommitted
ENH: inplace keyword to DataFrame.rename #207
1 parent 89485cf commit eb80a1c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pandas/core/frame.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2777,7 +2777,7 @@ def _replace_both_dict(self, to_replace, value, inplace):
27772777
#----------------------------------------------------------------------
27782778
# Rename
27792779

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

28112814
self._consolidate_inplace()
28122815

2813-
result = self.copy(deep=copy)
2816+
result = self if inplace else self.copy(deep=copy)
28142817

28152818
if index is not None:
28162819
result._rename_index_inplace(index_f)

pandas/tests/test_frame.py

+9
Original file line numberDiff line numberDiff line change
@@ -4094,6 +4094,15 @@ def test_rename_nocopy(self):
40944094
renamed['foo'] = 1.
40954095
self.assert_((self.frame['C'] == 1.).all())
40964096

4097+
def test_rename_inplace(self):
4098+
self.frame.rename(columns={'C' : 'foo'})
4099+
self.assert_('C' in self.frame)
4100+
self.assert_('foo' not in self.frame)
4101+
4102+
self.frame.rename(columns={'C' : 'foo'}, inplace=True)
4103+
self.assert_('C' not in self.frame)
4104+
self.assert_('foo' in self.frame)
4105+
40974106
#----------------------------------------------------------------------
40984107
# Time series related
40994108

0 commit comments

Comments
 (0)