Skip to content

BUG: Plot with label would overwrite index name #8541

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
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
1 change: 0 additions & 1 deletion doc/source/whatsnew/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

5 changes: 5 additions & 0 deletions doc/source/whatsnew/v0.15.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
8 changes: 8 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 3 additions & 2 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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']:
Expand Down