Skip to content

Commit 392d239

Browse files
author
MarcoGorelli
committed
fixup tests
1 parent 3da6ceb commit 392d239

File tree

2 files changed

+56
-57
lines changed

2 files changed

+56
-57
lines changed

pandas/_libs/tslibs/strptime.pyx

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def array_strptime(
137137
exact : matches must be exact if True, search if False
138138
errors : string specifying error handling, {'raise', 'ignore', 'coerce'}
139139
"""
140+
140141
cdef:
141142
Py_ssize_t i, n = len(values)
142143
npy_datetimestruct dts
@@ -277,57 +278,45 @@ def array_strptime(
277278
else:
278279
val = str(val)
279280

280-
if iso_format:
281-
string_to_dts_failed = string_to_dts(
282-
val, &dts, &out_bestunit, &out_local,
283-
&out_tzoffset, False, fmt, exact
284-
)
285-
if not string_to_dts_failed:
286-
# No error reported by string_to_dts, pick back up
287-
# where we left off
288-
value = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts)
289-
if out_local == 1:
290-
# Store the out_tzoffset in seconds
291-
# since we store the total_seconds of
292-
# dateutil.tz.tzoffset objects
293-
tz = timezone(timedelta(minutes=out_tzoffset))
294-
result_timezone[i] = tz
295-
out_local = 0
296-
out_tzoffset = 0
297-
iresult[i] = value
298-
try:
281+
if iso_format:
282+
string_to_dts_failed = string_to_dts(
283+
val, &dts, &out_bestunit, &out_local,
284+
&out_tzoffset, False, fmt, exact
285+
)
286+
if not string_to_dts_failed:
287+
# No error reported by string_to_dts, pick back up
288+
# where we left off
289+
value = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts)
290+
if out_local == 1:
291+
# Store the out_tzoffset in seconds
292+
# since we store the total_seconds of
293+
# dateutil.tz.tzoffset objects
294+
tz = timezone(timedelta(minutes=out_tzoffset))
295+
result_timezone[i] = tz
296+
out_local = 0
297+
out_tzoffset = 0
298+
iresult[i] = value
299299
check_dts_bounds(&dts)
300-
except ValueError:
301-
if is_coerce:
302-
iresult[i] = NPY_NAT
303-
continue
304-
raise
305-
continue
300+
continue
306301

307-
if parse_today_now(val, &iresult[i], utc):
308-
continue
302+
if parse_today_now(val, &iresult[i], utc):
303+
continue
309304

310-
# Some ISO formats can't be parsed by string_to_dts
311-
# For example, 6-digit YYYYMD. So, if there's an error,
312-
# try the string-matching code below.
305+
# Some ISO formats can't be parsed by string_to_dts
306+
# For example, 6-digit YYYYMD. So, if there's an error,
307+
# try the string-matching code below.
313308

314-
# exact matching
315-
if exact:
316-
found = format_regex.match(val)
317-
if not found:
318-
if is_coerce:
319-
iresult[i] = NPY_NAT
320-
continue
321-
raise ValueError(f"time data \"{val}\" at position {i} doesn't "
322-
f"match format \"{fmt}\"")
323-
if len(val) != found.end():
324-
if is_coerce:
325-
iresult[i] = NPY_NAT
326-
continue
327-
raise ValueError(
328-
f"unconverted data remains at position {i}: "
329-
f'"{val[found.end():]}"'
330-
)
309+
# exact matching
310+
if exact:
311+
found = format_regex.match(val)
312+
if not found:
313+
raise ValueError(f"time data \"{val}\" at position {i} doesn't "
314+
f"match format \"{fmt}\"")
315+
if len(val) != found.end():
316+
raise ValueError(
317+
f"unconverted data remains at position {i}: "
318+
f'"{val[found.end():]}"'
319+
)
331320

332321
# search
333322
else:

pandas/tests/tools/test_to_datetime.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ def test_to_datetime_format_YYYYMMDD_with_nat(self, cache):
132132
# string with NaT
133133
ser2 = ser.apply(str)
134134
ser2[2] = "nat"
135-
with pytest.raises(ValueError, match="unconverted data remains: .0"):
135+
with pytest.raises(
136+
ValueError, match='unconverted data remains at position 0: ".0'
137+
):
136138
# https://github.com/pandas-dev/pandas/issues/50051
137139
to_datetime(ser2, format="%Y%m%d", cache=cache)
138140

@@ -2066,8 +2068,8 @@ def test_to_datetime_iso8601_fails(self, input, format, exact):
20662068
with pytest.raises(
20672069
ValueError,
20682070
match=(
2069-
rf"time data '{input}' does not match format "
2070-
rf"\'{format}\' \((match|search)\) at position 0"
2071+
rf"time data \"{input}\" at position 0 doesn't match format "
2072+
rf"\"{format}\""
20712073
),
20722074
):
20732075
to_datetime(input, format=format, exact=exact)
@@ -2085,10 +2087,13 @@ def test_to_datetime_iso8601_fails(self, input, format, exact):
20852087
def test_to_datetime_iso8601_exact_fails(self, input, format):
20862088
# https://github.com/pandas-dev/pandas/issues/12649
20872089
# `format` is shorter than the date string, so only fails with `exact=True`
2088-
with pytest.raises(
2089-
ValueError,
2090-
match="(unconverted data remains: |does not match format).* at position 0",
2091-
):
2090+
msg = "|".join(
2091+
[
2092+
'^unconverted data remains at position 0: ".*"$',
2093+
'^time data "0" at position 0 doesn\'t match format ".*"$',
2094+
]
2095+
)
2096+
with pytest.raises(ValueError, match=msg):
20922097
to_datetime(input, format=format)
20932098

20942099
@pytest.mark.parametrize(
@@ -2124,8 +2129,8 @@ def test_to_datetime_iso8601_separator(self, input, format):
21242129
with pytest.raises(
21252130
ValueError,
21262131
match=(
2127-
rf"time data '{input}' does not match format "
2128-
rf"'{format}' \(match\) at position 0"
2132+
rf"time data \"{input}\" at position 0 doesn\'t match format "
2133+
rf"\"{format}\""
21292134
),
21302135
):
21312136
to_datetime(input, format=format)
@@ -2683,7 +2688,12 @@ def test_day_not_in_month_raise(self, cache):
26832688

26842689
@pytest.mark.parametrize("arg", ["2015-02-29", "2015-02-32", "2015-04-31"])
26852690
def test_day_not_in_month_raise_value(self, cache, arg):
2686-
msg = "(day is out of range for month|unconverted data remains).* at position 0"
2691+
msg = "|".join(
2692+
[
2693+
"^day is out of range for month$",
2694+
'^unconverted data remains at position 0: "2"$',
2695+
]
2696+
)
26872697
with pytest.raises(ValueError, match=msg):
26882698
to_datetime(arg, errors="raise", format="%Y-%m-%d", cache=cache)
26892699

0 commit comments

Comments
 (0)