Skip to content

Commit 72a6ed8

Browse files
committed
Speed up parse_datetime_string_with_reso
1 parent c770661 commit 72a6ed8

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

pandas/_libs/tslibs/parsing.pyx

+12-6
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,30 @@ cdef inline object parse_delimited_date(object date_string, bint dayfirst,
9090
buf = get_c_string_buf_and_size(date_string, &length)
9191
if length == 10:
9292
if _is_not_delimiter(buf[2]) or _is_not_delimiter(buf[5]):
93-
return None
93+
return None, None
9494
month = _parse_2digit(buf)
9595
day = _parse_2digit(buf + 3)
9696
year = _parse_4digit(buf + 6)
97+
reso = 'day'
9798
elif length == 7:
9899
if _is_not_delimiter(buf[2]):
99-
return None
100+
return None, None
100101
month = _parse_2digit(buf)
101102
year = _parse_4digit(buf + 3)
103+
reso = 'month'
102104
else:
103-
return None
105+
return None, None
104106

105107
if month < 0 or day < 0 or year < 0:
106108
# some part is not an integer, so it's not a mm/dd/yyyy date
107-
return None
109+
return None, None
108110

109111
if 1 <= month <= MAX_DAYS_IN_MONTH and 1 <= day <= MAX_DAYS_IN_MONTH \
110112
and (month <= MAX_MONTH or day <= MAX_MONTH):
111113
if month > MAX_MONTH or (day < MAX_MONTH and dayfirst):
112114
day, month = month, day
113115
return PyDateTimeAPI.DateTime_FromDateAndTime(year, month, day,
114-
0, 0, 0, 0, tzinfo, PyDateTimeAPI.DateTimeType)
116+
0, 0, 0, 0, tzinfo, PyDateTimeAPI.DateTimeType), reso
115117

116118
raise DateParseError("Invalid date specified (%d/%d)" %
117119
(month, day))
@@ -140,7 +142,7 @@ def parse_datetime_string(date_string, freq=None, dayfirst=False,
140142
yearfirst=yearfirst, **kwargs)
141143
return dt
142144

143-
dt = parse_delimited_date(date_string, dayfirst, _DEFAULT_TZINFO)
145+
dt, _ = parse_delimited_date(date_string, dayfirst, _DEFAULT_TZINFO)
144146
if dt is not None:
145147
return dt
146148

@@ -224,6 +226,10 @@ cdef parse_datetime_string_with_reso(date_string, freq=None, dayfirst=False,
224226
if not _does_string_look_like_datetime(date_string):
225227
raise ValueError('Given date string not likely a datetime.')
226228

229+
parsed, reso = parse_delimited_date(date_string, dayfirst, _DEFAULT_TZINFO)
230+
if parsed is not None:
231+
return parsed, parsed, reso
232+
227233
try:
228234
return _parse_dateabbr_string(date_string, _DEFAULT_DATETIME, freq)
229235
except DateParseError:

0 commit comments

Comments
 (0)