Skip to content

Commit 7b93b06

Browse files
authored
PERF: Improve performance with copy=True and different dtype (#52054)
* PERF: Improve performance with copy=True and different dtype * Switch copy back
1 parent 65cb7c4 commit 7b93b06

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

pandas/core/internals/construction.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from pandas._libs import lib
1919

20+
from pandas.core.dtypes.astype import astype_is_view
2021
from pandas.core.dtypes.cast import (
2122
construct_1d_arraylike_from_scalar,
2223
dict_compat,
@@ -293,7 +294,12 @@ def ndarray_to_mgr(
293294

294295
elif isinstance(values, (np.ndarray, ExtensionArray, ABCSeries, Index)):
295296
# drop subclass info
296-
values = np.array(values, copy=copy_on_sanitize)
297+
_copy = (
298+
copy_on_sanitize
299+
if (dtype is None or astype_is_view(values.dtype, dtype))
300+
else False
301+
)
302+
values = np.array(values, copy=_copy)
297303
values = _ensure_2d(values)
298304

299305
else:

0 commit comments

Comments
 (0)