Skip to content

Commit 303a648

Browse files
author
MarcoGorelli
committed
wip
1 parent ca04349 commit 303a648

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
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
@@ -396,7 +396,8 @@ def array_strptime(
396396

397397
result_timezone[i] = tz
398398

399-
except (ValueError, OutOfBoundsDatetime):
399+
except (ValueError, OutOfBoundsDatetime) as ex:
400+
ex.args = (str(ex) + f" at position {i}", )
400401
if is_coerce:
401402
iresult[i] = NPY_NAT
402403
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: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,10 @@ def test_datetime_bool_arrays_mixed(self, cache):
10931093
to_datetime([False, datetime.today()], cache=cache)
10941094
with pytest.raises(
10951095
ValueError,
1096-
match=r"^time data 'True' does not match format '%Y%m%d' \(match\)$",
1096+
match=(
1097+
r"^time data 'True' does not match format '%Y%m%d' "
1098+
r"\(match\) at position 1$"
1099+
),
10971100
):
10981101
to_datetime(["20130101", True], cache=cache)
10991102
tm.assert_index_equal(
@@ -2072,7 +2075,9 @@ def test_to_datetime_on_datetime64_series(self, cache):
20722075
def test_to_datetime_with_space_in_series(self, cache):
20732076
# GH 6428
20742077
ser = Series(["10/18/2006", "10/18/2008", " "])
2075-
msg = r"^time data ' ' does not match format '%m/%d/%Y' \(match\)$"
2078+
msg = (
2079+
r"^time data ' ' does not match format '%m/%d/%Y' \(match\) at position 2$"
2080+
)
20762081
with pytest.raises(ValueError, match=msg):
20772082
to_datetime(ser, errors="raise", cache=cache)
20782083
result_coerce = to_datetime(ser, errors="coerce", cache=cache)
@@ -2342,7 +2347,10 @@ def test_dayfirst_warnings_invalid_input(self):
23422347

23432348
with pytest.raises(
23442349
ValueError,
2345-
match=r"time data '03/30/2011' does not match format '%d/%m/%Y' \(match\)$",
2350+
match=(
2351+
r"time data '03/30/2011' does not match format '%d/%m/%Y' "
2352+
r"\(match\) at position 1$"
2353+
),
23462354
):
23472355
to_datetime(arr, dayfirst=True)
23482356

@@ -2923,17 +2931,22 @@ def test_incorrect_value_exception(self):
29232931
to_datetime(["today", "yesterday"])
29242932

29252933
@pytest.mark.parametrize(
2926-
"format, warning", [(None, UserWarning), ("%Y-%m-%d %H:%M:%S", None)]
2934+
"format, warning",
2935+
[
2936+
(None, UserWarning),
2937+
("%Y-%m-%d %H:%M:%S", None),
2938+
("%Y-%d-%m %H:%M:%S", None),
2939+
],
29272940
)
29282941
def test_to_datetime_out_of_bounds_with_format_arg(self, format, warning):
29292942
# see gh-23830
29302943
msg = (
2931-
"Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 "
2932-
"present at position 0"
2944+
r"Out of bounds nanosecond timestamp: 2417-10-10 00:00:00"
2945+
r".* at position 0"
29332946
)
29342947
with pytest.raises(OutOfBoundsDatetime, match=msg):
29352948
with tm.assert_produces_warning(warning, match="Could not infer format"):
2936-
to_datetime("2417-10-27 00:00:00", format=format)
2949+
to_datetime("2417-10-10 00:00:00", format=format)
29372950

29382951
@pytest.mark.parametrize(
29392952
"arg, origin, expected_str",

0 commit comments

Comments
 (0)