Skip to content

BUG/ENH: to_datetime dayfirst ought to be strict #3341

Closed
@hayd

Description

@hayd

xref #12585

I think this ought to raise a ValueErrror:

In [1]: pd.to_datetime('01-13-2012', dayfirst=True)
Out[1]: datetime.datetime(2012, 1, 13, 0, 0)

This means if my data is bad (i.e. somehow I have a mixture of american and british string-dates), I won't be told there was an issue when parsing! I can't think of a usecase where this kind of precedence would be appropriate, and it means you'll end up with incorrect dates.

The precedence behaviour appears to be a feature from dateutil (which tslib.array_to_datetime calls):

In [2]: from dateutil.parser import parse as parse_date

In [3]: parse_date('1/12/2012', dayfirst=True)
Out[3]: datetime.datetime(2012, 12, 1, 0, 0)

In [4]: parse_date('1/13/2012', dayfirst=True)
Out[4]: datetime.datetime(2012, 1, 13, 0, 0)

It should raising like this:

In [5]: parse_date('14/14/2012', dayfirst=True)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-ca14fc16e421> in <module>()
----> 1 parse_date('14/14/2012', dayfirst=True)

/Library/Python/2.7/site-packages/dateutil/parser.pyc in parse(timestr, parserinfo, **kwargs)
    718         return parser(parserinfo).parse(timestr, **kwargs)
    719     else:
--> 720         return DEFAULTPARSER.parse(timestr, **kwargs)
    721
    722

/Library/Python/2.7/site-packages/dateutil/parser.pyc in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    315             if value is not None:
    316                 repl[attr] = value
--> 317         ret = default.replace(**repl)
    318         if res.weekday is not None and not res.day:
    319             ret = ret+relativedelta.relativedelta(weekday=res.weekday)

ValueError: month must be in 1..12

... :(

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions