-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG/API: Disallow unit if input to Timedelta and to_timedelta is/contains str #34634
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,7 +218,7 @@ cdef convert_to_timedelta64(object ts, object unit): | |
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def array_to_timedelta64(object[:] values, unit='ns', errors='raise'): | ||
def array_to_timedelta64(object[:] values, unit=None, errors='raise'): | ||
""" | ||
Convert an ndarray to an array of timedeltas. If errors == 'coerce', | ||
coerce non-convertible objects to NaT. Otherwise, raise. | ||
|
@@ -234,29 +234,35 @@ def array_to_timedelta64(object[:] values, unit='ns', errors='raise'): | |
n = values.shape[0] | ||
result = np.empty(n, dtype='m8[ns]') | ||
iresult = result.view('i8') | ||
has_string = False | ||
|
||
# Usually, we have all strings. If so, we hit the fast path. | ||
# If this path fails, we try conversion a different way, and | ||
# this is where all of the error handling will take place. | ||
try: | ||
for i in range(n): | ||
if isinstance(values[i], str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be in the clause on L251 |
||
has_string = True | ||
if values[i] is NaT: | ||
# we allow this check in the fast-path because NaT is a C-object | ||
# so this is an inexpensive check | ||
iresult[i] = NPY_NAT | ||
else: | ||
result[i] = parse_timedelta_string(values[i]) | ||
except (TypeError, ValueError): | ||
unit = parse_timedelta_unit(unit) | ||
parsed_unit = parse_timedelta_unit(unit or 'ns') | ||
for i in range(n): | ||
try: | ||
result[i] = convert_to_timedelta64(values[i], unit) | ||
result[i] = convert_to_timedelta64(values[i], parsed_unit) | ||
except ValueError: | ||
if errors == 'coerce': | ||
result[i] = NPY_NAT | ||
else: | ||
raise | ||
|
||
if has_string and unit is not None: | ||
raise ValueError("unit must be un-specified if the input contains a str") | ||
pdnm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return iresult.base # .base to access underlying np.ndarray | ||
|
||
|
||
|
@@ -1155,6 +1161,8 @@ class Timedelta(_Timedelta): | |
elif isinstance(value, _Timedelta): | ||
value = value.value | ||
elif isinstance(value, str): | ||
if unit is not None: | ||
raise ValueError("unit must be un-specified if the value is a str") | ||
pdnm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(value) > 0 and value[0] == 'P': | ||
value = parse_iso_format_string(value) | ||
else: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.