Skip to content
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

Plotting when Interval coordinate is timedelta-based #4378

Open
gerritholl opened this issue Aug 26, 2020 · 2 comments
Open

Plotting when Interval coordinate is timedelta-based #4378

gerritholl opened this issue Aug 26, 2020 · 2 comments

Comments

@gerritholl
Copy link
Contributor

Is your feature request related to a problem? Please describe.

The xarray plotting interface supports coordinates containing pandas.Interval iff those intervals contain numbers. It fails when those intervals contain pandas.Timedelta:

import numpy as np
import pandas as pd
import xarray as xr

da = xr.DataArray(
        np.arange(10),
        dims=("x",),
        coords={"x": [pd.Interval(i, i+1) for i in range(10)]})
da.plot()  # works

da = xr.DataArray(
        np.arange(10),
        dims=("x",),
        coords={"x": [pd.Interval(
            d-pd.Timestamp("2000-01-01"),
            d-pd.Timestamp("2000-01-01")+pd.Timedelta("1H"))
            for d in pd.date_range("2000-01-01", "2000-01-02", 10)]})
da.plot()  # fails

The latter fails with:

Traceback (most recent call last):
  File "mwe82.py", line 18, in <module>
    da.plot()  # fails
  File "/data/gholl/miniconda3/envs/py38/lib/python3.8/site-packages/xarray/plot/plot.py", line 446, in __call__
    return plot(self._da, **kwargs)
  File "/data/gholl/miniconda3/envs/py38/lib/python3.8/site-packages/xarray/plot/plot.py", line 200, in plot
    return plotfunc(darray, **kwargs)
  File "/data/gholl/miniconda3/envs/py38/lib/python3.8/site-packages/xarray/plot/plot.py", line 302, in line
    _ensure_plottable(xplt_val, yplt_val)
  File "/data/gholl/miniconda3/envs/py38/lib/python3.8/site-packages/xarray/plot/utils.py", line 551, in _ensure_plottable
    raise TypeError(
TypeError: Plotting requires coordinates to be numeric or dates of type np.datetime64, datetime.datetime, cftime.datetime or pd.Interval.

This error message is somewhat confusing, because the coordinates are "dates of type (...) pd.Interval", but perhaps a timedelta is not considered a date.

Describe the solution you'd like

I would like that I can use the xarray plotting interface for any pandas.Interval coordinate, including pandas.Timestamp and pandas.Timedelta.

Describe alternatives you've considered

I'll "manually" calculate the midpoints and use those as a timedelta coordinate instead.

Additional context

It seems that regular timedeltas aren't really supported either, although they don't cause an error message, they rather produce incorrect results. There's probably a related issue somewhere, but I can't find it now.

@mathause
Copy link
Collaborator

Quickly looking at the code this error message seems to not be entirely correct. That should probably be along the lines of

TypeError: Plotting requires coordinates to be numeric or dates of type
np.datetime64, datetime.datetime, cftime.datetime
- or pd.Interval.
+ or pd.Interval containing one of these dtypes.

The pd.Interval are used to calculate their midpoints. Then the dtype of the midpoints is checked:

def _ensure_plottable(*args):

But indeed matplotlib does not seem to be able to directly plot pd.Timedeltas

x = [d-pd.Timestamp("2000-01-01") for d in pd.date_range("2000-01-01", "2000-01-02", 10)]
plt.plot(x, np.arange(10))
TypeError: ...

So I am not sure we should try to be clever here.

@stale
Copy link

stale bot commented Apr 18, 2022

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants