-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Enable parsing of ISO8601-like timestamps with negative signs using pd.Timedelta (GH37172) #39497
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
avinashpancham
commented
Jan 31, 2021
- closes BUG: Handle negative sign (-) when parsing ISO 8601 durations #37172
- tests added / passed
- Ensure all linting tests pass, see here for how to run them
- whatsnew entry
pandas/_libs/tslibs/timedeltas.pyx
Outdated
@@ -275,7 +275,7 @@ cdef convert_to_timedelta64(object ts, str unit): | |||
ts = cast_from_unit(ts, unit) | |||
ts = np.timedelta64(ts, "ns") | |||
elif isinstance(ts, str): | |||
if len(ts) > 0 and ts[0] == "P": | |||
if len(ts) > 0 and (ts[0] == "P" or ts[:2] == "-P"): |
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.
you need another length check here (e.g. if len(ts) ==1 this would raise)
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.
updated
pandas/_libs/tslibs/timedeltas.pyx
Outdated
@@ -746,11 +754,11 @@ cdef inline int64_t parse_iso_format_string(str ts) except? -1: | |||
else: | |||
raise ValueError(err_msg) | |||
|
|||
if not have_value: | |||
if not valid_ts: |
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.
you cannot use have_value or maybe have_value && sign? adding this makes harder to understand
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.
have_value
alone was sufficient
pandas/_libs/tslibs/timedeltas.pyx
Outdated
@@ -1251,7 +1259,7 @@ class Timedelta(_Timedelta): | |||
elif isinstance(value, str): | |||
if unit is not None: | |||
raise ValueError("unit must not be specified if the value is a str") | |||
if len(value) > 0 and value[0] == 'P': | |||
if len(value) > 0 and (value[0] == 'P' or value[:2] == "-P"): |
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.
same comment as above
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.
updated
Hello @avinashpancham! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found: There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2021-02-05 22:04:18 UTC |
thanks @avinashpancham |