Plotting when Interval coordinate is timedelta-based
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the plotting entry points in xarray/plot/plot.py, especially line and _ensure_plottable in xarray/plot/utils.py, then reproduce the pandas.Interval example from the issue. Done means plotting Interval coordinates containing pandas.Timestamp or pandas.Timedelta works, with timedelta coordinates producing correct results rather than failing or being misrepresented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, pandas, python
- Domain
- data-visualization
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100