Skip to content

Commit

Permalink
PERF: Only copy in plotting when needed (#58958)
Browse files Browse the repository at this point in the history
* PERF: Only copy in plotting when needed

* Remove more unnecessary copies
  • Loading branch information
mroeschke committed Jun 14, 2024
1 parent 6895f74 commit 0fb5cfe
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
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

0 comments on commit 0fb5cfe

Please sign in to comment.