|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pandas import Series |
| 4 | + |
| 5 | +# ----------------------------------------------------------------------------- |
| 6 | +# Copy/view behaviour for Series / DataFrame constructors |
| 7 | + |
| 8 | + |
| 9 | +def test_series_from_series(using_copy_on_write): |
| 10 | + # Case: constructing a Series from another Series object follows CoW rules: |
| 11 | + # a new object is returned and thus mutations are not propagated |
| 12 | + ser = Series([1, 2, 3], name="name") |
| 13 | + |
| 14 | + # default is copy=False -> new Series is a shallow copy / view of original |
| 15 | + result = Series(ser) |
| 16 | + |
| 17 | + # the shallow copy still shares memory |
| 18 | + assert np.shares_memory(ser.values, result.values) |
| 19 | + |
| 20 | + if using_copy_on_write: |
| 21 | + assert result._mgr.refs is not None |
| 22 | + |
| 23 | + if using_copy_on_write: |
| 24 | + # mutating new series copy doesn't mutate original |
| 25 | + result.iloc[0] = 0 |
| 26 | + assert ser.iloc[0] == 1 |
| 27 | + # mutating triggered a copy-on-write -> no longer shares memory |
| 28 | + assert not np.shares_memory(ser.values, result.values) |
| 29 | + else: |
| 30 | + # mutating shallow copy does mutate original |
| 31 | + result.iloc[0] = 0 |
| 32 | + assert ser.iloc[0] == 0 |
| 33 | + # and still shares memory |
| 34 | + assert np.shares_memory(ser.values, result.values) |
| 35 | + |
| 36 | + # the same when modifying the parent |
| 37 | + result = Series(ser) |
| 38 | + |
| 39 | + if using_copy_on_write: |
| 40 | + # mutating original doesn't mutate new series |
| 41 | + ser.iloc[0] = 0 |
| 42 | + assert result.iloc[0] == 1 |
| 43 | + else: |
| 44 | + # mutating original does mutate shallow copy |
| 45 | + ser.iloc[0] = 0 |
| 46 | + assert result.iloc[0] == 0 |
| 47 | + |
| 48 | + |
| 49 | +def test_series_from_series_with_reindex(using_copy_on_write): |
| 50 | + # Case: constructing a Series from another Series with specifying an index |
| 51 | + # that potentially requires a reindex of the values |
| 52 | + ser = Series([1, 2, 3], name="name") |
| 53 | + |
| 54 | + # passing an index that doesn't actually require a reindex of the values |
| 55 | + # -> without CoW we get an actual mutating view |
| 56 | + for index in [ |
| 57 | + ser.index, |
| 58 | + ser.index.copy(), |
| 59 | + list(ser.index), |
| 60 | + ser.index.rename("idx"), |
| 61 | + ]: |
| 62 | + result = Series(ser, index=index) |
| 63 | + assert np.shares_memory(ser.values, result.values) |
| 64 | + result.iloc[0] = 0 |
| 65 | + if using_copy_on_write: |
| 66 | + assert ser.iloc[0] == 1 |
| 67 | + else: |
| 68 | + assert ser.iloc[0] == 0 |
| 69 | + |
| 70 | + # ensure that if an actual reindex is needed, we don't have any refs |
| 71 | + # (mutating the result wouldn't trigger CoW) |
| 72 | + result = Series(ser, index=[0, 1, 2, 3]) |
| 73 | + assert not np.shares_memory(ser.values, result.values) |
| 74 | + if using_copy_on_write: |
| 75 | + assert result._mgr.refs is None or result._mgr.refs[0] is None |
0 commit comments