Skip to content

Commit 0eaa6da

Browse files
author
Marco Gorelli
committed
fixups
1 parent 8639ad3 commit 0eaa6da

File tree

7 files changed

+32
-30
lines changed

7 files changed

+32
-30
lines changed

pandas/_libs/tslib.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ cpdef array_to_datetime(
624624
continue
625625
elif is_raise:
626626
raise ValueError(
627-
f"time data \"{val}\" at position {i} doesn't match \"{format}\""
627+
f"time data \"{val}\" at position {i} doesn't match format \"{format}\""
628628
)
629629
return values, tz_out
630630

pandas/_libs/tslibs/np_datetime.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ cdef extern from "src/datetime/np_datetime_strings.h":
5353
npy_datetimestruct *out,
5454
NPY_DATETIMEUNIT *out_bestunit,
5555
int *out_local, int *out_tzoffset,
56-
const char *format, int exact)
56+
const char *format, int format_len, int exact)
5757

5858

5959
# ----------------------------------------------------------------------
@@ -287,7 +287,7 @@ cdef inline int string_to_dts(
287287
format_buf = get_c_string_buf_and_size(format, &format_length)
288288
return parse_iso_8601_datetime(buf, length, want_exc,
289289
dts, out_bestunit, out_local, out_tzoffset,
290-
format_buf, exact)
290+
format_buf, format_length, exact)
291291

292292

293293
cpdef ndarray astype_overflowsafe(

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,40 @@ This file implements string parsing and creation for NumPy datetime.
6666
* they aren't using the computer's local timezone offset.
6767
*
6868
* Returns 0 on success, -1 on failure.
69+
70+
so, basically:
71+
- if exact, need to check that it matches and that we're not at the end of the string
72+
- if not exact, then either it matches, or we're at the end of the string
73+
6974
*/
7075

7176
#define FORMAT_STARTSWITH(ch) \
72-
if (format_len > 0){ \
73-
if (*format != ch) { \
74-
goto parse_error; \
75-
} \
76-
++format; \
77+
if (exact){ \
78+
if (!format_len || *format != ch){ \
79+
goto parse_error; \
80+
} \
81+
++format; \
82+
--format_len; \
83+
} else { \
84+
if (format_len > 0){ \
85+
if (*format != ch) { \
86+
goto parse_error; \
87+
} \
88+
++format; \
89+
--format_len; \
90+
} \
7791
} \
7892

7993
int parse_iso_8601_datetime(const char *str, int len, int want_exc,
8094
npy_datetimestruct *out,
8195
NPY_DATETIMEUNIT *out_bestunit,
8296
int *out_local, int *out_tzoffset,
83-
const char* format, int exact) {
84-
printf("entering\n");
97+
const char* format, int format_len, int exact) {
8598
int year_leap = 0;
8699
int i, numdigits;
87100
const char *substr;
88101
int sublen;
89102
NPY_DATETIMEUNIT bestunit = NPY_FR_GENERIC;
90-
int format_len = strlen(format);
91103
printf("format len: %d", format_len);
92104

93105
/* If year-month-day are separated by a valid separator,
@@ -118,26 +130,14 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
118130
while (sublen > 0 && isspace(*substr)) {
119131
++substr;
120132
--sublen;
121-
if (!isspace(*format)) {
122-
goto parse_error;
123-
}
124-
++format;
125-
if (!*format) {
126-
goto parse_error;
127-
}
133+
FORMAT_STARTSWITH(' ');
128134
}
129135

130136
/* Leading '-' sign for negative year */
131137
if (*substr == '-') {
132138
++substr;
133139
--sublen;
134-
if (*format != '-') {
135-
goto parse_error;
136-
}
137-
++format;
138-
if (!*format) {
139-
goto parse_error;
140-
}
140+
FORMAT_STARTSWITH('-');
141141
}
142142

143143
FORMAT_STARTSWITH('%');

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ parse_iso_8601_datetime(const char *str, int len, int want_exc,
6060
int *out_local,
6161
int *out_tzoffset,
6262
const char* format,
63+
int format_len,
6364
int exact);
6465

6566
/*

pandas/core/arrays/datetimes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ def objects_to_datetime64ns(
22112211
yearfirst=yearfirst,
22122212
require_iso8601=require_iso8601,
22132213
allow_mixed=allow_mixed,
2214-
format=format,
2214+
format=format or "",
22152215
exact=exact
22162216
)
22172217
result = result.reshape(data.shape, order=order)

pandas/core/tools/datetimes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ def _convert_listlike_datetimes(
443443
errors=errors,
444444
require_iso8601=require_iso8601,
445445
allow_object=True,
446-
format=format, exact=False
446+
format=format,
447+
exact=exact,
447448
)
448449

449450
if tz_parsed is not None:

pandas/tests/tools/test_to_datetime.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ def test_to_datetime_iso8601_fails(self, input, format, exact):
17671767
# https://github.com/pandas-dev/pandas/issues/12649
17681768
with pytest.raises(
17691769
ValueError,
1770-
match=rf"time data \"{input}\" at position 0 doesn't match format {format}",
1770+
match=rf"time data \"{input}\" at position 0 doesn't match format \"{format}\"",
17711771
):
17721772
to_datetime(input, format=format, exact=exact)
17731773

@@ -1785,7 +1785,7 @@ def test_to_datetime_iso8601_exact_fails(self, input, format):
17851785
# https://github.com/pandas-dev/pandas/issues/12649
17861786
with pytest.raises(
17871787
ValueError,
1788-
match=rf"time data \"{input}\" at position 0 doesn't match format {format}",
1788+
match=rf"time data \"{input}\" at position 0 doesn't match format \"{format}\"",
17891789
):
17901790
to_datetime(input, format=format)
17911791

@@ -1822,7 +1822,7 @@ def test_to_datetime_iso8601_separator(self, input, format):
18221822
with pytest.raises(
18231823
ValueError,
18241824
match=(
1825-
rf"time data \"{input}\" at position 0 doesn\'t match format {format}"
1825+
rf"time data \"{input}\" at position 0 doesn\'t match format \"{format}\""
18261826
),
18271827
):
18281828
to_datetime(input, format=format)

0 commit comments

Comments
 (0)