Skip to content

Commit eccf9bd

Browse files
Backport PR pandas-dev#52054 on branch 2.0.x (PERF: Improve performance with copy=True and different dtype) (pandas-dev#52088)
Backport PR pandas-dev#52054: PERF: Improve performance with copy=True and different dtype Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 51121ee commit eccf9bd

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
@@ -22,6 +22,7 @@
2222
npt,
2323
)
2424

25+
from pandas.core.dtypes.astype import astype_is_view
2526
from pandas.core.dtypes.cast import (
2627
construct_1d_arraylike_from_scalar,
2728
dict_compat,
@@ -291,7 +292,12 @@ def ndarray_to_mgr(
291292

292293
elif isinstance(values, (np.ndarray, ExtensionArray, ABCSeries, Index)):
293294
# drop subclass info
294-
values = np.array(values, copy=copy_on_sanitize)
295+
_copy = (
296+
copy_on_sanitize
297+
if (dtype is None or astype_is_view(values.dtype, dtype))
298+
else False
299+
)
300+
values = np.array(values, copy=_copy)
295301
values = _ensure_2d(values)
296302

297303
else:

0 commit comments

Comments
 (0)