Skip to content

Commit 9dc3f7d

Browse files
author
MarcoGorelli
committed
wip
1 parent d9cf121 commit 9dc3f7d

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Other enhancements
9898
- Added :meth:`Index.infer_objects` analogous to :meth:`Series.infer_objects` (:issue:`50034`)
9999
- Added ``copy`` parameter to :meth:`Series.infer_objects` and :meth:`DataFrame.infer_objects`, passing ``False`` will avoid making copies for series or columns that are already non-object or where no better dtype can be inferred (:issue:`50096`)
100100
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
101+
- Improved error message in :func:`to_datetime` for non-ISO8601 formats, informing users about the position of the first error (:issue:`50361`)
101102
-
102103

103104
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/strptime.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ def array_strptime(
469469

470470
result_timezone[i] = tz
471471

472-
except (ValueError, OutOfBoundsDatetime):
472+
except (ValueError, OutOfBoundsDatetime) as ex:
473+
ex.args = (str(ex) + f" at position {i}", )
473474
if is_coerce:
474475
iresult[i] = NPY_NAT
475476
continue

pandas/tests/io/parser/test_parse_dates.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,10 @@ def test_parse_multiple_delimited_dates_with_swap_warnings():
17171717
# GH46210
17181718
with pytest.raises(
17191719
ValueError,
1720-
match=r"^time data '31/05/2000' does not match format '%m/%d/%Y' \(match\)$",
1720+
match=(
1721+
r"^time data '31/05/2000' does not match format '%m/%d/%Y' \(match\) "
1722+
"at position 1$"
1723+
),
17211724
):
17221725
pd.to_datetime(["01/01/2000", "31/05/2000", "31/05/2001", "01/02/2000"])
17231726

pandas/tests/tools/test_to_datetime.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,10 @@ def test_datetime_bool_arrays_mixed(self, cache):
12051205
to_datetime([False, datetime.today()], cache=cache)
12061206
with pytest.raises(
12071207
ValueError,
1208-
match=r"^time data 'True' does not match format '%Y%m%d' \(match\)$",
1208+
match=(
1209+
r"^time data 'True' does not match format '%Y%m%d' "
1210+
r"\(match\) at position 1$"
1211+
),
12091212
):
12101213
to_datetime(["20130101", True], cache=cache)
12111214
tm.assert_index_equal(
@@ -2197,7 +2200,9 @@ def test_to_datetime_on_datetime64_series(self, cache):
21972200
def test_to_datetime_with_space_in_series(self, cache):
21982201
# GH 6428
21992202
ser = Series(["10/18/2006", "10/18/2008", " "])
2200-
msg = r"^time data ' ' does not match format '%m/%d/%Y' \(match\)$"
2203+
msg = (
2204+
r"^time data ' ' does not match format '%m/%d/%Y' \(match\) at position 2$"
2205+
)
22012206
with pytest.raises(ValueError, match=msg):
22022207
to_datetime(ser, errors="raise", cache=cache)
22032208
result_coerce = to_datetime(ser, errors="coerce", cache=cache)
@@ -2467,7 +2472,10 @@ def test_dayfirst_warnings_invalid_input(self):
24672472

24682473
with pytest.raises(
24692474
ValueError,
2470-
match=r"time data '03/30/2011' does not match format '%d/%m/%Y' \(match\)$",
2475+
match=(
2476+
r"time data '03/30/2011' does not match format '%d/%m/%Y' "
2477+
r"\(match\) at position 1$"
2478+
),
24712479
):
24722480
to_datetime(arr, dayfirst=True)
24732481

@@ -3048,14 +3056,22 @@ def test_incorrect_value_exception(self):
30483056
to_datetime(["today", "yesterday"])
30493057

30503058
@pytest.mark.parametrize(
3051-
"format, warning", [(None, UserWarning), ("%Y-%m-%d %H:%M:%S", None)]
3059+
"format, warning",
3060+
[
3061+
(None, UserWarning),
3062+
("%Y-%m-%d %H:%M:%S", None),
3063+
("%Y-%d-%m %H:%M:%S", None),
3064+
],
30523065
)
30533066
def test_to_datetime_out_of_bounds_with_format_arg(self, format, warning):
30543067
# see gh-23830
3055-
msg = "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00"
3068+
msg = (
3069+
r"Out of bounds nanosecond timestamp: 2417-10-10 00:00:00"
3070+
r".* at position 0"
3071+
)
30563072
with pytest.raises(OutOfBoundsDatetime, match=msg):
30573073
with tm.assert_produces_warning(warning, match="Could not infer format"):
3058-
to_datetime("2417-10-27 00:00:00", format=format)
3074+
to_datetime("2417-10-10 00:00:00", format=format)
30593075

30603076
@pytest.mark.parametrize(
30613077
"arg, origin, expected_str",

0 commit comments

Comments
 (0)