From acbdcff64b14b753917e1a7bc8815bd14564b0fe Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 16 Feb 2024 17:56:11 +0100 Subject: [PATCH 1/2] PERF: DataFrame(ndarray) constructor ensure to copy to column-major layout --- pandas/core/internals/construction.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 047c25f4931a6..990d6bd26268e 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -248,10 +248,10 @@ def ndarray_to_mgr( elif isinstance(values, (np.ndarray, ExtensionArray)): # drop subclass info - _copy = ( - copy if (dtype is None or astype_is_view(values.dtype, dtype)) else False - ) - values = np.array(values, copy=_copy) + if dtype is None or astype_is_view(values.dtype, dtype): + values = np.array(values, copy=True, order="F") + else: + values = np.array(values, copy=False) values = _ensure_2d(values) else: From bad7dd666cffb33f62ad38f83586d8ddf77d54f8 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 19 Feb 2024 14:09:46 +0100 Subject: [PATCH 2/2] fixup --- pandas/core/internals/construction.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 990d6bd26268e..898c7f01a0ae8 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -248,7 +248,9 @@ def ndarray_to_mgr( elif isinstance(values, (np.ndarray, ExtensionArray)): # drop subclass info - if dtype is None or astype_is_view(values.dtype, dtype): + if copy and (dtype is None or astype_is_view(values.dtype, dtype)): + # only force a copy now if copy=True was requested + # and a subsequent `astype` will not already result in a copy values = np.array(values, copy=True, order="F") else: values = np.array(values, copy=False)