-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
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:
|
@@ -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): |
There was a problem hiding this comment.
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)
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 |
@@ -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`) |
There was a problem hiding this comment.
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()
I think also add the method |
c8a24a2
to
6ba2c57
Compare
values = self.asi8 | ||
hasnans = self.hasnans | ||
result = 1e-9 * values | ||
if hasnans: |
There was a problem hiding this comment.
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)
6ba2c57
to
a165269
Compare
ENH: Add Series.dt.total_seconds GH #10817
@sjdenny awesome job! |
@sjdenny Thanks a lot! |
Implements a Series.dt.total_seconds method for timedelta64 Series.
closes #10817