Skip to content

Commit 20be789

Browse files
author
Tom Augspurger
committed
Merge pull request #8541 from TomAugspurger/plot-label-index-name
BUG: Plot with ``label`` would overwrite index name
2 parents 32a2451 + af1a556 commit 20be789

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

doc/source/whatsnew/v0.15.0.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1149,4 +1149,3 @@ Bug Fixes
11491149
- Suppress FutureWarning generated by NumPy when comparing object arrays containing NaN for equality (:issue:`7065`)
11501150
- Bug in ``DataFrame.eval()`` where the dtype of the ``not`` operator (``~``)
11511151
was not correctly inferred as ``bool``.
1152-

doc/source/whatsnew/v0.15.1.txt

+5
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,8 @@ Bug Fixes
138138
- Fix ``shape`` attribute for ``MultiIndex`` (:issue:`8609`)
139139
- Bug in ``GroupBy`` where a name conflict between the grouper and columns
140140
would break ``groupby`` operations (:issue:`7115`, :issue:`8112`)
141+
142+
143+
144+
- Fixed a bug where plotting a column ``y`` and specifying a label
145+
would mutate the index name of the DataFrame ``y`` came from (:issue:`8494`)

pandas/tests/test_graphics.py

+8
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,14 @@ def test_explicit_label(self):
10581058
ax = df.plot(x='a', y='b', label='LABEL')
10591059
self._check_text_labels(ax.xaxis.get_label(), 'LABEL')
10601060

1061+
@slow
1062+
def test_donot_overwrite_index_name(self):
1063+
# GH 8494
1064+
df = DataFrame(randn(2, 2), columns=['a', 'b'])
1065+
df.index.name = 'NAME'
1066+
df.plot(y='b', label='LABEL')
1067+
self.assertEqual(df.index.name, 'NAME')
1068+
10611069
@slow
10621070
def test_plot_xy(self):
10631071
# columns.inferred_type == 'string'

pandas/tools/plotting.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2261,7 +2261,8 @@ def _plot(data, x=None, y=None, subplots=False,
22612261
elif y is not None:
22622262
if com.is_integer(y) and not data.columns.holds_integer():
22632263
y = data.columns[y]
2264-
data = data[y] # converted to series actually
2264+
# converted to series actually. copy to not modify
2265+
data = data[y].copy()
22652266
data.index.name = y
22662267
plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
22672268
else:
@@ -2276,7 +2277,7 @@ def _plot(data, x=None, y=None, subplots=False,
22762277
y = data.columns[y]
22772278
label = x if x is not None else data.index.name
22782279
label = kwds.pop('label', label)
2279-
series = data[y]
2280+
series = data[y].copy() # Don't modify
22802281
series.index.name = label
22812282

22822283
for kw in ['xerr', 'yerr']:

0 commit comments

Comments
 (0)