Skip to content
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

PERF: DataFrame.copy preserve row/column order #44871

Closed
wants to merge 11 commits into from
15 changes: 13 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,14 @@ def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
result = to_native_types(self.values, na_rep=na_rep, quoting=quoting, **kwargs)
return self.make_block(result)

@final
def copy(self, deep: bool = True):
"""copy constructor"""
values = self.values
if deep:
values = values.copy()
# "K" -> retain 'order' where possible, can be significantly
# faster than the default.
# error: Too many arguments for "copy" of "ExtensionArray"
values = values.copy("K") # type: ignore[call-arg]
return type(self)(values, placement=self._mgr_locs, ndim=self.ndim)

# ---------------------------------------------------------------------
Expand Down Expand Up @@ -1699,6 +1701,15 @@ def _unwrap_setitem_indexer(self, indexer):
)
return indexer

def copy(self, deep: bool = True):
"""copy constructor"""
values = self.values
if deep:
# The base class method passes order="K", which is not part of
# the EA API.
values = values.copy()
return type(self)(values, placement=self._mgr_locs, ndim=self.ndim)

@property
def is_view(self) -> bool:
"""Extension arrays are never treated as views."""
Expand Down