-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ERR non-ISO formats don't show position of error #50366
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
MarcoGorelli
merged 10 commits into
pandas-dev:main
from
MarcoGorelli:note-position-in-err-message
Dec 28, 2022
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
303a648
wip
8e40df4
unify messages
9040dca
fixup regexes
c49640d
simplify
1e7613f
fix test
93c3026
Merge remote-tracking branch 'upstream/main' into note-position-in-er…
3338b43
remove (search) and (match) from messages
ba48f33
Merge branch 'main' into note-position-in-err-message
MarcoGorelli 34a7138
simplify
8543fc9
Merge branch 'main' into note-position-in-err-message
MarcoGorelli 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -479,7 +479,12 @@ def test_to_datetime_parse_timezone_malformed(self, offset): | |
fmt = "%Y-%m-%d %H:%M:%S %z" | ||
date = "2010-01-01 12:00:00 " + offset | ||
|
||
msg = "does not match format|unconverted data remains" | ||
msg = "|".join( | ||
[ | ||
r'^time data ".*" at position 0 doesn\'t match format ".*"$', | ||
r'^unconverted data remains at position 0: ".*"$', | ||
] | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime([date], format=fmt) | ||
|
||
|
@@ -1093,7 +1098,7 @@ def test_datetime_bool_arrays_mixed(self, cache): | |
to_datetime([False, datetime.today()], cache=cache) | ||
with pytest.raises( | ||
ValueError, | ||
match=r"^time data 'True' does not match format '%Y%m%d' \(match\)$", | ||
match=r'^time data "True" at position 1 doesn\'t match format "%Y%m%d"$', | ||
): | ||
to_datetime(["20130101", True], cache=cache) | ||
tm.assert_index_equal( | ||
|
@@ -1132,11 +1137,13 @@ def test_datetime_invalid_scalar(self, value, format, warning): | |
res = to_datetime(value, errors="coerce", format=format) | ||
assert res is NaT | ||
|
||
msg = ( | ||
"does not match format|" | ||
"unconverted data remains:|" | ||
"second must be in 0..59|" | ||
f"Given date string {value} not likely a datetime" | ||
msg = "|".join( | ||
[ | ||
r'^time data "a" at position 0 doesn\'t match format "%H:%M:%S"$', | ||
r'^Given date string "a" not likely a datetime present at position 0$', | ||
r'^unconverted data remains at position 0: "9"$', | ||
r"^second must be in 0..59: 00:01:99 present at position 0$", | ||
] | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
with tm.assert_produces_warning(warning, match="Could not infer format"): | ||
|
@@ -1157,7 +1164,7 @@ def test_datetime_outofbounds_scalar(self, value, format, warning): | |
assert res is NaT | ||
|
||
if format is not None: | ||
msg = "does not match format|Out of bounds .* present at position 0" | ||
msg = r'^time data ".*" at position 0 doesn\'t match format ".*"$' | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(value, errors="raise", format=format) | ||
else: | ||
|
@@ -1181,11 +1188,13 @@ def test_datetime_invalid_index(self, values, format, warning): | |
res = to_datetime(values, errors="coerce", format=format) | ||
tm.assert_index_equal(res, DatetimeIndex([NaT] * len(values))) | ||
|
||
msg = ( | ||
"does not match format|" | ||
"unconverted data remains:|" | ||
f"Given date string {values[0]} not likely a datetime|" | ||
"second must be in 0..59" | ||
msg = "|".join( | ||
[ | ||
r'^Given date string "a" not likely a datetime present at position 0$', | ||
r'^time data "a" at position 0 doesn\'t match format "%H:%M:%S"$', | ||
r'^unconverted data remains at position 0: "9"$', | ||
r"^second must be in 0..59: 00:01:99 present at position 0$", | ||
] | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
with tm.assert_produces_warning(warning, match="Could not infer format"): | ||
|
@@ -1805,8 +1814,8 @@ def test_dataframe_coerce(self, cache): | |
df2 = DataFrame({"year": [2015, 2016], "month": [2, 20], "day": [4, 5]}) | ||
|
||
msg = ( | ||
"cannot assemble the datetimes: time data .+ does not " | ||
r"match format '%Y%m%d' \(match\)" | ||
r'^cannot assemble the datetimes: time data ".+" at position 1 doesn\'t ' | ||
r'match format "%Y%m%d"$' | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(df2, cache=cache) | ||
|
@@ -1882,7 +1891,10 @@ def test_dataframe_mixed(self, cache): | |
def test_dataframe_float(self, cache): | ||
# float | ||
df = DataFrame({"year": [2000, 2001], "month": [1.5, 1], "day": [1, 1]}) | ||
msg = "cannot assemble the datetimes: unconverted data remains: 1" | ||
msg = ( | ||
r"^cannot assemble the datetimes: unconverted data remains at position " | ||
r'0: "1"$' | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(df, cache=cache) | ||
|
||
|
@@ -2072,7 +2084,7 @@ def test_to_datetime_on_datetime64_series(self, cache): | |
def test_to_datetime_with_space_in_series(self, cache): | ||
# GH 6428 | ||
ser = Series(["10/18/2006", "10/18/2008", " "]) | ||
msg = r"^time data ' ' does not match format '%m/%d/%Y' \(match\)$" | ||
msg = r'^time data " " at position 2 doesn\'t match format "%m/%d/%Y"$' | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(ser, errors="raise", cache=cache) | ||
result_coerce = to_datetime(ser, errors="coerce", cache=cache) | ||
|
@@ -2342,7 +2354,10 @@ def test_dayfirst_warnings_invalid_input(self): | |
|
||
with pytest.raises( | ||
ValueError, | ||
match=r"time data '03/30/2011' does not match format '%d/%m/%Y' \(match\)$", | ||
match=( | ||
r'^time data "03/30/2011" at position 1 doesn\'t match format ' | ||
r'"%d/%m/%Y"$' | ||
), | ||
): | ||
to_datetime(arr, dayfirst=True) | ||
|
||
|
@@ -2410,7 +2425,11 @@ def test_to_datetime_infer_datetime_format_consistent_format( | |
def test_to_datetime_inconsistent_format(self, cache): | ||
data = ["01/01/2011 00:00:00", "01-02-2011 00:00:00", "2011-01-03T00:00:00"] | ||
ser = Series(np.array(data)) | ||
with pytest.raises(ValueError, match="does not match format"): | ||
msg = ( | ||
r'^time data "01-02-2011 00:00:00" at position 1 doesn\'t match format ' | ||
r'"%m/%d/%Y %H:%M:%S"$' | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(ser, cache=cache) | ||
|
||
def test_to_datetime_consistent_format(self, cache): | ||
|
@@ -2923,17 +2942,22 @@ def test_incorrect_value_exception(self): | |
to_datetime(["today", "yesterday"]) | ||
|
||
@pytest.mark.parametrize( | ||
"format, warning", [(None, UserWarning), ("%Y-%m-%d %H:%M:%S", None)] | ||
"format, warning", | ||
[ | ||
(None, UserWarning), | ||
("%Y-%m-%d %H:%M:%S", None), | ||
("%Y-%d-%m %H:%M:%S", None), | ||
], | ||
) | ||
def test_to_datetime_out_of_bounds_with_format_arg(self, format, warning): | ||
# see gh-23830 | ||
msg = ( | ||
"Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 " | ||
"present at position 0" | ||
r"^Out of bounds nanosecond timestamp: 2417-10-10 00:00:00 " | ||
r"present at position 0$" | ||
) | ||
with pytest.raises(OutOfBoundsDatetime, match=msg): | ||
with tm.assert_produces_warning(warning, match="Could not infer format"): | ||
to_datetime("2417-10-27 00:00:00", format=format) | ||
to_datetime("2417-10-10 00:00:00", format=format) | ||
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. slightly changing the input here so we can parametrise of it with both |
||
|
||
@pytest.mark.parametrize( | ||
"arg, origin, expected_str", | ||
|
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.
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.
NBD but is the "r" necessary?
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.
Shouldn't be, it's just a habit to always use that in regular expressions