Home > Software engineering >  dateparser throws regex._regex_core.error
dateparser throws regex._regex_core.error

Time:03-16

I am trying to run a very basic script:

import dateparser

dateparser.parse('12/12/12')

but I keep getting the same error

Traceback (most recent call last):
  File ".../app/test.py", line 3, in <module>
    dateparser.parse('12/12/12')
  File ".../lib/python3.9/site-packages/dateparser/conf.py", line 92, in wrapper
    return f(*args, **kwargs)
  File ".../lib/python3.9/site-packages/dateparser/__init__.py", line 61, in parse
    data = parser.get_date_data(date_string, date_formats)
  File ".../lib/python3.9/site-packages/dateparser/date.py", line 428, in get_date_data
    parsed_date = _DateLocaleParser.parse(
  File ".../lib/python3.9/site-packages/dateparser/date.py", line 178, in parse
    return instance._parse()
  File ".../lib/python3.9/site-packages/dateparser/date.py", line 182, in _parse
    date_data = self._parsers[parser_name]()
  File ".../lib/python3.9/site-packages/dateparser/date.py", line 196, in _try_freshness_parser
    return freshness_date_parser.get_date_data(self._get_translated_date(), self._settings)
  File ".../lib/python3.9/site-packages/dateparser/date.py", line 234, in _get_translated_date
    self._translated_date = self.locale.translate(
  File ".../lib/python3.9/site-packages/dateparser/languages/locale.py", line 131, in translate
    relative_translations = self._get_relative_translations(settings=settings)
  File ".../lib/python3.9/site-packages/dateparser/languages/locale.py", line 158, in _get_relative_translations
    self._generate_relative_translations(normalize=True))
  File ".../lib/python3.9/site-packages/dateparser/languages/locale.py", line 172, in _generate_relative_translations
    pattern = DIGIT_GROUP_PATTERN.sub(r'?P<n>\d ', pattern)
  File ".../lib/python3.9/site-packages/regex/regex.py", line 700, in _compile_replacement_helper
    is_group, items = _compile_replacement(source, pattern, is_unicode)
  File ".../lib/python3.9/site-packages/regex/_regex_core.py", line 1736, in _compile_replacement
    raise error("bad escape \\%s" % ch, source.string, source.pos)
regex._regex_core.error: bad escape \d at position 7

Could it be a compatibility version between dateparser and python? I am using dateparser 1.1 and python 3.9 and the documentation of dateparsers says that it accepts this python versoin.

CodePudding user response:

I don't know how I would get by without dateutil!

Try this:

python -m pip install python-dateutil

or conda-forge or whatever (it's not in the standard libarary), and then:

>>> from dateutil.parser import parse as date_parse
>>> date_parse('12/12/12')
datetime.datetime(2012, 12, 12, 0, 0)

If you have pandas, dateutil is already installed as a dependency.

CodePudding user response:

Since you are providing an awkward format I would consider setting the formatting for it

dateparser.parse('12/12/1212', date_formats=['%m/%d/%Y'])
  • Related