Skip to content

Commit f7917ca

Browse files
Backport PR #52276 on branch 2.0.x (API / CoW: Respect CoW for DataFrame(Index)) (#52289)
Backport PR #52276: API / CoW: Respect CoW for DataFrame(Index) Co-authored-by: Patrick Hoefler <[email protected]>
1 parent bca610a commit f7917ca

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

doc/source/whatsnew/v2.0.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ Copy-on-Write improvements
191191
of those Series objects for the columns of the DataFrame (:issue:`50777`)
192192

193193
- The :class:`DataFrame` constructor, when constructing a DataFrame from a
194-
:class:`Series` and specifying ``copy=False``, will now respect Copy-on-Write.
194+
:class:`Series` or :class:`Index` and specifying ``copy=False``, will
195+
now respect Copy-on-Write.
195196

196197
- The :class:`DataFrame` and :class:`Series` constructors, when constructing from
197198
a NumPy array, will now copy the array by default to avoid mutating

pandas/core/internals/construction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def ndarray_to_mgr(
291291
if values.ndim == 1:
292292
values = values.reshape(-1, 1)
293293

294-
elif isinstance(values, ABCSeries):
294+
elif isinstance(values, (ABCSeries, Index)):
295295
if not copy_on_sanitize and (
296296
dtype is None or astype_is_view(values.dtype, dtype)
297297
):
@@ -304,7 +304,7 @@ def ndarray_to_mgr(
304304

305305
values = _ensure_2d(values)
306306

307-
elif isinstance(values, (np.ndarray, ExtensionArray, Index)):
307+
elif isinstance(values, (np.ndarray, ExtensionArray)):
308308
# drop subclass info
309309
_copy = (
310310
copy_on_sanitize

pandas/tests/copy_view/test_constructors.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -260,26 +260,28 @@ def test_dataframe_from_dict_of_series_with_reindex(dtype):
260260
assert np.shares_memory(arr_before, arr_after)
261261

262262

263+
@pytest.mark.parametrize("cons", [Series, Index])
263264
@pytest.mark.parametrize(
264265
"data, dtype", [([1, 2], None), ([1, 2], "int64"), (["a", "b"], None)]
265266
)
266-
def test_dataframe_from_series(using_copy_on_write, data, dtype):
267-
ser = Series(data, dtype=dtype)
268-
ser_orig = ser.copy()
269-
df = DataFrame(ser, dtype=dtype)
270-
assert np.shares_memory(get_array(ser), get_array(df, 0))
267+
def test_dataframe_from_series_or_index(using_copy_on_write, data, dtype, cons):
268+
obj = cons(data, dtype=dtype)
269+
obj_orig = obj.copy()
270+
df = DataFrame(obj, dtype=dtype)
271+
assert np.shares_memory(get_array(obj), get_array(df, 0))
271272
if using_copy_on_write:
272273
assert not df._mgr._has_no_reference(0)
273274

274275
df.iloc[0, 0] = data[-1]
275276
if using_copy_on_write:
276-
tm.assert_series_equal(ser, ser_orig)
277+
tm.assert_equal(obj, obj_orig)
277278

278279

279-
def test_dataframe_from_series_different_dtype(using_copy_on_write):
280-
ser = Series([1, 2], dtype="int64")
281-
df = DataFrame(ser, dtype="int32")
282-
assert not np.shares_memory(get_array(ser), get_array(df, 0))
280+
@pytest.mark.parametrize("cons", [Series, Index])
281+
def test_dataframe_from_series_or_index_different_dtype(using_copy_on_write, cons):
282+
obj = cons([1, 2], dtype="int64")
283+
df = DataFrame(obj, dtype="int32")
284+
assert not np.shares_memory(get_array(obj), get_array(df, 0))
283285
if using_copy_on_write:
284286
assert df._mgr._has_no_reference(0)
285287

0 commit comments

Comments
 (0)