-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: add to_offset method to Timedelta #9064
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
Comments
Does this look like an acceptable ENH (as suggested in SO) or is it expected to introduce a to_offset() method in timedelta which returns offsets.Second? diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 54b29b1..8694436 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime,timedelta from pandas.compat import range, long, zip from pandas import compat import re @@ -298,6 +298,10 @@ def to_offset(freqstr): name, stride = stride, name name, _ = _base_and_stride(name) delta = get_offset(name) * stride + + elif isinstance(freqstr, timedelta): + delta = offsets.Second(freqstr.total_seconds()) + else: delta = None stride_sign = None |
create a test that fails w/o the fix and passes with |
Done. Created pull request #9226. Thanks. |
CI build failed.ERROR: pandas.tseries.tests.test_frequencies.test_to_offset_timedeltaTraceback (most recent call last): AttributeError: 'datetime.timedelta' object has no attribute 'total_seconds'I will need to revisit and add existence check for method etc. |
@tvyomkesh the reason is that |
@jorisvandenbossche for now I have fixed this by introducing a check if the method is available or not (https://github.com/pydata/pandas/pull/9226/files). For older versions which do not have the method, it will use the equivalent calculation mentioned in the Python 2.7 docs (https://docs.python.org/2/library/datetime.html#datetime.timedelta.total_seconds). |
ENH: add to_offset method to Timedelta #9064
from SO
This works, but I think
td.to_offset()
makes senseThe text was updated successfully, but these errors were encountered: