-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
BUG: Behavior with fallback between raise and coerce #46071 #47745
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 9 commits
0d320e9
df08fd1
7c69de8
2ab558a
78e6bc7
3da1585
b53d897
ef1f736
8867090
7347c78
3b08c71
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 |
|---|---|---|
|
|
@@ -3,11 +3,13 @@ | |
| """ | ||
| import operator | ||
|
|
||
| from dateutil.parser._parser import ParserError | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from pandas._libs.tslibs import tz_compare | ||
| from pandas._libs.tslibs.dtypes import NpyDatetimeUnit | ||
| from pandas.errors import OutOfBoundsDatetime | ||
|
|
||
| from pandas.core.dtypes.dtypes import DatetimeTZDtype | ||
|
|
||
|
|
@@ -639,3 +641,41 @@ def test_tz_localize_t2d(self): | |
|
|
||
| roundtrip = expected.tz_localize("US/Pacific") | ||
| tm.assert_datetime_array_equal(roundtrip, dta) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "error", | ||
| ["coerce", "raise"], | ||
| ) | ||
| def test_coerce_fallback(self, error): | ||
| # GH#46071 | ||
datapythonista marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # 2 valid dates with different formats | ||
| # Should parse with no errors | ||
| s = pd.Series(["6/30/2025", "1 27 2024"]) | ||
| expected = pd.Series( | ||
| [pd.Timestamp("2025-06-30 00:00:00"), pd.Timestamp("2024-01-27 00:00:00")] | ||
| ) | ||
| result = pd.to_datetime(s, errors=error, infer_datetime_format=True) | ||
| tm.assert_series_equal(expected, result) | ||
|
|
||
| # Invalid inputs | ||
| # Errors should be raised for the second element | ||
| expected2 = pd.Series([pd.Timestamp("2000-01-01 00:00:00"), pd.NaT]) | ||
| # Out of bounds date | ||
| es1 = pd.Series(["1/1/2000", "7/12/1200"]) | ||
| # Invalid input string | ||
| es2 = pd.Series(["1/1/2000", "Invalid input"]) | ||
| if error == "coerce": | ||
|
||
| eres1 = pd.to_datetime(es1, errors=error, infer_datetime_format=True) | ||
| tm.assert_series_equal(expected2, eres1) | ||
| eres2 = pd.to_datetime(es2, errors=error, infer_datetime_format=True) | ||
| tm.assert_series_equal(expected2, eres2) | ||
| else: | ||
| with pytest.raises( | ||
| OutOfBoundsDatetime, match="Out of bounds nanosecond timestamp" | ||
| ): | ||
| pd.to_datetime(es1, errors=error, infer_datetime_format=True) | ||
|
|
||
| with pytest.raises( | ||
| ParserError, match="Unknown string format: Invalid input" | ||
| ): | ||
| pd.to_datetime(es2, errors=error, infer_datetime_format=True) | ||
Uh oh!
There was an error while loading. Please reload this page.