Skip to content

ENH: Add Series.dt.total_seconds GH #10817 #10939

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

Merged
merged 1 commit into from
Sep 2, 2015

Conversation

sjdenny
Copy link
Contributor

@sjdenny sjdenny commented Aug 30, 2015

Implements a Series.dt.total_seconds method for timedelta64 Series.

closes #10817

@jreback
Copy link
Contributor

jreback commented Aug 30, 2015

does timedelta.total_seconds() provide fractional second as wel?

@sjdenny
Copy link
Contributor Author

sjdenny commented Aug 30, 2015

Yes, fractional seconds are included:

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

In [2]: s
Out[2]: 
0          1 days 00:00:00
1   1 days 00:00:00.001000
2   1 days 00:00:00.002000
3   1 days 00:00:00.003000
4   1 days 00:00:00.004000
dtype: timedelta64[ns]

In [3]: s.dt.total_seconds
Out[3]: 
0    86400.000
1    86400.001
2    86400.002
3    86400.003
4    86400.004
dtype: float64

@jreback
Copy link
Contributor

jreback commented Aug 30, 2015

not what I mean

is the actual Python timedelta.total_seconds have fractions (I think yes) just confirming

@sjdenny
Copy link
Contributor Author

sjdenny commented Aug 30, 2015

Ah, I understand. Yes, it does:

In [38]: t = timedelta(days=1,microseconds=40)

In [39]: t.total_seconds()
Out[39]: 86400.00004

@jreback jreback added this to the 0.17.0 milestone Aug 31, 2015
@@ -944,6 +945,27 @@ def test_fields(self):

tm.assert_series_equal(s.dt.days,Series([1,np.nan],index=[0,1]))
tm.assert_series_equal(s.dt.seconds,Series([10*3600+11*60+12,np.nan],index=[0,1]))

def test_total_seconds(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test in test_series/test_dt_namespace_accessor as well (its like the one you have for test Series)

@jorisvandenbossche
Copy link
Member

Indeed, didn't see that, but +1 making it a method instead of a property for consistency with datetime.timedelta/pd.Timedelta

@jorisvandenbossche
Copy link
Member

Further, @sjdenny can you add a whatsnew notice (see doc/source/whatsnew/v0.17.0.txt, somewhere in 'other enhancements')

@sjdenny
Copy link
Contributor Author

sjdenny commented Aug 31, 2015

In extending the (nanosecond-precision) tests, I've come across this behaviour:

In [18]: td = pd.Timedelta(nanoseconds=50)

In [19]: td.nanoseconds
Out[19]: 50

In [20]: td.total_seconds()
Out[20]: 0.0

In [21]: td.total_seconds() == 0.0
Out[21]: True

In [22]: td2 = pd.Timedelta(microseconds=50)

In [23]: td2.total_seconds()
Out[23]: 5e-05

It appears (e.g. here) that timedelta.total_seconds() aims for microsecond accuracy only. Is this the behaviour we want to reproduce in Series.dt.total_seconds()? Nanosecond precision appears preferable, but then the scalar and Series functions would have slightly different behaviour.

@jorisvandenbossche
Copy link
Member

It should be consistent between both in any case, but maybe we can also see the Timedelta behaviour as a bug?

I suspect that this method is just inherited from datetime.timedelta, so maybe we will have to overwrite it ourselves in the subclass.

BTW, the reason of this difference is that datetime.timedelta does not support nanoseconds, while pd.Timedelta does

@jreback
Copy link
Contributor

jreback commented Aug 31, 2015

the pandas functions should work with ns precision in all cases

so need to override the scalar function as well

@jorisvandenbossche
Copy link
Member

@sjdenny This should probably be added somewhere here: https://github.com/pydata/pandas/blob/master/pandas/tslib.pyx#L2415 (I think the self.value will give you the nanoseconds, and then you can use the same approach)

@@ -176,6 +176,10 @@ Other enhancements

- ``pandas.tseries.offsets`` larger than the ``Day`` offset can now be used with with ``Series`` for addition/subtraction (:issue:`10699`). See the :ref:`Documentation <timeseries.offsetseries>` for more details.

- ``pd.Series`` of type timedelta64 has new method .dt.total_seconds() returning the duration of the timedelta in seconds (:issue: `10817`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use double-backticks around timedelta64 and .dt.total_seconds()

@jreback
Copy link
Contributor

jreback commented Aug 31, 2015

I think also add the method .total_seconds() on the NaTType object (returns np.nan) for compat. (and pls add a test as well).

@sjdenny sjdenny force-pushed the series_total_seconds branch from c8a24a2 to 6ba2c57 Compare September 1, 2015 12:55
@sjdenny sjdenny changed the title Add Series.dt.total_seconds GH #10817 ENH: Add Series.dt.total_seconds GH #10817 Sep 1, 2015
values = self.asi8
hasnans = self.hasnans
result = 1e-9 * values
if hasnans:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use result = self._maybe_mask_results(result) (you can remove the hasnans as well)

@sjdenny sjdenny force-pushed the series_total_seconds branch from 6ba2c57 to a165269 Compare September 1, 2015 22:53
jreback added a commit that referenced this pull request Sep 2, 2015
@jreback jreback merged commit 582eb17 into pandas-dev:master Sep 2, 2015
@jreback
Copy link
Contributor

jreback commented Sep 2, 2015

@sjdenny awesome job!

@jorisvandenbossche
Copy link
Member

@sjdenny Thanks a lot!

@sjdenny sjdenny deleted the series_total_seconds branch September 2, 2015 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add .dt.total_seconds() method for timedelta64 Series
3 participants