Skip to content

Commit 261c3a6

Browse files
randomstuffTomAugspurger
authored andcommitted
API: Don't fail when plotting Series/DataFrame with no rows (#28226)
1 parent 9bb82b3 commit 261c3a6

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Plotting
190190

191191
- Bug in :meth:`Series.plot` not able to plot boolean values (:issue:`23719`)
192192
-
193+
- Bug in :meth:`DataFrame.plot` not able to plot when no rows (:issue:`27758`)
193194
- Bug in :meth:`DataFrame.plot` producing incorrect legend markers when plotting multiple series on the same axis (:issue:`18222`)
194195
- Bug in :meth:`DataFrame.plot` when ``kind='box'`` and data contains datetime or timedelta data. These types are now automatically dropped (:issue:`22799`)
195196
- Bug in :meth:`DataFrame.plot.line` and :meth:`DataFrame.plot.area` produce wrong xlim in x-axis (:issue:`27686`, :issue:`25160`, :issue:`24784`)

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,6 +3229,21 @@ 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+
# GH 27758
3234+
df = pd.DataFrame(columns=["foo"], dtype=int)
3235+
assert df.empty
3236+
ax = df.plot()
3237+
assert len(ax.get_lines()) == 1
3238+
line = ax.get_lines()[0]
3239+
assert len(line.get_xdata()) == 0
3240+
assert len(line.get_ydata()) == 0
3241+
3242+
def test_plot_no_numeric_data(self):
3243+
df = pd.DataFrame(["a", "b", "c"])
3244+
with pytest.raises(TypeError):
3245+
df.plot()
3246+
32323247

32333248
def _generate_4_axes_via_gridspec():
32343249
import matplotlib.pyplot as plt

pandas/tests/plotting/test_series.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,3 +909,18 @@ def test_plot_xlim_for_series(self, kind):
909909

910910
assert xlims[0] < 0
911911
assert xlims[1] > 1
912+
913+
def test_plot_no_rows(self):
914+
# GH 27758
915+
df = pd.Series(dtype=int)
916+
assert df.empty
917+
ax = df.plot()
918+
assert len(ax.get_lines()) == 1
919+
line = ax.get_lines()[0]
920+
assert len(line.get_xdata()) == 0
921+
assert len(line.get_ydata()) == 0
922+
923+
def test_plot_no_numeric_data(self):
924+
df = pd.Series(["a", "b", "c"])
925+
with pytest.raises(TypeError):
926+
df.plot()

0 commit comments

Comments
 (0)