Skip to content

Commit 1d7ad5f

Browse files
nathalierjorisvandenbossche
authored andcommitted
BUG: fix TypeError for invalid integer dates %Y%m%d with errors='ignore' (# GH 26583) (#26585)
1 parent 047d32d commit 1d7ad5f

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ Datetimelike
537537
- Bug in :func:`to_datetime` which does not replace the invalid argument with ``NaT`` when error is set to coerce (:issue:`26122`)
538538
- Bug in adding :class:`DateOffset` with nonzero month to :class:`DatetimeIndex` would raise ``ValueError`` (:issue:`26258`)
539539
- Bug in :func:`to_datetime` which raises unhandled ``OverflowError`` when called with mix of invalid dates and ``NaN`` values with ``format='%Y%m%d'`` and ``error='coerce'`` (:issue:`25512`)
540+
- Bug in :func:`to_datetime` which raises ``TypeError`` for ``format='%Y%m%d'`` when called for invalid integer dates with length >= 6 digits with ``errors='ignore'``
540541

541542
Timedelta
542543
^^^^^^^^^

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

+19
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@ def test_to_datetime_format_integer(self, cache):
133133
result = to_datetime(s, format='%Y%m', cache=cache)
134134
assert_series_equal(result, expected)
135135

136+
@pytest.mark.parametrize('int_date, expected', [
137+
# valid date, length == 8
138+
[20121030, datetime(2012, 10, 30)],
139+
# short valid date, length == 6
140+
[199934, datetime(1999, 3, 4)],
141+
# long integer date partially parsed to datetime(2012,1,1), length > 8
142+
[2012010101, 2012010101],
143+
# invalid date partially parsed to datetime(2012,9,9), length == 8
144+
[20129930, 20129930],
145+
# short integer date partially parsed to datetime(2012,9,9), length < 8
146+
[2012993, 2012993],
147+
# short invalid date, length == 4
148+
[2121, 2121]])
149+
def test_int_to_datetime_format_YYYYMMDD_typeerror(self, int_date,
150+
expected):
151+
# GH 26583
152+
result = to_datetime(int_date, format='%Y%m%d', errors='ignore')
153+
assert result == expected
154+
136155
@pytest.mark.parametrize('cache', [True, False])
137156
def test_to_datetime_format_microsecond(self, cache):
138157

0 commit comments

Comments
 (0)