Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
ENH: Add Series.dt.total_seconds GH #10817 #10939
Conversation
|
does timedelta.total_seconds() provide fractional second as wel? |
|
Yes, fractional seconds are included:
|
|
not what I mean is the actual Python timedelta.total_seconds have fractions (I think yes) just confirming |
|
Ah, I understand. Yes, it does:
|
jreback
added Enhancement API Design Timedelta
labels
Aug 31, 2015
jreback
added this to the
0.17.0
milestone
Aug 31, 2015
jreback
commented on an outdated diff
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): |
jreback
Contributor
|
jreback
commented on an outdated diff
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): | ||
| + # test index | ||
| + rng = timedelta_range('1 days, 10:11:12.100123456', periods=2, freq='s') | ||
| + expt = [1*86400+10*3600+11*60+12+100123456./1e9,1*86400+10*3600+11*60+13+100123456./1e9] | ||
| + assert_allclose(rng.total_seconds, expt, atol=1e-10, rtol=0) | ||
| + | ||
| + # test Series | ||
| + s = Series(rng) | ||
| + s_expt = Series(expt,index=[0,1]) | ||
| + tm.assert_series_equal(s.dt.total_seconds,s_expt) |
jreback
Contributor
|
|
Indeed, didn't see that, but +1 making it a method instead of a property for consistency with datetime.timedelta/pd.Timedelta |
|
Further, @sjdenny can you add a whatsnew notice (see doc/source/whatsnew/v0.17.0.txt, somewhere in 'other enhancements') |
|
In extending the (nanosecond-precision) tests, I've come across this behaviour:
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. |
|
It should be consistent between both in any case, but maybe we can also see the 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 |
|
the pandas functions should work with ns precision in all cases so need to override the scalar function as well |
|
@sjdenny This should probably be added somewhere here: https://github.com/pydata/pandas/blob/master/pandas/tslib.pyx#L2415 (I think the |
jreback
commented on an outdated diff
Aug 31, 2015
| @@ -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`) |
|
|
jreback
commented on an outdated diff
Aug 31, 2015
jreback
commented on an outdated diff
Aug 31, 2015
|
I think also add the method |
sjdenny
changed the title from
Add Series.dt.total_seconds GH #10817 to ENH: Add Series.dt.total_seconds GH #10817
Sep 1, 2015
jreback
commented on an outdated diff
Sep 1, 2015
| @@ -391,6 +391,17 @@ def f(x): | ||
| result = result.astype('int64') | ||
| return result | ||
| + def total_seconds(self): | ||
| + """ Total duration of each element expressed in seconds. """ | ||
| + values = self.asi8 | ||
| + hasnans = self.hasnans | ||
| + result = 1e-9 * values | ||
| + if hasnans: |
jreback
Contributor
|
jreback
added a commit
that referenced
this pull request
Sep 2, 2015
|
|
jreback |
582eb17
|
jreback
merged commit 582eb17
into pandas-dev:master
Sep 2, 2015
1 check passed
|
@sjdenny awesome job! |
|
@sjdenny Thanks a lot! |
sjdenny commentedAug 30, 2015
Implements a Series.dt.total_seconds method for timedelta64 Series.
closes #10817