Skip to content

API: Improper x/y arg given to df.plot #18695

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 4 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 0 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ Other API Changes
- :func:`pandas.DataFrame.merge` no longer casts a ``float`` column to ``object`` when merging on ``int`` and ``float`` columns (:issue:`16572`)
- The default NA value for :class:`UInt64Index` has changed from 0 to ``NaN``, which impacts methods that mask with NA, such as ``UInt64Index.where()`` (:issue:`18398`)
- Refactored ``setup.py`` to use ``find_packages`` instead of explicitly listing out all subpackages (:issue:`18535`)
- :func: `DataFrame.plot` now raises a ``ValueError`` when the ``x`` or ``y`` argument is improperly formed (:issue:`18671`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can move this to plotting bugs.


.. _whatsnew_0220.deprecations:

Expand Down
6 changes: 5 additions & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ def _plot(data, x=None, y=None, subplots=False,
else:
raise ValueError("%r is not a valid plot kind" % kind)

from pandas import DataFrame
from pandas import DataFrame, Series
if kind in _dataframe_kinds:
if isinstance(data, DataFrame):
plot_obj = klass(data, x=x, y=y, subplots=subplots, ax=ax,
Expand All @@ -1706,11 +1706,15 @@ def _plot(data, x=None, y=None, subplots=False,
if x is not None:
if is_integer(x) and not data.columns.holds_integer():
x = data.columns[x]
elif not isinstance(data[x], Series):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from pandas.core.dtypes.generic import ABCSeries, ABCDataFrame

not isinstance(data[x], ABCSeries)

and change the DataFrame tests to use ABCDataFrame (and remove the inline import)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and change the DataFrame tests to use ABCDataFrame (and remove the inline import)

Not sure what you mean here? I changed the test to use ABCDataFrame but that doesn't seem right?

raise ValueError("x must be a label or position")
data = data.set_index(x)

if y is not None:
if is_integer(y) and not data.columns.holds_integer():
y = data.columns[y]
elif not isinstance(data[y], Series):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I do df.plot.line(x='x', y=['y1', 'y2', 'y3']) this is now a ValueError?

raise ValueError("y must be a label or position")
label = kwds['label'] if 'label' in kwds else y
series = data[y].copy() # Don't modify
series.name = label
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,15 @@ def test_invalid_kind(self):
with pytest.raises(ValueError):
df.plot(kind='aasdf')

def test_invalid_xy_args(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parameterize this on x, y

df = DataFrame({"A": [1, 2], 'B': [3, 4], 'C': [5, 6]})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can yu add the issue number here

bad_arg = ['B', 'C']
valid_arg = 'A'
with pytest.raises(ValueError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add a dataframe with duplicate columns as a test, e.g.

In [6]: df = DataFrame([[1, 3, 5], [2, 4, 6]], columns=list('AAB'))

In [7]: df
Out[7]: 
   A  A  B
0  1  3  5
1  2  4  6

In [8]: df['A']
Out[8]: 
   A  A
0  1  3
1  2  4

df.plot(x=bad_arg, y=valid_arg)
with pytest.raises(ValueError):
df.plot(x=valid_arg, y=bad_arg)

@pytest.mark.slow
def test_hexbin_basic(self):
df = self.hexbin_df
Expand Down