Skip to content

Commit 262be89

Browse files
author
MarcoGorelli
committed
point to format=ISO8601 in error message
1 parent eb36d8c commit 262be89

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

pandas/_libs/tslibs/strptime.pyx

+6-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,12 @@ def array_strptime(
533533
result_timezone[i] = tz
534534

535535
except (ValueError, OutOfBoundsDatetime) as ex:
536-
ex.args = (f"{str(ex)}, at position {i}",)
536+
if iso_format:
537+
ex.args = (f"{str(ex)}, at position {i}. If your time strings "
538+
"are all (not-necessarily-identically-formatted) ISO8601, "
539+
"you could try passing 'format=\"ISO8601\"'",)
540+
else:
541+
ex.args = (f"{str(ex)}, at position {i}",)
537542
if is_coerce:
538543
iresult[i] = NPY_NAT
539544
continue

pandas/tests/tools/test_to_datetime.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,9 @@ def test_datetime_bool_arrays_mixed(self, cache):
12951295
ValueError,
12961296
match=(
12971297
r'^time data "True" doesn\'t match \(inferred\) format "%Y%m%d", '
1298-
"at position 1$"
1298+
"at position 1. If your time strings are all "
1299+
r"\(not-necessarily-identically-formatted\) ISO8601, you could "
1300+
"try passing 'format=\"ISO8601\"'$"
12991301
),
13001302
):
13011303
to_datetime(["20130101", True], cache=cache)
@@ -2093,7 +2095,9 @@ def test_dataframe_coerce(self, cache):
20932095

20942096
msg = (
20952097
r'^cannot assemble the datetimes: time data ".+" doesn\'t '
2096-
r'match format "%Y%m%d", at position 1$'
2098+
r'match format "%Y%m%d", at position 1. '
2099+
r"If your time strings are all \(not-necessarily-identically-formatted\) "
2100+
"ISO8601, you could try passing 'format=\"ISO8601\"'$"
20972101
)
20982102
with pytest.raises(ValueError, match=msg):
20992103
to_datetime(df2, cache=cache)
@@ -2171,7 +2175,9 @@ def test_dataframe_float(self, cache):
21712175
df = DataFrame({"year": [2000, 2001], "month": [1.5, 1], "day": [1, 1]})
21722176
msg = (
21732177
r"^cannot assemble the datetimes: unconverted data remains when parsing "
2174-
r'with format ".*": "1", at position 0$'
2178+
r'with format ".*": "1", at position 0. '
2179+
r"If your time strings are all \(not-necessarily-identically-formatted\) "
2180+
"ISO8601, you could try passing 'format=\"ISO8601\"'$"
21752181
)
21762182
with pytest.raises(ValueError, match=msg):
21772183
to_datetime(df, cache=cache)
@@ -2254,7 +2260,9 @@ def test_to_datetime_iso8601_exact_fails(self, input, format):
22542260
msg = "|".join(
22552261
[
22562262
'^unconverted data remains when parsing with format ".*": ".*"'
2257-
", at position 0$",
2263+
", at position 0. "
2264+
r"If your time strings are all \(not-necessarily-identically-"
2265+
r"formatted\) ISO8601, you could try passing 'format=\"ISO8601\"'$",
22582266
'time data ".*" doesn\'t match format ".*", at position 0',
22592267
]
22602268
)
@@ -2856,7 +2864,9 @@ def test_day_not_in_month_raise(self, cache):
28562864
(
28572865
"2015-02-29",
28582866
"%Y-%m-%d",
2859-
"^day is out of range for month, at position 0$",
2867+
"^day is out of range for month, at position 0. "
2868+
r"If your time strings are all \(not-necessarily-identically-"
2869+
r"formatted\) ISO8601, you could try passing 'format=\"ISO8601\"'$",
28602870
),
28612871
(
28622872
"2015-29-02",
@@ -2867,7 +2877,9 @@ def test_day_not_in_month_raise(self, cache):
28672877
"2015-02-32",
28682878
"%Y-%m-%d",
28692879
'^unconverted data remains when parsing with format "%Y-%m-%d": "2", '
2870-
"at position 0$",
2880+
"at position 0. "
2881+
r"If your time strings are all \(not-necessarily-identically-"
2882+
r"formatted\) ISO8601, you could try passing 'format=\"ISO8601\"'$",
28712883
),
28722884
(
28732885
"2015-32-02",
@@ -2878,7 +2890,9 @@ def test_day_not_in_month_raise(self, cache):
28782890
(
28792891
"2015-04-31",
28802892
"%Y-%m-%d",
2881-
"^day is out of range for month, at position 0$",
2893+
"^day is out of range for month, at position 0. "
2894+
r"If your time strings are all \(not-necessarily-identically-"
2895+
r"formatted\) ISO8601, you could try passing 'format=\"ISO8601\"'$",
28822896
),
28832897
(
28842898
"2015-31-04",
@@ -3290,9 +3304,7 @@ def test_incorrect_value_exception(self):
32903304
)
32913305
def test_to_datetime_out_of_bounds_with_format_arg(self, format, warning):
32923306
# see gh-23830
3293-
msg = (
3294-
r"^Out of bounds nanosecond timestamp: 2417-10-10 00:00:00, at position 0$"
3295-
)
3307+
msg = r"^Out of bounds nanosecond timestamp: 2417-10-10 00:00:00, at position 0"
32963308
with pytest.raises(OutOfBoundsDatetime, match=msg):
32973309
with tm.assert_produces_warning(warning, match="Could not infer format"):
32983310
to_datetime("2417-10-10 00:00:00", format=format)

0 commit comments

Comments
 (0)