Skip to content

Commit 38a65a6

Browse files
committed
BUG: fix IndexError for invalid integer dates %Y%m%d with errors='ignore' (# GH 26583)
array_strptime returned IndexError when trying to slice 'too long' integer for the given format %Y%m%d. Note, it tried to parse 20209911 to datetime(2020, 9, 9) and having 11 left tried to return it in ValueError message as a slice of integer which in turn caused IndexError. String representation of the value is now used to be able to make slice.
1 parent 8154efb commit 38a65a6

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

pandas/_libs/tslibs/strptime.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ def array_strptime(object[:] values, object fmt,
140140
iresult[i] = NPY_NAT
141141
continue
142142
raise ValueError("time data %r does not match "
143-
"format %r (match)" % (values[i], fmt))
143+
"format %r (match)" % (val, fmt))
144144
if len(val) != found.end():
145145
if is_coerce:
146146
iresult[i] = NPY_NAT
147147
continue
148148
raise ValueError("unconverted data remains: %s" %
149-
values[i][found.end():])
149+
val[found.end():])
150150

151151
# search
152152
else:
@@ -156,7 +156,7 @@ def array_strptime(object[:] values, object fmt,
156156
iresult[i] = NPY_NAT
157157
continue
158158
raise ValueError("time data %r does not match format "
159-
"%r (search)" % (values[i], fmt))
159+
"%r (search)" % (val, fmt))
160160

161161
iso_year = -1
162162
year = 1900

pandas/tests/indexes/datetimes/test_tools.py

+11
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ def test_to_datetime_format_integer(self, cache):
114114
result = to_datetime(s, format='%Y%m', cache=cache)
115115
assert_series_equal(result, expected)
116116

117+
@pytest.mark.parametrize('int_date, expected', [
118+
[20121030, datetime(2012, 10, 30)],
119+
[20129930, 20129930],
120+
[20120011, 20120011],
121+
[2012010101, 2012010101]])
122+
def test_int_to_datetime_format_YYYYMMDD_indexerror(self, int_date,
123+
expected):
124+
# GH 26583
125+
result = to_datetime(int_date, format='%Y%m%d', errors='ignore')
126+
assert result == expected
127+
117128
@pytest.mark.parametrize('cache', [True, False])
118129
def test_to_datetime_format_microsecond(self, cache):
119130

0 commit comments

Comments
 (0)