4

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user3714205
  • 83
  • 1
  • 1
  • 8

3 Answers3

9

Our team just experienced this exact issue which took down our production servers. Last night, one of the dependencies of dateparser, regex was updated to version 2022.3.15. Dateparser installs regex, but without a frozen version. It seems that the newest version of regex does not like something that dateparser is doing, leading to the error you saw. By pip installing regex==2022.3.2 before dateparser, this completely fixed our issue.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Lukas Schmit
  • 1,118
  • 2
  • 9
  • 14
  • Do you have any other comments about this? I'm having an issue where I'm able to use a module that relies on dateparser/regex in the terminal but when I try to use it in Anaconda, I keep getting the error. I tried moving the related folders between site-packages directories and still get the error. And of course I tried your answer of installing `regex==2022.3.2` before installing dateparser. – SuperCodeBrah May 10 '22 at 04:15
  • 1
    Turned out to be more my lack of knowledge with Anaconda. Just needed to update all conda packages and then get the correct version (still `<2022-3.15`) also through conda. Thanks for pointing me in the right direction. – SuperCodeBrah May 10 '22 at 13:26
  • Happened same with me, app was working on my local dev machine but broke in production due to docker image being updated while I fix other bug. I updated to next version of date-parser and it is working fine now. – PankajKushwaha May 25 '22 at 05:08
2

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.

molsonkiko
  • 31
  • 5
  • I up-voted this answer because dateparser causes problems in zipapp applications, throwing a regex error – Psionman Aug 04 '23 at 12:25
0

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'])
Robert Lee
  • 1,541
  • 1
  • 10
  • 20