-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ERR: "day out of range" doesn't show position of error #50464
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -481,8 +481,8 @@ def test_to_datetime_parse_timezone_malformed(self, offset): | |
|
||
msg = "|".join( | ||
[ | ||
r'^time data ".*" at position 0 doesn\'t match format ".*"$', | ||
r'^unconverted data remains at position 0: ".*"$', | ||
r'^time data ".*" doesn\'t match format ".*", at position 0$', | ||
r'^unconverted data remains: ".*", at position 0$', | ||
] | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
|
@@ -859,7 +859,7 @@ def test_to_datetime_dt64s_and_str(self, arg, format): | |
"dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")] | ||
) | ||
def test_to_datetime_dt64s_out_of_bounds(self, cache, dt): | ||
msg = "Out of bounds .* present at position 0" | ||
msg = "^Out of bounds nanosecond timestamp: .*, at position 0$" | ||
with pytest.raises(OutOfBoundsDatetime, match=msg): | ||
to_datetime(dt, errors="raise") | ||
|
||
|
@@ -1098,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" at position 1 doesn\'t match format "%Y%m%d"$', | ||
match=r'^time data "True" doesn\'t match format "%Y%m%d", at position 1$', | ||
): | ||
to_datetime(["20130101", True], cache=cache) | ||
tm.assert_index_equal( | ||
|
@@ -1139,10 +1139,10 @@ def test_datetime_invalid_scalar(self, value, format, warning): | |
|
||
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$", | ||
r'^time data "a" doesn\'t match format "%H:%M:%S", at position 0$', | ||
r'^Given date string "a" not likely a datetime, at position 0$', | ||
r'^unconverted data remains: "9", at position 0$', | ||
r"^second must be in 0..59: 00:01:99, at position 0$", | ||
] | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
|
@@ -1164,11 +1164,11 @@ def test_datetime_outofbounds_scalar(self, value, format, warning): | |
assert res is NaT | ||
|
||
if format is not None: | ||
msg = r'^time data ".*" at position 0 doesn\'t match format ".*"$' | ||
msg = r'^time data ".*" doesn\'t match format ".*", at position 0$' | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(value, errors="raise", format=format) | ||
else: | ||
msg = "Out of bounds .* present at position 0" | ||
msg = "^Out of bounds .*, at position 0$" | ||
with pytest.raises( | ||
OutOfBoundsDatetime, match=msg | ||
), tm.assert_produces_warning(warning, match="Could not infer format"): | ||
|
@@ -1190,10 +1190,10 @@ def test_datetime_invalid_index(self, values, format, warning): | |
|
||
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$", | ||
r'^Given date string "a" not likely a datetime, at position 0$', | ||
r'^time data "a" doesn\'t match format "%H:%M:%S", at position 0$', | ||
r'^unconverted data remains: "9", at position 0$', | ||
r"^second must be in 0..59: 00:01:99, at position 0$", | ||
] | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
|
@@ -1373,7 +1373,7 @@ def test_to_datetime_malformed_raise(self): | |
ts_strings = ["200622-12-31", "111111-24-11"] | ||
with pytest.raises( | ||
ValueError, | ||
match=r"^hour must be in 0\.\.23: 111111-24-11 present at position 1$", | ||
match=r"^hour must be in 0\.\.23: 111111-24-11, at position 1$", | ||
): | ||
with tm.assert_produces_warning( | ||
UserWarning, match="Could not infer format" | ||
|
@@ -1814,8 +1814,8 @@ def test_dataframe_coerce(self, cache): | |
df2 = DataFrame({"year": [2015, 2016], "month": [2, 20], "day": [4, 5]}) | ||
|
||
msg = ( | ||
r'^cannot assemble the datetimes: time data ".+" at position 1 doesn\'t ' | ||
r'match format "%Y%m%d"$' | ||
r'^cannot assemble the datetimes: time data ".+" doesn\'t ' | ||
r'match format "%Y%m%d", at position 1$' | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(df2, cache=cache) | ||
|
@@ -1892,8 +1892,8 @@ def test_dataframe_float(self, cache): | |
# float | ||
df = DataFrame({"year": [2000, 2001], "month": [1.5, 1], "day": [1, 1]}) | ||
msg = ( | ||
r"^cannot assemble the datetimes: unconverted data remains at position " | ||
r'0: "1"$' | ||
r"^cannot assemble the datetimes: unconverted data remains: " | ||
r'"1", at position 0$' | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(df, cache=cache) | ||
|
@@ -1915,7 +1915,7 @@ def test_to_datetime_barely_out_of_bounds(self): | |
# in an in-bounds datetime | ||
arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object) | ||
|
||
msg = "Out of bounds .* present at position 0" | ||
msg = "^Out of bounds nanosecond timestamp: .*, at position 0" | ||
with pytest.raises(OutOfBoundsDatetime, match=msg): | ||
with tm.assert_produces_warning( | ||
UserWarning, match="Could not infer format" | ||
|
@@ -1954,8 +1954,8 @@ def test_to_datetime_iso8601_fails(self, input, format, exact): | |
with pytest.raises( | ||
ValueError, | ||
match=( | ||
rf"time data \"{input}\" at position 0 doesn't match format " | ||
rf"\"{format}\"" | ||
rf"time data \"{input}\" doesn't match format " | ||
rf"\"{format}\", at position 0" | ||
), | ||
): | ||
to_datetime(input, format=format, exact=exact) | ||
|
@@ -1976,8 +1976,8 @@ def test_to_datetime_iso8601_exact_fails(self, input, format): | |
with pytest.raises( | ||
ValueError, | ||
match=( | ||
rf"time data \"{input}\" at position 0 doesn't match format " | ||
rf"\"{format}\"" | ||
rf"time data \"{input}\" doesn't match format " | ||
rf"\"{format}\", at position 0" | ||
), | ||
): | ||
to_datetime(input, format=format) | ||
|
@@ -2015,8 +2015,8 @@ def test_to_datetime_iso8601_separator(self, input, format): | |
with pytest.raises( | ||
ValueError, | ||
match=( | ||
rf"time data \"{input}\" at position 0 doesn\'t match format " | ||
rf"\"{format}\"" | ||
rf"time data \"{input}\" doesn\'t match format " | ||
rf"\"{format}\", at position 0" | ||
), | ||
): | ||
to_datetime(input, format=format) | ||
|
@@ -2084,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 " " at position 2 doesn\'t match format "%m/%d/%Y"$' | ||
msg = r'^time data " " doesn\'t match format "%m/%d/%Y", at position 2$' | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(ser, errors="raise", cache=cache) | ||
result_coerce = to_datetime(ser, errors="coerce", cache=cache) | ||
|
@@ -2355,8 +2355,8 @@ def test_dayfirst_warnings_invalid_input(self): | |
with pytest.raises( | ||
ValueError, | ||
match=( | ||
r'^time data "03/30/2011" at position 1 doesn\'t match format ' | ||
r'"%d/%m/%Y"$' | ||
r'^time data "03/30/2011" doesn\'t match format ' | ||
r'"%d/%m/%Y", at position 1$' | ||
), | ||
): | ||
to_datetime(arr, dayfirst=True) | ||
|
@@ -2426,8 +2426,8 @@ 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)) | ||
msg = ( | ||
r'^time data "01-02-2011 00:00:00" at position 1 doesn\'t match format ' | ||
r'"%m/%d/%Y %H:%M:%S"$' | ||
r'^time data "01-02-2011 00:00:00" doesn\'t match format ' | ||
r'"%m/%d/%Y %H:%M:%S", at position 1$' | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(ser, cache=cache) | ||
|
@@ -2550,11 +2550,49 @@ def test_day_not_in_month_raise(self, cache): | |
): | ||
to_datetime("2015-02-29", errors="raise", cache=cache) | ||
|
||
@pytest.mark.parametrize("arg", ["2015-02-29", "2015-02-32", "2015-04-31"]) | ||
def test_day_not_in_month_raise_value(self, cache, arg): | ||
msg = f'time data "{arg}" at position 0 doesn\'t match format "%Y-%m-%d"' | ||
@pytest.mark.parametrize( | ||
"arg, format, msg", | ||
[ | ||
( | ||
"2015-02-29", | ||
"%Y-%m-%d", | ||
'^time data "2015-02-29" doesn\'t match format "%Y-%m-%d", ' | ||
"at position 0$", | ||
), | ||
( | ||
"2015-29-02", | ||
"%Y-%d-%m", | ||
"^day is out of range for month, at position 0$", | ||
), | ||
( | ||
"2015-02-32", | ||
"%Y-%m-%d", | ||
'^time data "2015-02-32" doesn\'t match format "%Y-%m-%d", ' | ||
"at position 0$", | ||
), | ||
( | ||
"2015-32-02", | ||
"%Y-%d-%m", | ||
'^time data "2015-32-02" doesn\'t match format "%Y-%d-%m", ' | ||
"at position 0$", | ||
), | ||
( | ||
"2015-04-31", | ||
"%Y-%m-%d", | ||
'^time data "2015-04-31" doesn\'t match format "%Y-%m-%d", ' | ||
"at position 0$", | ||
), | ||
( | ||
"2015-31-04", | ||
"%Y-%d-%m", | ||
"^day is out of range for month, at position 0$", | ||
), | ||
Comment on lines
+2579
to
+2589
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. note how the ISO and non-ISO messages don't quite match yet - this PR just ensures that they both show the position of the error #50242 will ensure that the messages match |
||
], | ||
) | ||
def test_day_not_in_month_raise_value(self, cache, arg, format, msg): | ||
# https://github.com/pandas-dev/pandas/issues/50462 | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(arg, errors="raise", format="%Y-%m-%d", cache=cache) | ||
to_datetime(arg, errors="raise", format=format, cache=cache) | ||
|
||
@pytest.mark.parametrize( | ||
"expected, format, warning", | ||
|
@@ -2934,7 +2972,7 @@ def test_invalid_origins_tzinfo(self): | |
def test_incorrect_value_exception(self): | ||
# GH47495 | ||
with pytest.raises( | ||
ValueError, match="Unknown string format: yesterday present at position 1" | ||
ValueError, match="Unknown string format: yesterday, at position 1" | ||
): | ||
with tm.assert_produces_warning( | ||
UserWarning, match="Could not infer format" | ||
|
@@ -2952,8 +2990,7 @@ def test_incorrect_value_exception(self): | |
def test_to_datetime_out_of_bounds_with_format_arg(self, format, warning): | ||
# see gh-23830 | ||
msg = ( | ||
r"^Out of bounds nanosecond timestamp: 2417-10-10 00:00:00 " | ||
r"present at position 0$" | ||
r"^Out of bounds nanosecond timestamp: 2417-10-10 00:00:00, at position 0$" | ||
) | ||
with pytest.raises(OutOfBoundsDatetime, match=msg): | ||
with tm.assert_produces_warning(warning, match="Could not infer format"): | ||
|
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.
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.
Rather than trying to set
as position {i}
in each place where an error can occur, it's more reliable to just preprendat position {i}
when an error is caught (see below)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.
I haven't done the same in tslib.pyx because when
format
is provided, that code is going to be called here anyway in #50242So for now I've just made sure the error messages match for the two paths