Skip to content

BUG: fix TypeError for invalid integer dates %Y%m%d with errors='ignore' #26585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ Datetimelike
- Bug in :func:`to_datetime` which does not replace the invalid argument with ``NaT`` when error is set to coerce (:issue:`26122`)
- Bug in adding :class:`DateOffset` with nonzero month to :class:`DatetimeIndex` would raise ``ValueError`` (:issue:`26258`)
- 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`)
- 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'``

Timedelta
^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ def array_strptime(object[:] values, object fmt,
iresult[i] = NPY_NAT
continue
raise ValueError("time data %r does not match "
"format %r (match)" % (values[i], fmt))
"format %r (match)" % (val, fmt))
Copy link
Member

@mroeschke mroeschke May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind converting these to use format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just tried to do it but a couple of tests failed. I don't know the reason yet.

if len(val) != found.end():
if is_coerce:
iresult[i] = NPY_NAT
continue
raise ValueError("unconverted data remains: %s" %
values[i][found.end():])
val[found.end():])

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

iso_year = -1
year = 1900
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ def test_to_datetime_format_integer(self, cache):
result = to_datetime(s, format='%Y%m', cache=cache)
assert_series_equal(result, expected)

@pytest.mark.parametrize('int_date, expected', [
# valid date, length == 8
[20121030, datetime(2012, 10, 30)],
# short valid date, length == 6
[199934, datetime(1999, 3, 4)],
# long integer date partially parsed to datetime(2012,1,1), length > 8
[2012010101, 2012010101],
# invalid date partially parsed to datetime(2012,9,9), length == 8
[20129930, 20129930],
# short integer date partially parsed to datetime(2012,9,9), length < 8
[2012993, 2012993],
# short invalid date, length == 4
[2121, 2121]])
def test_int_to_datetime_format_YYYYMMDD_typeerror(self, int_date,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you try with a too short as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jreback,
Thanks for feedback.
I reworded whatsnew message and added a couple of short dates.

Also I found result can be unpredictable for some short dates (either in integer or string format) with length > 4.

pd.to_datetime(12912, format="%Y%m%d", errors='ignore')
Out[4]: 12912
pd.to_datetime(21213, format="%Y%m%d", errors='ignore')
Out[5]: datetime.datetime(2, 12, 13, 0, 0)

Nevertheless it's not connected to this TypeError.
I'll add these examples to corresponding issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is weird, these should for sure fail, but maybe going down an odd path. ok can address in another issue.

Copy link
Contributor Author

@nathalier nathalier Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback I added it to #14487. It's all about how format '%Y%m%d is treated.

expected):
# GH 26583
result = to_datetime(int_date, format='%Y%m%d', errors='ignore')
assert result == expected

@pytest.mark.parametrize('cache', [True, False])
def test_to_datetime_format_microsecond(self, cache):

Expand Down