Skip to content

Commit 1af5d08

Browse files
author
Gabriel Corona
committed
Don't fail when plotting Series/DataFrame with no row
When Series or DataFrames was empty (no cells) after removing columns without a suitable type (.select_dtypes), pandas was throwing a "no numeric data to plot" exception. This can happen: 1) either because there is no column with a suitable type; 2) or if there is no row. Raising an exception in the first case makes sense but we should probably avoid throwing an exception in the second case.
1 parent 03b3c8f commit 1af5d08

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def _compute_plot_data(self):
418418
numeric_data = data.select_dtypes(include=include_type, exclude=exclude_type)
419419

420420
try:
421-
is_empty = numeric_data.empty
421+
is_empty = numeric_data.columns.empty
422422
except AttributeError:
423423
is_empty = not len(numeric_data)
424424

pandas/tests/plotting/test_frame.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,6 +3229,20 @@ def test_subplots_sharex_false(self):
32293229
tm.assert_numpy_array_equal(axs[0].get_xticks(), expected_ax1)
32303230
tm.assert_numpy_array_equal(axs[1].get_xticks(), expected_ax2)
32313231

3232+
def test_plot_no_rows(self):
3233+
df = pd.DataFrame({"foo": [1]}, index=pd.Index(data=[], dtype=int), dtype="int")
3234+
assert df.empty
3235+
ax = df.plot()
3236+
assert len(ax.get_lines()) == 1
3237+
line = ax.get_lines()[0]
3238+
assert len(line.get_xdata()) == 0
3239+
assert len(line.get_ydata()) == 0
3240+
3241+
def test_plot_no_numeric_data(self):
3242+
df = pd.DataFrame(["a", "b", "c"])
3243+
with pytest.raises(TypeError):
3244+
df.plot()
3245+
32323246

32333247
def _generate_4_axes_via_gridspec():
32343248
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)