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

Add .dt.total_seconds() method for timedelta64 Series #10817

Closed
brandon-rhodes opened this Issue Aug 13, 2015 · 6 comments

Comments

Projects
None yet
3 participants
@brandon-rhodes
Contributor

brandon-rhodes commented Aug 13, 2015

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!

@jreback

This comment has been minimized.

Show comment
Hide comment
@jreback

This comment has been minimized.

Show comment
Hide comment
@jreback

jreback Aug 14, 2015

Contributor

canoncially this is just an astype or division operation

In [3]: s = Series(pd.timedelta_range('1day',periods=5,freq='1s'))

In [4]: s
Out[4]: 
0   1 days 00:00:00
1   1 days 00:00:01
2   1 days 00:00:02
3   1 days 00:00:03
4   1 days 00:00:04
dtype: timedelta64[ns]

In [5]: s.astype('timedelta64[s]')
Out[5]: 
0    86400
1    86401
2    86402
3    86403
4    86404
dtype: float64

In [12]: s/np.timedelta64(1,'s')
Out[12]: 
0    86400
1    86401
2    86402
3    86403
4    86404
dtype: float64

In [6]: s.iloc[0].total_seconds()
Out[6]: 86400.0

I suppose could add a Series.dt.total_seconds to make TimedeltaIndex consistent with the method on Timedelta

Contributor

jreback commented Aug 14, 2015

canoncially this is just an astype or division operation

In [3]: s = Series(pd.timedelta_range('1day',periods=5,freq='1s'))

In [4]: s
Out[4]: 
0   1 days 00:00:00
1   1 days 00:00:01
2   1 days 00:00:02
3   1 days 00:00:03
4   1 days 00:00:04
dtype: timedelta64[ns]

In [5]: s.astype('timedelta64[s]')
Out[5]: 
0    86400
1    86401
2    86402
3    86403
4    86404
dtype: float64

In [12]: s/np.timedelta64(1,'s')
Out[12]: 
0    86400
1    86401
2    86402
3    86403
4    86404
dtype: float64

In [6]: s.iloc[0].total_seconds()
Out[6]: 86400.0

I suppose could add a Series.dt.total_seconds to make TimedeltaIndex consistent with the method on Timedelta

@jreback jreback added this to the Next Major Release milestone Aug 14, 2015

@jorisvandenbossche

This comment has been minimized.

Show comment
Hide comment
@jorisvandenbossche

jorisvandenbossche Aug 14, 2015

Member

+1 on a Series.dt.total_seconds()

Member

jorisvandenbossche commented Aug 14, 2015

+1 on a Series.dt.total_seconds()

@jreback jreback modified the milestones: 0.17.0, Next Major Release Aug 31, 2015

sjdenny added a commit to sjdenny/pandas that referenced this issue Sep 1, 2015

jreback added a commit that referenced this issue Sep 2, 2015

@brandon-rhodes

This comment has been minimized.

Show comment
Hide comment
@brandon-rhodes

brandon-rhodes Sep 2, 2015

Contributor

Wonderful! Thank you — Pandas is an unusually responsive project to feature suggestions, and gets better with each version.

Contributor

brandon-rhodes commented Sep 2, 2015

Wonderful! Thank you — Pandas is an unusually responsive project to feature suggestions, and gets better with each version.

@brandon-rhodes

This comment has been minimized.

Show comment
Hide comment
@brandon-rhodes

brandon-rhodes Sep 2, 2015

Contributor

And thank you, @jreback, for the idea of a s.astype('timedelta64[s]') — I had never seen an astype before with something in brackets!

Contributor

brandon-rhodes commented Sep 2, 2015

And thank you, @jreback, for the idea of a s.astype('timedelta64[s]') — I had never seen an astype before with something in brackets!

@jreback

This comment has been minimized.

Show comment
Hide comment
@jreback

jreback Sep 2, 2015

Contributor

@brandon-rhodes np. issues building up like crazy, so trying to work them down :)

Contributor

jreback commented Sep 2, 2015

@brandon-rhodes np. issues building up like crazy, so trying to work them down :)

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