Skip to content

Commit 10636ca

Browse files
committed
removed string-to-dts-noexc and parse_iso_8601_datetime_noexc
1 parent 9d3b739 commit 10636ca

File tree

7 files changed

+59
-103
lines changed

7 files changed

+59
-103
lines changed

pandas/_libs/tslib.pyx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ from pandas._libs.util cimport (
2020
from pandas._libs.tslibs.c_timestamp cimport _Timestamp
2121

2222
from pandas._libs.tslibs.np_datetime cimport (
23-
_string_to_dts_noexc, check_dts_bounds, npy_datetimestruct, _string_to_dts,
23+
check_dts_bounds, npy_datetimestruct, _string_to_dts,
2424
dt64_to_dtstruct, dtstruct_to_dt64, pydatetime_to_dt64, pydate_to_dt64,
2525
get_datetime64_value)
2626
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
@@ -205,7 +205,8 @@ def _test_parse_iso8601(object ts):
205205
elif ts == 'today':
206206
return Timestamp.now().normalize()
207207

208-
_string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)
208+
if _string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset) == -1:
209+
raise ValueError
209210
obj.value = dtstruct_to_dt64(&obj.dts)
210211
check_dts_bounds(&obj.dts)
211212
if out_local == 1:
@@ -580,7 +581,7 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
580581
iresult[i] = NPY_NAT
581582
continue
582583

583-
string_to_dts_failed = _string_to_dts_noexc(
584+
string_to_dts_failed = _string_to_dts(
584585
val, &dts, &out_local,
585586
&out_tzoffset
586587
)

pandas/_libs/tslibs/conversion.pyx

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -443,47 +443,57 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
443443
ts = datetime.now(tz)
444444
# equiv: datetime.today().replace(tzinfo=tz)
445445
else:
446-
try:
447-
_string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)
448-
obj.value = dtstruct_to_dt64(&obj.dts)
449-
check_dts_bounds(&obj.dts)
450-
if out_local == 1:
451-
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
452-
obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
453-
if tz is None:
454-
check_dts_bounds(&obj.dts)
455-
check_overflows(obj)
456-
return obj
457-
else:
458-
# Keep the converter same as PyDateTime's
459-
obj = convert_to_tsobject(obj.value, obj.tzinfo,
460-
None, 0, 0)
461-
dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day,
462-
obj.dts.hour, obj.dts.min, obj.dts.sec,
463-
obj.dts.us, obj.tzinfo)
464-
obj = convert_datetime_to_tsobject(
465-
dt, tz, nanos=obj.dts.ps // 1000)
466-
return obj
467-
468-
else:
469-
ts = obj.value
470-
if tz is not None:
471-
# shift for localize_tso
472-
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz,
473-
ambiguous='raise')[0]
474-
475-
except OutOfBoundsDatetime:
476-
# GH#19382 for just-barely-OutOfBounds falling back to dateutil
477-
# parser will return incorrect result because it will ignore
478-
# nanoseconds
479-
raise
480-
481-
except ValueError:
446+
string_to_dts_failed = _string_to_dts(
447+
ts, &obj.dts, &out_local,
448+
&out_tzoffset
449+
)
450+
if string_to_dts_failed:
482451
try:
483452
ts = parse_datetime_string(ts, dayfirst=dayfirst,
484453
yearfirst=yearfirst)
485454
except Exception:
486455
raise ValueError("could not convert string to Timestamp")
456+
else:
457+
try:
458+
obj.value = dtstruct_to_dt64(&obj.dts)
459+
check_dts_bounds(&obj.dts)
460+
if out_local == 1:
461+
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
462+
obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
463+
if tz is None:
464+
check_dts_bounds(&obj.dts)
465+
check_overflows(obj)
466+
return obj
467+
else:
468+
# Keep the converter same as PyDateTime's
469+
obj = convert_to_tsobject(obj.value, obj.tzinfo,
470+
None, 0, 0)
471+
dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day,
472+
obj.dts.hour, obj.dts.min, obj.dts.sec,
473+
obj.dts.us, obj.tzinfo)
474+
obj = convert_datetime_to_tsobject(
475+
dt, tz, nanos=obj.dts.ps // 1000)
476+
return obj
477+
478+
else:
479+
ts = obj.value
480+
if tz is not None:
481+
# shift for localize_tso
482+
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz,
483+
ambiguous='raise')[0]
484+
485+
except OutOfBoundsDatetime:
486+
# GH#19382 for just-barely-OutOfBounds falling back to dateutil
487+
# parser will return incorrect result because it will ignore
488+
# nanoseconds
489+
raise
490+
491+
except ValueError:
492+
try:
493+
ts = parse_datetime_string(ts, dayfirst=dayfirst,
494+
yearfirst=yearfirst)
495+
except Exception:
496+
raise ValueError("could not convert string to Timestamp")
487497

488498
return convert_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
489499

pandas/_libs/tslibs/np_datetime.pxd

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,4 @@ cdef npy_timedelta get_timedelta64_value(object obj) nogil
7373
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil
7474

7575
cdef int _string_to_dts(object val, npy_datetimestruct* dts,
76-
int* out_local, int* out_tzoffset) except? -1
77-
# see np_datetime.pyx for reasons of second _noexc version
78-
cdef int _string_to_dts_noexc(object val, npy_datetimestruct* dts,
79-
int* out_local, int* out_tzoffset)
76+
int* out_local, int* out_tzoffset)

pandas/_libs/tslibs/np_datetime.pyx

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from cpython cimport Py_EQ, Py_NE, Py_GE, Py_GT, Py_LT, Py_LE, PyErr_Clear
1+
from cpython cimport Py_EQ, Py_NE, Py_GE, Py_GT, Py_LT, Py_LE
22

33
from cpython.datetime cimport (datetime, date,
44
PyDateTime_IMPORT,
@@ -31,12 +31,9 @@ cdef extern from "src/datetime/np_datetime.h":
3131
npy_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS
3232

3333
cdef extern from "src/datetime/np_datetime_strings.h":
34-
int parse_iso_8601_datetime(const char *str, int len,
34+
int parse_iso_8601_datetime(const char *str, int len, int want_exc,
3535
npy_datetimestruct *out,
3636
int *out_local, int *out_tzoffset)
37-
int parse_iso_8601_datetime_noexc(const char *str, int len,
38-
npy_datetimestruct *out,
39-
int *out_local, int *out_tzoffset)
4037

4138

4239
# ----------------------------------------------------------------------
@@ -173,30 +170,11 @@ cdef inline int64_t pydate_to_dt64(date val, npy_datetimestruct *dts):
173170

174171

175172
cdef inline int _string_to_dts(object val, npy_datetimestruct* dts,
176-
int* out_local, int* out_tzoffset) except? -1:
173+
int* out_local, int* out_tzoffset):
177174
cdef:
178175
Py_ssize_t length
179176
const char* buf
180177

181178
buf = get_c_string_buf_and_size(val, &length)
182-
return parse_iso_8601_datetime(buf, length,
179+
return parse_iso_8601_datetime(buf, length, 0,
183180
dts, out_local, out_tzoffset)
184-
185-
186-
# Slightly faster version that doesn't raise a ValueError
187-
# if a date cannot be parsed, it reports various errors via return result.
188-
# Caller must check that return value == 0 to determine if parsing succeeded.
189-
cdef inline int _string_to_dts_noexc(object val, npy_datetimestruct* dts,
190-
int* out_local, int* out_tzoffset):
191-
cdef:
192-
Py_ssize_t length
193-
const char* buf
194-
195-
buf = get_c_string_buf_and_size(val, &length)
196-
if buf == NULL:
197-
PyErr_Clear()
198-
return -1
199-
200-
result = parse_iso_8601_datetime_noexc(buf, length,
201-
dts, out_local, out_tzoffset);
202-
return result

pandas/_libs/tslibs/src/datetime/np_datetime_strings.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,7 @@ This file implements string parsing and creation for NumPy datetime.
6666
*
6767
* Returns 0 on success, -1 on failure.
6868
*/
69-
static int __parse_iso_8601_datetime(const char *str, int len, int want_exc,
70-
npy_datetimestruct *out,
71-
int *out_local, int *out_tzoffset);
72-
73-
int parse_iso_8601_datetime(const char *str, int len,
74-
npy_datetimestruct *out,
75-
int *out_local, int *out_tzoffset) {
76-
return __parse_iso_8601_datetime(str, len, 1, out, out_local, out_tzoffset);
77-
}
78-
79-
// slightly faster version of parse_iso_8601_datetime which
80-
// doesn't set Python exceptions but still returns -1 on error
81-
int parse_iso_8601_datetime_noexc(const char *str, int len,
82-
npy_datetimestruct *out,
83-
int *out_local, int *out_tzoffset) {
84-
return __parse_iso_8601_datetime(str, len, 0, out, out_local, out_tzoffset);
85-
}
86-
87-
static int __parse_iso_8601_datetime(const char *str, int len, int want_exc,
69+
int parse_iso_8601_datetime(const char *str, int len, int want_exc,
8870
npy_datetimestruct *out,
8971
int *out_local, int *out_tzoffset) {
9072
int year_leap = 0;

pandas/_libs/tslibs/src/datetime/np_datetime_strings.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,10 @@ This file implements string parsing and creation for NumPy datetime.
5454
* Returns 0 on success, -1 on failure.
5555
*/
5656
int
57-
parse_iso_8601_datetime(const char *str, int len,
57+
parse_iso_8601_datetime(const char *str, int len, int want_exc,
5858
npy_datetimestruct *out,
5959
int *out_local,
6060
int *out_tzoffset);
61-
// slightly faster version of parse_iso_8601_datetime which
62-
// doesn't set Python exceptions but still returns -1 on error
63-
int
64-
parse_iso_8601_datetime_noexc(const char *str, int len,
65-
npy_datetimestruct *out,
66-
int *out_local,
67-
int *out_tzoffset);
6861

6962
/*
7063
* Provides a string length to use for converting datetime

pandas/tests/tslibs/test_parse_iso8601.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,13 @@ def test_parsers_iso8601(date_str, exp):
4646
"20010101 12345Z",
4747
])
4848
def test_parsers_iso8601_invalid(date_str):
49-
msg = "Error parsing datetime string \"{s}\"".format(s=date_str)
50-
51-
with pytest.raises(ValueError, match=msg):
49+
with pytest.raises(ValueError):
5250
tslib._test_parse_iso8601(date_str)
5351

5452

5553
def test_parsers_iso8601_invalid_offset_invalid():
5654
date_str = "2001-01-01 12-34-56"
57-
msg = ("Timezone hours offset out of range "
58-
"in datetime string \"{s}\"".format(s=date_str))
59-
60-
with pytest.raises(ValueError, match=msg):
55+
with pytest.raises(ValueError):
6156
tslib._test_parse_iso8601(date_str)
6257

6358

0 commit comments

Comments
 (0)