From af1a5568f08ff957f724e2c0b457e707b5363ee1 Mon Sep 17 00:00:00 2001 From: TomAugspurger Date: Sat, 11 Oct 2014 14:37:55 -0500 Subject: [PATCH] BUG: Do not overwrite index name with plot --- doc/source/whatsnew/v0.15.0.txt | 1 - doc/source/whatsnew/v0.15.1.txt | 5 +++++ pandas/tests/test_graphics.py | 8 ++++++++ pandas/tools/plotting.py | 5 +++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.15.0.txt b/doc/source/whatsnew/v0.15.0.txt index 5d7598b749feb..b6b36ce8c1bf9 100644 --- a/doc/source/whatsnew/v0.15.0.txt +++ b/doc/source/whatsnew/v0.15.0.txt @@ -1149,4 +1149,3 @@ Bug Fixes - Suppress FutureWarning generated by NumPy when comparing object arrays containing NaN for equality (:issue:`7065`) - Bug in ``DataFrame.eval()`` where the dtype of the ``not`` operator (``~``) was not correctly inferred as ``bool``. - diff --git a/doc/source/whatsnew/v0.15.1.txt b/doc/source/whatsnew/v0.15.1.txt index cd695ab5f992c..1f50470e4cca9 100644 --- a/doc/source/whatsnew/v0.15.1.txt +++ b/doc/source/whatsnew/v0.15.1.txt @@ -138,3 +138,8 @@ Bug Fixes - Fix ``shape`` attribute for ``MultiIndex`` (:issue:`8609`) - Bug in ``GroupBy`` where a name conflict between the grouper and columns would break ``groupby`` operations (:issue:`7115`, :issue:`8112`) + + + +- Fixed a bug where plotting a column ``y`` and specifying a label +would mutate the index name of the DataFrame ``y`` came from (:issue:`8494`) diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index 45814795ec060..a5d203d688b16 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -1058,6 +1058,14 @@ def test_explicit_label(self): ax = df.plot(x='a', y='b', label='LABEL') self._check_text_labels(ax.xaxis.get_label(), 'LABEL') + @slow + def test_donot_overwrite_index_name(self): + # GH 8494 + df = DataFrame(randn(2, 2), columns=['a', 'b']) + df.index.name = 'NAME' + df.plot(y='b', label='LABEL') + self.assertEqual(df.index.name, 'NAME') + @slow def test_plot_xy(self): # columns.inferred_type == 'string' diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 0e477d8eedb98..9065e0a340aaa 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -2261,7 +2261,8 @@ def _plot(data, x=None, y=None, subplots=False, elif y is not None: if com.is_integer(y) and not data.columns.holds_integer(): y = data.columns[y] - data = data[y] # converted to series actually + # converted to series actually. copy to not modify + data = data[y].copy() data.index.name = y plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds) else: @@ -2276,7 +2277,7 @@ def _plot(data, x=None, y=None, subplots=False, y = data.columns[y] label = x if x is not None else data.index.name label = kwds.pop('label', label) - series = data[y] + series = data[y].copy() # Don't modify series.index.name = label for kw in ['xerr', 'yerr']: