Skip to content

Commit d7992fe

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

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
@@ -774,6 +774,11 @@ def __init__(self, data, x, y, **kwargs):
774774
x = self.data.columns[x]
775775
if is_integer(y) and not self.data.columns.holds_integer():
776776
y = self.data.columns[y]
777+
if len(self.data[x]._get_numeric_data()) == 0:
778+
raise ValueError(self._kind + ' requires x column to be numeric')
779+
if len(self.data[y]._get_numeric_data()) == 0:
780+
raise ValueError(self._kind + ' requires y column to be numeric')
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)