Skip to content

Commit 5693de4

Browse files
committed
Merge pull request #3671 from jreback/convert_objects
BUG: convert_objects with convert_dates=coerce was parsing `a` into a date
2 parents 9985ace + 0c81bbb commit 5693de4

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ pandas 0.11.1
156156
- Fix running of bs4 tests when it is not installed (GH3605_)
157157
- Fix parsing of html table (GH3606_)
158158
- ``read_html()`` now only allows a single backend: ``html5lib`` (GH3616_)
159+
- ``convert_objects`` with ``convert_dates='coerce'`` was parsing some single-letter strings
160+
into today's date
159161

160162
.. _GH3164: https://github.com/pydata/pandas/issues/3164
161163
.. _GH2786: https://github.com/pydata/pandas/issues/2786

pandas/tests/test_series.py

+9
Original file line numberDiff line numberDiff line change
@@ -3509,6 +3509,15 @@ def test_convert_objects(self):
35093509
#result = r.convert_objects(convert_dates=True,convert_numeric=False)
35103510
#self.assert_(result.dtype == 'M8[ns]')
35113511

3512+
# dateutil parses some single letters into today's value as a date
3513+
for x in 'abcdefghijklmnopqrstuvwxyz':
3514+
s = Series([x])
3515+
result = s.convert_objects(convert_dates='coerce')
3516+
assert_series_equal(result,s)
3517+
s = Series([x.upper()])
3518+
result = s.convert_objects(convert_dates='coerce')
3519+
assert_series_equal(result,s)
3520+
35123521
def test_apply_args(self):
35133522
s = Series(['foo,bar'])
35143523

pandas/tslib.pyx

+9
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ class Timestamp(_Timestamp):
319319

320320

321321
_nat_strings = set(['NaT','nat','NAT','nan','NaN','NAN'])
322+
_not_datelike_strings = set(['a','A','m','M','p','P','t','T'])
322323
class NaTType(_NaT):
323324
"""(N)ot-(A)-(T)ime, the time equivalent of NaN"""
324325

@@ -876,6 +877,14 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
876877
&dts)
877878
_check_dts_bounds(iresult[i], &dts)
878879
except ValueError:
880+
881+
# for some reason, dateutil parses some single letter len-1 strings into today's date
882+
if len(val) == 1 and val in _not_datelike_strings:
883+
if coerce:
884+
iresult[i] = iNaT
885+
continue
886+
elif raise_:
887+
raise
879888
try:
880889
result[i] = parse(val, dayfirst=dayfirst)
881890
except Exception:

0 commit comments

Comments
 (0)