Skip to content

PERF: Use shallow copies/remove unnecessary copies in reshaping #58959

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 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ def melt(
if value_vars_was_not_none:
frame = frame.iloc[:, algos.unique(idx)]
else:
frame = frame.copy()
frame = frame.copy(deep=False)
else:
frame = frame.copy()
frame = frame.copy(deep=False)

if col_level is not None: # allow list or other?
# frame is a copy
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,6 @@ def _all_key(key):
piece = piece.T
all_key = _all_key(key)

# we are going to mutate this, so need to copy!
piece = piece.copy()
piece[all_key] = margin[key]

table_pieces.append(piece)
Expand Down Expand Up @@ -842,11 +840,11 @@ def pivot(
# If columns is None we will create a MultiIndex level with None as name
# which might cause duplicated names because None is the default for
# level names
data = data.copy(deep=False)
data.index = data.index.copy()
data.index.names = [
name if name is not None else lib.no_default for name in data.index.names
]
if any(name is None for name in data.index.names):
data = data.copy(deep=False)
data.index.names = [
name if name is not None else lib.no_default for name in data.index.names
]

indexed: DataFrame | Series
if values is lib.no_default:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def _unstack_multiple(
)

if isinstance(data, Series):
dummy = data.copy()
dummy = data.copy(deep=False)
dummy.index = dummy_index

unstacked = dummy.unstack("__placeholder__", fill_value=fill_value, sort=sort)
Expand Down Expand Up @@ -1025,7 +1025,7 @@ def stack_reshape(
buf = []
for idx in stack_cols.unique():
if len(frame.columns) == 1:
data = frame.copy()
data = frame.copy(deep=False)
else:
if not isinstance(frame.columns, MultiIndex) and not isinstance(idx, tuple):
# GH#57750 - if the frame is an Index with tuples, .loc below will fail
Expand Down