diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9afccce266522..65d18c9017e70 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 @@ -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 -------- @@ -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) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index f74c38ac5f450..c4f18a7400a7e 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -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) diff --git a/pandas/core/series.py b/pandas/core/series.py index d8e4ad546734f..d46b5f2f15f6a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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 @@ -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 diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index b21bd09957bd7..f2a3351629305 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -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 diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index d81fb312099b5..97954d65e1948 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -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