Search code examples
pythondatepython-dateutil

Python dateutil not parsing month


I am getting the following stacktrace using dateutil to parse a date:

>>> dateutil.parser.parse('Sept 18 2014', fuzzy=True).date()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File     "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.py", line     697, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File     "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.py", line     310, in parse
    ret = default.replace(**repl)
ValueError: month must be in 1..12

This works:

dateutil.parser.parse('Sep 18 2014', fuzzy=True).date()

Any idea how I can fix this, given user input similar to the above?


Solution

  • It looks like this issue was fixed here: https://bugs.launchpad.net/dateutil/+bug/876900.

    Installing the latest version of dateutil:

    % pip install -U python-dateutil
    Downloading/unpacking python-dateutil
      Downloading python-dateutil-2.2.tar.gz (259Kb): 259Kb downloaded
      Running setup.py egg_info for package python-dateutil
    ...
    

    fixes the problem:

    In [2]: import dateutil
    
    In [3]: dateutil.parser.parse('Sept 18 2014').date()
    Out[3]: datetime.date(2014, 9, 18)