Skip to content

Commit 523b3cd

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 523b3cd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,6 +3229,22 @@ 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]},
3234+
index=pd.Index(data=[], dtype=int),
3235+
dtype="int")
3236+
assert df.empty
3237+
ax = df.plot()
3238+
assert len(ax.get_lines()) == 1
3239+
line = ax.get_lines()[0]
3240+
assert len(line.get_xdata()) == 0
3241+
assert len(line.get_ydata()) == 0
3242+
3243+
def test_plot_no_numeric_data(self):
3244+
df = pd.DataFrame(["a", "b", "c"])
3245+
with pytest.raises(TypeError):
3246+
df.plot()
3247+
32323248

32333249
def _generate_4_axes_via_gridspec():
32343250
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)