Skip to content

Backport PR #52054 on branch 2.0.x (PERF: Improve performance with copy=True and different dtype) #52088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
npt,
)

from pandas.core.dtypes.astype import astype_is_view
from pandas.core.dtypes.cast import (
construct_1d_arraylike_from_scalar,
dict_compat,
Expand Down Expand Up @@ -291,7 +292,12 @@ def ndarray_to_mgr(

elif isinstance(values, (np.ndarray, ExtensionArray, ABCSeries, Index)):
# drop subclass info
values = np.array(values, copy=copy_on_sanitize)
_copy = (
copy_on_sanitize
if (dtype is None or astype_is_view(values.dtype, dtype))
else False
)
values = np.array(values, copy=_copy)
values = _ensure_2d(values)

else:
Expand Down