+46
-16
lines changedFilter options
+46
-16
lines changed Original file line number Diff line number Diff line change
@@ -1346,7 +1346,7 @@ def fillna(self, value=None, method='pad'):
1346
1346
#----------------------------------------------------------------------
1347
1347
# Rename
1348
1348
1349
-
def rename(self, index=None, columns=None):
1349
+
def rename(self, index=None, columns=None, copy=True):
1350
1350
"""
1351
1351
Alter index and / or columns using input function or
1352
1352
functions. Function / dict values must be unique (1-to-1). Labels not
@@ -1358,6 +1358,8 @@ def rename(self, index=None, columns=None):
1358
1358
Transformation to apply to index values
1359
1359
columns : dict-like or function, optional
1360
1360
Transformation to apply to column values
1361
+
copy : boolean, default True
1362
+
Also copy underlying data
1361
1363
1362
1364
See also
1363
1365
--------
@@ -1390,7 +1392,7 @@ def columns_f(x):
1390
1392
1391
1393
self._consolidate_inplace()
1392
1394
1393
-
result = self.copy()
1395
+
result = self.copy(deep=copy)
1394
1396
1395
1397
if index is not None:
1396
1398
result._rename_index_inplace(index_f)
@@ -1405,7 +1407,7 @@ def _rename_index_inplace(self, mapper):
1405
1407
self._series_cache.clear()
1406
1408
1407
1409
def _rename_columns_inplace(self, mapper):
1408
-
self._data = self._data.rename_items(mapper)
1410
+
self._data = self._data.rename_items(mapper, copydata=False)
1409
1411
self._series_cache.clear()
1410
1412
1411
1413
#----------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -397,9 +397,23 @@ def _values_aggregate(self, func, axis, fill_value, skipna=True):
397
397
398
398
return result
399
399
400
-
def copy(self):
401
-
"""Make a deep copy of this object"""
402
-
return self._constructor(self._data.copy())
400
+
def copy(self, deep=True):
401
+
"""
402
+
Make a copy of this object
403
+
404
+
Parameters
405
+
----------
406
+
deep : boolean, default True
407
+
Make a deep copy, i.e. also copy data
408
+
409
+
Returns
410
+
-------
411
+
copy : type of caller
412
+
"""
413
+
data = self._data
414
+
if deep:
415
+
data = data.copy()
416
+
return self._constructor(data)
403
417
404
418
def swaplevel(self, i, j, axis=0):
405
419
"""
Original file line number Diff line number Diff line change
@@ -84,8 +84,11 @@ def shape(self):
84
84
def dtype(self):
85
85
return self.values.dtype
86
86
87
-
def copy(self):
88
-
return make_block(self.values.copy(), self.items, self.ref_items)
87
+
def copy(self, deep=True):
88
+
values = self.values
89
+
if deep:
90
+
values = values.copy()
91
+
return make_block(values, self.items, self.ref_items)
89
92
90
93
def merge(self, other):
91
94
assert(self.ref_items.equals(other.ref_items))
@@ -682,13 +685,13 @@ def rename_axis(self, mapper, axis=1):
682
685
new_axes[axis] = new_axis
683
686
return BlockManager(self.blocks, new_axes)
684
687
685
-
def rename_items(self, mapper):
688
+
def rename_items(self, mapper, copydata=True):
686
689
new_items = Index([mapper(x) for x in self.items])
687
690
new_items._verify_integrity()
688
691
689
692
new_blocks = []
690
693
for block in self.blocks:
691
-
newb = block.copy()
694
+
newb = block.copy(deep=copydata)
692
695
newb.set_ref_items(new_items, maybe_rename=True)
693
696
new_blocks.append(newb)
694
697
new_axes = list(self.axes)
Original file line number Diff line number Diff line change
@@ -410,12 +410,15 @@ def astype(self, dtype=None):
410
410
411
411
return self.copy()
412
412
413
-
def copy(self):
413
+
def copy(self, deep=True):
414
414
"""
415
415
Make a copy of the SparseSeries. Only the actual sparse values need to
416
416
be copied
417
417
"""
418
-
values = self.sp_values.copy()
418
+
if deep:
419
+
values = self.sp_values.copy()
420
+
else:
421
+
values = self.sp_values
419
422
return SparseSeries(values, index=self.index,
420
423
sparse_index=self.sp_index,
421
424
fill_value=self.fill_value)
@@ -775,12 +778,15 @@ def to_dense(self):
775
778
data = dict((k, v.to_dense()) for k, v in self.iteritems())
776
779
return DataFrame(data, index=self.index)
777
780
778
-
def copy(self):
781
+
def copy(self, deep=True):
779
782
"""
780
-
Make a deep copy of this SparseDataFrame
783
+
Make a copy of this SparseDataFrame
781
784
"""
782
-
return SparseDataFrame(self._series, index=self.index,
783
-
columns=self.columns,
785
+
if deep:
786
+
series = self._series.copy()
787
+
else:
788
+
series = self._series
789
+
return SparseDataFrame(series, index=self.index, columns=self.columns,
784
790
default_fill_value=self.default_fill_value,
785
791
default_kind=self.default_kind)
786
792
Original file line number Diff line number Diff line change
@@ -2025,6 +2025,11 @@ def test_rename(self):
2025
2025
renamed = self.frame.T.rename(index={'C' : 'foo', 'D' : 'bar'})
2026
2026
self.assert_(np.array_equal(renamed.index, ['A', 'B', 'foo', 'bar']))
2027
2027
2028
+
def test_rename_nocopy(self):
2029
+
renamed = self.frame.rename(columns={'C' : 'foo'}, copy=False)
2030
+
renamed['foo'] = 1.
2031
+
self.assert_((self.frame['C'] == 1.).all())
2032
+
2028
2033
#----------------------------------------------------------------------
2029
2034
# Time series related
2030
2035
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4