Skip to content

PERF: Only copy in plotting when needed #58958

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 2 commits into from
Jun 14, 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
10 changes: 3 additions & 7 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,10 +982,7 @@ def __call__(self, *args, **kwargs):
f"Valid plot kinds: {self._all_kinds}"
)

# The original data structured can be transformed before passed to the
# backend. For example, for DataFrame is common to set the index as the
# `x` parameter, and return a Series with the parameter `y` as values.
data = self._parent.copy()
data = self._parent

if isinstance(data, ABCSeries):
kwargs["reuse_plot"] = True
Expand All @@ -1005,7 +1002,7 @@ def __call__(self, *args, **kwargs):
if is_integer(y) and not holds_integer(data.columns):
y = data.columns[y]
# converted to series actually. copy to not modify
data = data[y].copy()
data = data[y].copy(deep=False)
data.index.name = y
elif isinstance(data, ABCDataFrame):
data_cols = data.columns
Expand All @@ -1032,8 +1029,7 @@ def __call__(self, *args, **kwargs):
except (IndexError, KeyError, TypeError):
pass

# don't overwrite
data = data[y].copy()
data = data[y]

if isinstance(data, ABCSeries):
label_name = label_kw or y
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ def test_boxplot_return_type_invalid_type(self, return_type):

def test_kde_df(self):
pytest.importorskip("scipy")
df = DataFrame(np.random.default_rng(2).standard_normal((100, 4)))
df = DataFrame(np.random.default_rng(2).standard_normal((10, 4)))
ax = _check_plot_works(df.plot, kind="kde")
expected = [pprint_thing(c) for c in df.columns]
_check_legend_labels(ax, labels=expected)
Expand Down