Skip to content

Commit d2c05c4

Browse files
TST: add CoW test for update() (#51426)
1 parent 81d4f0c commit d2c05c4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pandas/tests/copy_view/test_methods.py

+36
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,42 @@ def test_xs_multiindex(using_copy_on_write, using_array_manager, key, level, axi
15391539
tm.assert_frame_equal(df, df_orig)
15401540

15411541

1542+
def test_update_frame(using_copy_on_write):
1543+
df1 = DataFrame({"a": [1.0, 2.0, 3.0], "b": [4.0, 5.0, 6.0]})
1544+
df2 = DataFrame({"b": [100.0]}, index=[1])
1545+
df1_orig = df1.copy()
1546+
view = df1[:]
1547+
1548+
df1.update(df2)
1549+
1550+
expected = DataFrame({"a": [1.0, 2.0, 3.0], "b": [4.0, 100.0, 6.0]})
1551+
tm.assert_frame_equal(df1, expected)
1552+
if using_copy_on_write:
1553+
# df1 is updated, but its view not
1554+
tm.assert_frame_equal(view, df1_orig)
1555+
assert np.shares_memory(get_array(df1, "a"), get_array(view, "a"))
1556+
assert not np.shares_memory(get_array(df1, "b"), get_array(view, "b"))
1557+
else:
1558+
tm.assert_frame_equal(view, expected)
1559+
1560+
1561+
def test_update_series(using_copy_on_write):
1562+
ser1 = Series([1.0, 2.0, 3.0])
1563+
ser2 = Series([100.0], index=[1])
1564+
ser1_orig = ser1.copy()
1565+
view = ser1[:]
1566+
1567+
ser1.update(ser2)
1568+
1569+
expected = Series([1.0, 100.0, 3.0])
1570+
tm.assert_series_equal(ser1, expected)
1571+
if using_copy_on_write:
1572+
# ser1 is updated, but its view not
1573+
tm.assert_series_equal(view, ser1_orig)
1574+
else:
1575+
tm.assert_series_equal(view, expected)
1576+
1577+
15421578
def test_inplace_arithmetic_series():
15431579
ser = Series([1, 2, 3])
15441580
data = get_array(ser)

0 commit comments

Comments
 (0)