Skip to content

Inplace #1234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Inplace #1234

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2610,7 +2610,7 @@ def fillna(self, value=None, method='pad', axis=0, inplace=False,
#----------------------------------------------------------------------
# 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 @@ -2624,6 +2624,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 @@ -2643,7 +2646,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
10 changes: 7 additions & 3 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,22 @@ def merge(self, other):
return _merge_blocks([self, other], self.ref_items)

def reindex_axis(self, indexer, mask, needs_masking, axis=0,
fill_value=np.nan):
fill_value=np.nan, out=None):
"""
Reindex using pre-computed indexer information
"""
if self.values.size > 0:
new_values = com.take_fast(self.values, indexer, mask,
needs_masking, axis=axis,
fill_value=fill_value)
fill_value=fill_value,
out=out)
else:
shape = list(self.shape)
shape[axis] = len(indexer)
new_values = np.empty(shape)
if out is not None and (out.shape == shape):
new_values = out
else:
new_values = np.empty(shape)
new_values.fill(fill_value)
return make_block(new_values, self.items, self.ref_items)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2377,7 +2377,7 @@ def interpolate(self, method='linear'):

return Series(result, index=self.index, name=self.name)

def rename(self, mapper):
def rename(self, mapper, inplace=False):
"""
Alter Series index using dict or function

Expand Down Expand Up @@ -2412,7 +2412,7 @@ def rename(self, mapper):
renamed : Series (new object)
"""
mapper_f = _get_rename_function(mapper)
result = self.copy()
result = self if inplace else self.copy()
result.index = [mapper_f(x) for x in self.index]

return result
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 @@ -3807,6 +3807,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
6 changes: 6 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,12 @@ def test_rename(self):
renamed = s.rename({'b' : 'foo', 'd' : 'bar'})
self.assert_(np.array_equal(renamed.index, ['a', 'foo', 'c', 'bar']))

def test_rename_inplace(self):
renamer = lambda x: x.strftime('%Y%m%d')
expected = renamer(self.ts.index[0])
self.ts.rename(renamer, inplace=True)
self.assertEqual(self.ts.index[0], expected)

def test_preserveRefs(self):
seq = self.ts[[5,10,15]]
seq[1] = np.NaN
Expand Down