Skip to content

Commit 91344b9

Browse files
committed
BUG: .iloc[:] and .loc[:] return copy of original object (#13873)
This is consistent with the behavior of [:] on a list https://docs.python.org/2/tutorial/introduction.html#lists https://docs.python.org/3/tutorial/introduction.html#lists
1 parent 49ec31b commit 91344b9

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
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
1253+
return obj.copy()
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
1352+
return obj.copy()
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
1693+
return obj.copy()
16941694

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

pandas/tests/indexing/test_iloc.py

+5
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,8 @@ def test_iloc_empty_list_indexer_is_ok(self):
591591
tm.assert_frame_equal(df.iloc[[]], df.iloc[:0, :],
592592
check_index_type=True,
593593
check_column_type=True)
594+
595+
def test_loc_identity_slice_returns_new_object(self):
596+
# GH13873
597+
df = DataFrame({'a': [1, 3, 5], 'b': [2, 4, 6]})
598+
assert not df.iloc[:] is df

pandas/tests/indexing/test_loc.py

+5
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,8 @@ def test_loc_empty_list_indexer_is_ok(self):
630630
tm.assert_frame_equal(df.loc[[]], df.iloc[:0, :],
631631
check_index_type=True,
632632
check_column_type=True)
633+
634+
def test_loc_identity_slice_returns_new_object(self):
635+
# GH13873
636+
df = DataFrame({'a': [1, 3, 5], 'b': [2, 4, 6]})
637+
assert not df.loc[:] is df

0 commit comments

Comments
 (0)