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
Implement a TimedeltaLocator (possibly along with timedelta versions of SecondLocator, MinuteLocator etc.)
Implement an AutoTimedeltaLocator
Implement a TimedeltaConverter to go in the units registry
Reacted by Benjamin T. Vincent, Ben Dichter, codl, David Bernat, Christoph Buchner, Skip R., uptoga, Cemlyn Waters, gitsdfawfevi, Ali Hamdan and 3 more
(disclaimer: I don't really know what I'm doing with all this and am just copying datetime support and learning as I go along)
I think datetime is registered in the unit framework. The last line of dates.py is units.registry[datetime.datetime] = DateConverter() which means a datetime passed to plot automatically uses all the converter/ticker stuff in dates.py. Is that what you meant?
I don't know anything about datetime plotting either, but the general idea was indeed whether we could end up with something like units.registry[timedelta] = TimeDeltaConverter.
Is there something somewhere that details how unit support is handled? Folding timedelta directly into datetime seems weird - it seems like it would enable you to do things like plt.plot([timedelta(days=200), datetime(1970, 11, 14)], [0, 1])? Seems like you'd want to be kinda explicit about when you can use timedelta and when you can use datetime.
That said, we're essentially never explicit about when you can use "meters" vs. "delta-meters", so maybe my concerns are a bit overblown? I guess it feels weirder because numbers are mostly unitless and these have explicit, non-interchangeable units.
I think there are two cases that I've probably accidentally folded into one here:
Plotting timedelta objects
Plotting datetime objects that need some sort of offsetting with timedelta (ie. plotting a square
In case number 2., offsetting should be done before any conversion to internal representations, such that only datetime coordinates are ever plotted, and none of the converting timedelta machinery is needed.
Case 1. is what should be tackled by this issue, and as is said above, we need to be very careful to keep the two cases distinct, and error when a code snippet like above (#8869 (comment)) is run.
@dstansby Yes, I think with these concerns cleanly separated, implementing timedelta handling becomes much easier, too. Other than the question of formatting (and if you have any references on timedelta formatting I'd like to implement that in dateutil, so I'm interested in what's the "best way"), so long as the units are being enforced, it's just timedelta.total_seconds(), none of the complicated nonsense about time zones and the like, since timedelta() by its nature represents a duration.
Great, will put a comment on that issue about timedelta formatting. Would be much better to have it somewhere in dateutil than for us to roll our own version.
The ideas in #19236 were that we would use datetime-like Formatters and Locators to the timedeltas, but I'm no longer convinced that this is a good idea. If someone really wants time, they can add an origin datetime to each timedelta, either when their experiment started, or a nominal date and pass to our datetime converter.
I evaluate simulations and write the values into a pandas DataFrame with timedelta as index. Using datetime is the wrong choice in this case, as there is no starting day on which the simulation starts (it is relatively at 0 seconds). see my whole comment: pandas-dev/pandas#60079 (comment)
@gitsdfawfevi Yes, we understand that use case. The question is if there is truly value in HH.MM.SS since simulation start, or if folks are OK with decimal seconds, hours, or days etc. Most fields I'm familiar with, the latter is fine.
@jklymak I assume that you mean the following representation 03:15:00 == 3.25 h, therefore an example of when the decimal representation is very unfavorable in a plot: For example, there is data from a year simulation, represented decimal in months (= 12 mo). Now you want to interactively enlarge the plot to a certain time range in seconds. Of course, this can also be achieved by converting units and changing the corresponding axis labeling, but it is very complex.
Regardless of whether this is implemented or not, I find it very critical how matplotlib deals with timedelta “The critical point is that matplotlib pretends to understand timedelta, but the result is ‘wrong’ -- at least not what you would expect.” (pandas-dev/pandas#60079 (comment))
lets not mix concerns here - Matplotlib deals with np.timedelta64, we don't directly handle pandas Timedelta plotting, and we don't provide pd.df.plot. If there is something we can do to make that work more smoothly, we will, because pandas is used so extensively, but please express the feature request in terms of numpy objects and our plotting routines (which underpin the pandas ones)
Sorry, that was my mistake. I wasn't aware that there is a difference between pandas and numpy timedelta. Especially as both use the same dtype designation.
In example #8869 , timedelta from python datetime is used, but it is also different from numpy datetime (https://numpy.org/doc/stable/reference/arrays.datetime.html). Would the implementations of datetime from numpy and python then be compatible with matplotlib?
lets not mix concerns here - Matplotlib deals with np.timedelta64, we don't directly handle pandas Timedelta plotting, and we don't provide pd.df.plot. If there is something we can do to make that work more smoothly, we will, because pandas is used so extensively, but please express the feature request in terms of numpy objects and our plotting routines (which underpin the pandas ones)
@jklymak the example with numpy objects is the following:
Here the context is displayed correctly in both cases. In pandas-dev/pandas#60079 the context isn't displayed properly, compare the linear context with pd.timedelta versus the curved with pd.datetime.
So this is a problem of pandas using matplotlib (or anythin related) rather than a matplotlib problem, and thus should be fixed in pandas, isn't it?
Plotting datetime objects that need some sort of offsetting with timedelta
I'm running into this use case right now. Allowing Axes.convert_xunits and Axes.convert_yunits to take in timedelta objects seems reasonable, for axes that already have a time scale.
Hm, that was driven by some subclassing of Text and manually setting positions. When working through the normal interfaces, datetimes as coordinates work fine. I'm having some trouble making a standalone script that shows the issue I was running into, it's probably fine as-is.
Activity
anntzer commentedon Jul 12, 2017
Quick question from the peanut gallery: could datetime/timedelta support be folded into unit support? (I have no idea.)
dstansby commentedon Jul 12, 2017
(disclaimer: I don't really know what I'm doing with all this and am just copying
datetimesupport and learning as I go along)I think
datetimeis registered in the unit framework. The last line ofdates.pyisunits.registry[datetime.datetime] = DateConverter()which means a datetime passed to plot automatically uses all the converter/ticker stuff indates.py. Is that what you meant?anntzer commentedon Jul 12, 2017
I don't know anything about datetime plotting either, but the general idea was indeed whether we could end up with something like units.registry[timedelta] = TimeDeltaConverter.
has2k1 commentedon Jul 30, 2017
I dealt with timedelta breaks and formats in mizani. The implementation is not the cleanest but I have been satisfied with the results.
pganssle commentedon Aug 19, 2017
Is there something somewhere that details how unit support is handled? Folding
timedeltadirectly intodatetimeseems weird - it seems like it would enable you to do things likeplt.plot([timedelta(days=200), datetime(1970, 11, 14)], [0, 1])? Seems like you'd want to be kinda explicit about when you can usetimedeltaand when you can usedatetime.That said, we're essentially never explicit about when you can use "meters" vs. "delta-meters", so maybe my concerns are a bit overblown? I guess it feels weirder because numbers are mostly unitless and these have explicit, non-interchangeable units.
dstansby commentedon Aug 22, 2017
Thanks for all your comments @pganssle!
I think there are two cases that I've probably accidentally folded into one here:
timedeltaobjectsdatetimeobjects that need some sort of offsetting withtimedelta(ie. plotting a squareIn case number 2., offsetting should be done before any conversion to internal representations, such that only
datetimecoordinates are ever plotted, and none of the convertingtimedeltamachinery is needed.Case 1. is what should be tackled by this issue, and as is said above, we need to be very careful to keep the two cases distinct, and error when a code snippet like above (#8869 (comment)) is run.
pganssle commentedon Aug 22, 2017
@dstansby Yes, I think with these concerns cleanly separated, implementing
timedeltahandling becomes much easier, too. Other than the question of formatting (and if you have any references on timedelta formatting I'd like to implement that in dateutil, so I'm interested in what's the "best way"), so long as the units are being enforced, it's justtimedelta.total_seconds(), none of the complicated nonsense about time zones and the like, sincetimedelta()by its nature represents a duration.dstansby commentedon Aug 22, 2017
Great, will put a comment on that issue about timedelta formatting. Would be much better to have it somewhere in
dateutilthan for us to roll our own version.dstansby commentedon Aug 29, 2017
Was easier to just open a new PR at #9120
dstansby commentedon Aug 29, 2017
Woops, that comment was meant for the PR!
6 remaining items
gitsdfawfevi commentedon Nov 14, 2024
I evaluate simulations and write the values into a pandas DataFrame with timedelta as index. Using datetime is the wrong choice in this case, as there is no starting day on which the simulation starts (it is relatively at 0 seconds). see my whole comment: pandas-dev/pandas#60079 (comment)
jklymak commentedon Nov 14, 2024
@gitsdfawfevi Yes, we understand that use case. The question is if there is truly value in HH.MM.SS since simulation start, or if folks are OK with decimal seconds, hours, or days etc. Most fields I'm familiar with, the latter is fine.
gitsdfawfevi commentedon Nov 14, 2024
@jklymak I assume that you mean the following representation 03:15:00 == 3.25 h, therefore an example of when the decimal representation is very unfavorable in a plot: For example, there is data from a year simulation, represented decimal in months (= 12 mo). Now you want to interactively enlarge the plot to a certain time range in seconds. Of course, this can also be achieved by converting units and changing the corresponding axis labeling, but it is very complex.
Regardless of whether this is implemented or not, I find it very critical how matplotlib deals with timedelta “The critical point is that matplotlib pretends to understand timedelta, but the result is ‘wrong’ -- at least not what you would expect.” (pandas-dev/pandas#60079 (comment))
jklymak commentedon Nov 14, 2024
lets not mix concerns here - Matplotlib deals with
np.timedelta64, we don't directly handle pandas Timedelta plotting, and we don't providepd.df.plot. If there is something we can do to make that work more smoothly, we will, because pandas is used so extensively, but please express the feature request in terms of numpy objects and our plotting routines (which underpin the pandas ones)gitsdfawfevi commentedon Nov 27, 2024
Sorry, that was my mistake. I wasn't aware that there is a difference between pandas and numpy timedelta. Especially as both use the same dtype designation.
In example #8869 , timedelta from python datetime is used, but it is also different from numpy datetime (https://numpy.org/doc/stable/reference/arrays.datetime.html). Would the implementations of datetime from numpy and python then be compatible with matplotlib?
maoding commentedon Dec 16, 2024
@jklymak the example with numpy objects is the following:
Resulting in


and
Here the context is displayed correctly in both cases. In pandas-dev/pandas#60079 the context isn't displayed properly, compare the linear context with
pd.timedeltaversus the curved withpd.datetime.So this is a problem of pandas using matplotlib (or anythin related) rather than a matplotlib problem, and thus should be fixed in pandas, isn't it?
scottshambaugh commentedon Mar 7, 2025
I'm running into this use case right now. Allowing
Axes.convert_xunitsandAxes.convert_yunitsto take intimedeltaobjects seems reasonable, for axes that already have a time scale.scottshambaugh commentedon Mar 7, 2025
As a workaround, the below shows how to convert a timedelta to matplotlib's numerical date coordinates.
jklymak commentedon Mar 8, 2025
@scottshambaugh Thanks for this. Can you clarify the output you want and how you originally set the x-axis units?
scottshambaugh commentedon Mar 8, 2025
Hm, that was driven by some subclassing of
Textand manually setting positions. When working through the normal interfaces, datetimes as coordinates work fine. I'm having some trouble making a standalone script that shows the issue I was running into, it's probably fine as-is.ax.hist()fails on sequence of timedeltas due to comparison withnp.inf#31182