Skip to content

Commit 995ca49

Browse files
authored
TST: df.loc[:, 'col'] returning a view, but df.loc[df.index, 'col'] returning a copy (#34996)
1 parent c8b44dd commit 995ca49

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pandas/tests/indexing/test_loc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,22 @@ def test_identity_slice_returns_new_object(self):
894894
original_series[:3] = [7, 8, 9]
895895
assert all(sliced_series[:3] == [7, 8, 9])
896896

897+
def test_loc_copy_vs_view(self):
898+
# GH 15631
899+
x = DataFrame(zip(range(3), range(3)), columns=["a", "b"])
900+
901+
y = x.copy()
902+
q = y.loc[:, "a"]
903+
q += 2
904+
905+
tm.assert_frame_equal(x, y)
906+
907+
z = x.copy()
908+
q = z.loc[x.index, "a"]
909+
q += 2
910+
911+
tm.assert_frame_equal(x, z)
912+
897913
def test_loc_uint64(self):
898914
# GH20722
899915
# Test whether loc accept uint64 max value as index.

0 commit comments

Comments
 (0)