|
1 | 1 | import numpy as np
|
2 | 2 |
|
3 |
| -from pandas import DataFrame |
| 3 | +from pandas import ( |
| 4 | + DataFrame, |
| 5 | + Series, |
| 6 | +) |
4 | 7 | import pandas._testing as tm
|
5 | 8 |
|
6 | 9 |
|
@@ -136,14 +139,36 @@ def test_select_dtypes(using_copy_on_write):
|
136 | 139 | df2._mgr._verify_integrity()
|
137 | 140 |
|
138 | 141 | # currently this always returns a "view"
|
139 |
| - assert np.may_share_memory(df2["a"].values, df["a"].values) |
| 142 | + assert np.shares_memory(df2["a"].values, df["a"].values) |
140 | 143 |
|
141 | 144 | # mutating df2 triggers a copy-on-write for that column/block
|
142 | 145 | df2.iloc[0, 0] = 0
|
143 | 146 | if using_copy_on_write:
|
144 |
| - assert not np.may_share_memory(df2["a"].values, df["a"].values) |
| 147 | + assert not np.shares_memory(df2["a"].values, df["a"].values) |
145 | 148 | tm.assert_frame_equal(df, df_orig)
|
146 | 149 | else:
|
147 | 150 | # but currently select_dtypes() actually returns a view -> mutates parent
|
148 | 151 | df_orig.iloc[0, 0] = 0
|
149 | 152 | tm.assert_frame_equal(df, df_orig)
|
| 153 | + |
| 154 | + |
| 155 | +def test_to_frame(using_copy_on_write): |
| 156 | + # Case: converting a Series to a DataFrame with to_frame |
| 157 | + ser = Series([1, 2, 3]) |
| 158 | + ser_orig = ser.copy() |
| 159 | + |
| 160 | + df = ser.to_frame() |
| 161 | + |
| 162 | + # currently this always returns a "view" |
| 163 | + assert np.shares_memory(ser.values, df._get_column_array(0)) |
| 164 | + |
| 165 | + df.iloc[0, 0] = 0 |
| 166 | + |
| 167 | + if using_copy_on_write: |
| 168 | + # mutating df triggers a copy-on-write for that column |
| 169 | + assert not np.shares_memory(ser.values, df[0].values) |
| 170 | + tm.assert_series_equal(ser, ser_orig) |
| 171 | + else: |
| 172 | + # but currently select_dtypes() actually returns a view -> mutates parent |
| 173 | + ser_orig.iloc[0] = 0 |
| 174 | + tm.assert_series_equal(ser, ser_orig) |
0 commit comments