From 36cb4b0c671e52d0a89dd904338391be0cb118ee Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 7 Jun 2024 11:19:38 -0700 Subject: [PATCH 1/2] PERF: Only copy in plotting when needed --- pandas/plotting/_core.py | 11 +++++------ pandas/tests/plotting/frame/test_frame.py | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 0daf3cfafe81c..097d45a7eb756 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -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 @@ -1032,8 +1029,10 @@ def __call__(self, *args, **kwargs): except (IndexError, KeyError, TypeError): pass - # don't overwrite - data = data[y].copy() + data = data[y] + if x is None: + # don't overwrite + data = data.copy() if isinstance(data, ABCSeries): label_name = label_kw or y diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index e809bd33610f1..b381c4fce8430 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -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) From 6f2724e183ef655e10e687afe67061faa2438c3e Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 7 Jun 2024 11:39:35 -0700 Subject: [PATCH 2/2] Remove more unnecessary copies --- pandas/plotting/_core.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 097d45a7eb756..0a29ab530c2fc 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1002,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 @@ -1030,9 +1030,6 @@ def __call__(self, *args, **kwargs): pass data = data[y] - if x is None: - # don't overwrite - data = data.copy() if isinstance(data, ABCSeries): label_name = label_kw or y