Skip to content

Commit bb1b9c9

Browse files
committed
return a shallow copy instead of deep copy
1 parent 54da197 commit bb1b9c9

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

pandas/core/indexing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ def _get_slice_axis(self, slice_obj, axis=0):
12501250
obj = self.obj
12511251

12521252
if not need_slice(slice_obj):
1253-
return obj.copy()
1253+
return obj.copy(deep=False)
12541254
indexer = self._convert_slice_indexer(slice_obj, axis)
12551255

12561256
if isinstance(indexer, slice):
@@ -1349,7 +1349,7 @@ def _get_slice_axis(self, slice_obj, axis=0):
13491349
""" this is pretty simple as we just have to deal with labels """
13501350
obj = self.obj
13511351
if not need_slice(slice_obj):
1352-
return obj.copy()
1352+
return obj.copy(deep=False)
13531353

13541354
labels = obj._get_axis(axis)
13551355
indexer = labels.slice_indexer(slice_obj.start, slice_obj.stop,
@@ -1690,7 +1690,7 @@ def _get_slice_axis(self, slice_obj, axis=0):
16901690
obj = self.obj
16911691

16921692
if not need_slice(slice_obj):
1693-
return obj.copy()
1693+
return obj.copy(deep=False)
16941694

16951695
slice_obj = self._convert_slice_indexer(slice_obj, axis)
16961696
if isinstance(slice_obj, slice):

pandas/tests/indexing/test_iloc.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -594,5 +594,9 @@ def test_iloc_empty_list_indexer_is_ok(self):
594594

595595
def test_loc_identity_slice_returns_new_object(self):
596596
# GH13873
597-
df = DataFrame({'a': [1, 3, 5], 'b': [2, 4, 6]})
598-
assert not df.iloc[:] is df
597+
df = DataFrame({'a': [1, 2, 3]})
598+
result = df.iloc[:]
599+
assert not result is df
600+
# should be a shallow copy
601+
df['a'] = [4, 4, 4]
602+
assert (result['a'] == 4).all()

pandas/tests/indexing/test_loc.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -633,5 +633,9 @@ def test_loc_empty_list_indexer_is_ok(self):
633633

634634
def test_loc_identity_slice_returns_new_object(self):
635635
# GH13873
636-
df = DataFrame({'a': [1, 3, 5], 'b': [2, 4, 6]})
637-
assert not df.loc[:] is df
636+
df = DataFrame({'a': [1, 2, 3]})
637+
result = df.loc[:]
638+
assert not result is df
639+
# should be a shallow copy
640+
df['a'] = [4, 4, 4]
641+
assert (result['a'] == 4).all()

0 commit comments

Comments
 (0)