Skip to content

Commit

Permalink
BUG: Categorical scatter plot has KeyError pandas-dev#16199 (pandas-d…
Browse files Browse the repository at this point in the history
…ev#16208)

* BUG: Categorical scatter plot has KeyError pandas-dev#16199

Appropriately handles categorical data for dataframe scatter plots which
currently raises KeyError for categorical data

* Add to whatsnew
  • Loading branch information
stangirala authored and TomAugspurger committed Jun 12, 2017
1 parent b72519e commit 11d274f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Performance Improvements

Bug Fixes
~~~~~~~~~
- Fixed issue with dataframe scatter plot for categorical data that reports incorrect column key not found when categorical data is used for plotting (:issue:`16199`)



Expand Down
5 changes: 5 additions & 0 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,11 @@ def __init__(self, data, x, y, **kwargs):
x = self.data.columns[x]
if is_integer(y) and not self.data.columns.holds_integer():
y = self.data.columns[y]
if len(self.data[x]._get_numeric_data()) == 0:
raise ValueError(self._kind + ' requires x column to be numeric')
if len(self.data[y]._get_numeric_data()) == 0:
raise ValueError(self._kind + ' requires y column to be numeric')

self.x = x
self.y = y

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,24 @@ def test_plot_scatter(self):
axes = df.plot(x='x', y='y', kind='scatter', subplots=True)
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))

@slow
def test_plot_scatter_with_categorical_data(self):
# GH 16199
df = pd.DataFrame({'x': [1, 2, 3, 4],
'y': pd.Categorical(['a', 'b', 'a', 'c'])})

with pytest.raises(ValueError) as ve:
df.plot(x='x', y='y', kind='scatter')
ve.match('requires y column to be numeric')

with pytest.raises(ValueError) as ve:
df.plot(x='y', y='x', kind='scatter')
ve.match('requires x column to be numeric')

with pytest.raises(ValueError) as ve:
df.plot(x='y', y='y', kind='scatter')
ve.match('requires x column to be numeric')

@slow
def test_plot_scatter_with_c(self):
df = DataFrame(randn(6, 4),
Expand Down

0 comments on commit 11d274f

Please sign in to comment.