-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Datetime error message (unable to continue that PR) #30711
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
Changes from 2 commits
31ead07
ad0bb55
a413f46
c95a6a6
5ebe498
3cca64b
e30c146
8fe73c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1970,6 +1970,15 @@ def objects_to_datetime64ns( | |
# return them as i8 to distinguish from wall times | ||
return values.view("i8"), tz_parsed | ||
except (ValueError, TypeError): | ||
# GH#10720. If we failed to parse datetime then notify | ||
# that flag errors='coerce' could be used to NaT. | ||
# Trying to distinguish exception based on message. | ||
if "Unknown string format" in e.args[0]: | ||
msg = ( | ||
" ".join(e.args) | ||
+ ". You can coerce to NaT by passing errors='coerce'" | ||
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. Let's opt for f-strings over adding them like this |
||
) | ||
e.args = (msg,) | ||
raise e | ||
|
||
if tz_parsed is not None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,9 +113,11 @@ def _coerce_scalar_to_timedelta_type(r, unit="ns", errors="raise"): | |
|
||
try: | ||
result = Timedelta(r, unit) | ||
except ValueError: | ||
except ValueError as e: | ||
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. can you call this "err" instead of "e". i really dislike 1-letter variable names 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. even here. ok!) |
||
if errors == "raise": | ||
raise | ||
msg = e.args[0] + ". You can coerce to NaT by passing errors='coerce'" | ||
e.args = (msg,) | ||
raise e | ||
elif errors == "ignore": | ||
return r | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -480,6 +480,17 @@ def test_to_datetime_unparseable_ignore(self): | |||||
s = "Month 1, 1999" | ||||||
assert pd.to_datetime(s, errors="ignore") == s | ||||||
|
||||||
def test_to_datetime_unparseable_raise(self): | ||||||
# GH#10720 | ||||||
invalid_data = "Month 1, 1999" | ||||||
expected_args = ( | ||||||
f"Unknown string format: {invalid_data}. " | ||||||
f"You can coerce to NaT by passing errors='coerce'" | ||||||
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.
Suggested change
Don't need the f prefix if not parametrizing anything |
||||||
) | ||||||
|
||||||
with pytest.raises(ValueError, match=expected_args): | ||||||
pd.to_datetime(invalid_data, errors="raise") | ||||||
|
||||||
@td.skip_if_windows # `tm.set_timezone` does not work in windows | ||||||
def test_to_datetime_now(self): | ||||||
# See GH#18666 | ||||||
|
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.
Can you use
raise from
instead? I think makes things clearer with Python3