Skip to content

BUG: IndexError when plotting empty dataframe subset with DatetimeIndex on an existing axis #39705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 3 tasks
matthme opened this issue Feb 9, 2021 · 0 comments
Open
2 of 3 tasks
Labels

Comments

@matthme
Copy link

matthme commented Feb 9, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

# Case 1 - no error raised:
df = pd.DataFrame({"col1":[1,2,3]})
ax = df.plot()
df[df["col1"]==9].plot(ax=ax)

# Case 2 - error raised:
df = pd.DataFrame({"col1":[1,2,3]}, index=pd.date_range("2020-01-01 00:00:00", periods=3, freq="10s"))
ax = df.plot()
df[df["col1"]==9].plot(ax=ax)

Problem description

IndexError is raised when trying to plot an empty subset of a dataframe that has a DatetimeIndex to an already existing axis (case 2). The better option would be that no error is raised and simply nothing is added to the existing axis as is the case if the index is a normal index instead of a DatetimeIndex (case 1). This is better becaue it allows to plot subsets of a dataframe within a loop or similar without having to care about whether the resulting subset is gonna be empty or not.

Expected Output

When adding an empty subset of a dataframe with DatetimeIndex to an existing axis, nothing is added to the plot and no error is raised.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.7.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.2.2
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 21.0
setuptools : 49.6.0.post20210108
Cython : None
pytest : None
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.20
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

@matthme matthme added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 9, 2021
@jbrockmendel jbrockmendel added Visualization plotting and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants