Skip to content

Cannot plot empty Series #27758

Closed
Closed
@randomstuff

Description

@randomstuff

Code Sample

import pandas as pd
import matplotlib.pyplot as plt

ax = plt.subplots(1)[1]
ax.set_xlim((0, 10))
ax.set_ylim((0, 10))
x = pd.Series([], index=pd.Index([], dtype=float), dtype=float)
x.plot(ax=ax)

Problem description

It is not possible to .plot() an empty Series or DataFrame. When .plot() is called on an empty Series or DataFrame, an exception is raised:

~/software/venv-scipy/lib/python3.7/site-packages/pandas/plotting/_matplotlib/core.py in _compute_plot_data(self)
    412         # no non-numeric frames or series allowed
    413         if is_empty:
--> 414             raise TypeError("no numeric data to plot")
    415 
    416         # GH25587: cast ExtensionArray of pandas (IntegerArray, etc.) to

TypeError: no numeric data to plot

This is problematic because code path which are supposed to work can suddenly fail when the data to plot is empty (for example, there was no data for the requested time-range).

In order to be robust to this, the code needs to include a lot of things such as:

if not x.empty:
    x.plot(ax=ax)
``

#### Expected Output

#### Output of ``pd.show_versions()``

<details>
INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.4.final.0
python-bits      : 64
OS               : Linux
OS-release       : 4.19.0-5-amd64
machine          : x86_64
processor        : 
byteorder        : little
LC_ALL           : None
LANG             : fr_FR.utf8
LOCALE           : fr_FR.UTF-8

pandas           : 0.25.0
numpy            : 1.17.0
pytz             : 2019.2
dateutil         : 2.8.0
pip              : 18.1
setuptools       : 40.8.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.10.1
IPython          : 7.7.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : 3.1.1
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None
</details>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions