Skip to content

Commit 53378fd

Browse files
committed
BUG: Categorical scatter plot has KeyError pandas-dev#16199
Appropriately handles categorical data for dataframe scatter plots which currently raises KeyError for categorical data
1 parent 39cc1d0 commit 53378fd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pandas/plotting/_core.py

+5
Original file line numberDiff line numberDiff line change
@@ -770,10 +770,15 @@ def __init__(self, data, x, y, **kwargs):
770770
MPLPlot.__init__(self, data, **kwargs)
771771
if x is None or y is None:
772772
raise ValueError(self._kind + ' requires and x and y column')
773+
if len(self.data[x]._get_numeric_data()) == 0:
774+
raise ValueError(self._kind + ' requires x column to be numeric')
775+
if len(self.data[y]._get_numeric_data()) == 0:
776+
raise ValueError(self._kind + ' requires y column to be numeric')
773777
if is_integer(x) and not self.data.columns.holds_integer():
774778
x = self.data.columns[x]
775779
if is_integer(y) and not self.data.columns.holds_integer():
776780
y = self.data.columns[y]
781+
777782
self.x = x
778783
self.y = y
779784

pandas/tests/plotting/test_frame.py

+18
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,24 @@ def test_plot_scatter(self):
909909
axes = df.plot(x='x', y='y', kind='scatter', subplots=True)
910910
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
911911

912+
@slow
913+
def test_plot_scatter_with_categorical_data(self):
914+
# GH 16199
915+
df = pd.DataFrame({'x': [1, 2, 3, 4],
916+
'y': pd.Categorical(['a', 'b', 'a', 'c'])})
917+
918+
with pytest.raises(ValueError) as ve:
919+
df.plot(x='x', y='y', kind='scatter')
920+
ve.match('requires y column to be numeric')
921+
922+
with pytest.raises(ValueError) as ve:
923+
df.plot(x='y', y='x', kind='scatter')
924+
ve.match('requires x column to be numeric')
925+
926+
with pytest.raises(ValueError) as ve:
927+
df.plot(x='y', y='y', kind='scatter')
928+
ve.match('requires x column to be numeric')
929+
912930
@slow
913931
def test_plot_scatter_with_c(self):
914932
df = DataFrame(randn(6, 4),

0 commit comments

Comments
 (0)