You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Plotting with fill_between and timedelta on yaxis throws TypeError #8352
Hi everyone,
I don't know whether it is a bug or a missing feature in numpy or matplotlib, but plotting with fill_between and a simple datetime.timedelta (x axis) throws the following exception:
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Traceback (most recent call last):
File "/de/test.py", line 11, in <module>
pyplot.fill_between(df.TIME, 0, df.VALUE)
File "/usr/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2896, in fill_between
**kwargs)
File "/usr/lib/python3.6/site-packages/matplotlib/__init__.py", line 1819, in inner
return func(ax, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4581, in fill_between
x = ma.masked_invalid(self.convert_xunits(x))
File "/usr/lib/python3.6/site-packages/numpy/ma/core.py", line 2340, in masked_invalid
condition = ~(np.isfinite(a))
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
I wouldn't expect this to work, though that doesn't mean that it shouldn't. But it's just not obvious to me how timedeltas should be handled. For example, your x-values were instead:
Sorry, but I don't see any difference between the behavior with datetimes and timedeltas. Both are complex datatypes which can be transformed in various "simple" operations, e.g.: df.TIME = df.TIME - df.TIME.min()
If a datatype is sortable and all the items are unique (here: index), there should be a intuitive way to plot it? Timedelta similar to datetime?
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Removing the comment above, replacing x with a list of timedeltas fails plot as well as fill_between with:
File "/Users/jklymak/matplotlib/lib/matplotlib/cbook.py", line 1280, in _to_unmasked_float_array
return np.asarray(x, float)
^^^^^^^^^^^^^^^^^^^^
TypeError: float() argument must be a string or a real number, not 'datetime.timedelta'
That happens because you can convert a timedelta object directly to float: float(timedelta).
In my opinion this is all fine. If you can't do float(x[0]) we can't handle it. Numpy timedeltas work because as floats they return their "seconds since" values.
As for the pandas integration, again, if you can't do float(x[0]) we can't help you. It's up to the user to convert their data to something Matplotlib can handle.
My vote is to close as won't-fix and/or add more documentation on this. The converse argument is that we should add tiemdeltas to our date handling, but I actually am now convinced that is a mistake - timedeltas are not dates, they are the difference between dates. If you want dates do [date0 + td for td in x] where date0 is a your origin.
Activity
phobson commentedon Mar 21, 2017
I wouldn't expect this to work, though that doesn't mean that it shouldn't. But it's just not obvious to me how timedeltas should be handled. For example, your x-values were instead:
should the values be plotted at something analogous to [0, 10, 5], or [0, 10, 15]?
My intuition is that you should be using a pandas PeriodIndex, rather than stdlib timedeltas.
SebastianRzk commentedon Mar 24, 2017
Sorry, but I don't see any difference between the behavior with datetimes and timedeltas. Both are complex datatypes which can be transformed in various "simple" operations, e.g.:
df.TIME = df.TIME - df.TIME.min()If a datatype is sortable and all the items are unique (here: index), there should be a intuitive way to plot it? Timedelta similar to datetime?
phobson commentedon Mar 24, 2017
@RosesTheN00b from that response, it's still unclear to me if my examples should be plotted as
[0, 10, 5], or[0, 10, 15]. Which is it?SebastianRzk commentedon Mar 24, 2017
Okay, I realized the problem. Are all timedeltas absolute to one "imaginary" origin, or describe they the interval to the last element.
anntzer commentedon Apr 15, 2017
I think the only realistic interpretation is elapsed time since a common start point (e.g. start of a physical measurement).
dstansby commentedon Jul 11, 2017
It should definitely be [0, 10, 5] @phobson. Timedeltas have a 1 to 1 mapping to total seconds (eg.
timedelta(minutes=1).total_seconds()returns 60).I think fixing this depends on #8730 (as does plotting timedeltas in general)
github-actions commentedon Apr 9, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
astrojuanlu commentedon Apr 9, 2023
IIUC #9120 was not merged and probably this issue still stands
jklymak commentedon Apr 9, 2023
The following works fine:
Removing the comment above, replacing
xwith a list of timedeltas failsplotas well asfill_betweenwith:That happens because you can convert a
timedeltaobject directly to float:float(timedelta).In my opinion this is all fine. If you can't do
float(x[0])we can't handle it. Numpy timedeltas work because as floats they return their "seconds since" values.As for the pandas integration, again, if you can't do
float(x[0])we can't help you. It's up to the user to convert their data to something Matplotlib can handle.My vote is to close as won't-fix and/or add more documentation on this. The converse argument is that we should add tiemdeltas to our date handling, but I actually am now convinced that is a mistake - timedeltas are not dates, they are the difference between dates. If you want dates do
[date0 + td for td in x]where date0 is a your origin.phobson commentedon Jun 21, 2023
agree with @jklymak here. I'd close this myself, but that feels presumptuous given how sparse my involvement as been for the past ::mumbles:: years