Skip to content

Commit

Permalink
Backport PR #52054 on branch 2.0.x (PERF: Improve performance with co…
Browse files Browse the repository at this point in the history
…py=True and different dtype) (#52088)

Backport PR #52054: PERF: Improve performance with copy=True and different dtype

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Mar 20, 2023
1 parent 51121ee commit eccf9bd
Showing 1 changed file with 7 additions and 1 deletion.
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

0 comments on commit eccf9bd

Please sign in to comment.