Add .dt.total_seconds() method for timedelta64 Series · Issue #10817 · pandas-dev/pandas (original) (raw)

Background: I am trying to build a horizonal bar graph that shows me when various builds on a server started and ended, so that I can see visually which builds overlapped in time. But the idea that a horizontal bar graph's bar-width would be a timedelta seems completely foreign to all of the plotting libraries that I have tried today (matplotlib, bokeh, ggplot), so I want to punt and instead reduce my timedelta64's to a floating-point number of days. I can then fudge the axes to display dates anyway.

Problem: there is no easy way to move from a timedelta64 to the standard Python idea of an ordinal date. I would have to extract all of the components manually (days, seconds, fractions of a second) and then multiply them each by an appropriate factor and then add them back together.

The Python timedelta class has an alternative that I like very much: a total_seconds() method that expresses then entire value of the timedelta as a single scalar number of seconds.

This is not a perfect solution, since the factors it uses internally neglect the fact that a day might have included a leap second, but it works pretty well for non-astronomical use.

Could a Series.dt.total_seconds() method be added that returns the floating point value days * 86400 + seconds + microseconds / 1e6? It would be a great convenience for moving from date-land to scalar-land before plotting. Thanks!