Skip to content

Commit 1e7f22d

Browse files
committed
PERF: DataFrame(ndarr, copy=True)
1 parent 3ce07cb commit 1e7f22d

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Performance improvements
195195
- Performance improvement in :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` when ``verify_integrity=True`` (:issue:`51873`)
196196
- Performance improvement in :func:`factorize` for object columns not containing strings (:issue:`51921`)
197197
- Performance improvement in :class:`Series` reductions (:issue:`52341`)
198+
- Constructing :class:`DataFrame` instances from a 2d ``ndarray`` using ``copy=True`` now ensures the copied array is contiguous, giving performance improvements for many operations (:issue:`52438`)
198199
-
199200

200201
.. ---------------------------------------------------------------------------

pandas/core/internals/construction.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ def ndarray_to_mgr(
312312
if (dtype is None or astype_is_view(values.dtype, dtype))
313313
else False
314314
)
315-
values = np.array(values, copy=_copy)
315+
_order = "F" if _copy else "K"
316+
values = np.array(values, copy=_copy, order=_order)
316317
values = _ensure_2d(values)
317318

318319
else:

pandas/tests/frame/test_constructors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,7 @@ def test_constructor_ndarray_copy(
21232123
df = DataFrame(arr, copy=True)
21242124
arr[6] = 6
21252125
assert not (df.values[6] == 6).all()
2126+
assert df._mgr.arrays[0].flags.c_contiguous
21262127
else:
21272128
arr = float_frame.values.copy()
21282129
# default: copy to ensure contiguous arrays

0 commit comments

Comments
 (0)