Skip to content

DATE_ORDER not always honored. #804

Open

Description

@ArthurKantor

Thanks for an excellent library!

Version: 0.7.6

Steps to reproduce:

import dateparser as dp
p1=dp.DateDataParser(settings={'DATE_ORDER': 'MDY','PREFER_LANGUAGE_DATE_ORDER':False})
p2=dp.DateDataParser(settings={'DATE_ORDER': 'DMY','PREFER_LANGUAGE_DATE_ORDER':False})

#good behavior
p1.get_date_data('02-23-2004')
{'date_obj': datetime.datetime(2004, 2, 23, 0, 0),
 'period': 'day',
 'locale': 'en'}

p2.get_date_data('02-23-2004')
{'date_obj': None, 'period': 'day', 'locale': None}

#bad behavior
p1.get_date_data('02-23-04')
{'date_obj': datetime.datetime(2004, 2, 23, 0, 0),
 'period': 'day',
 'locale': 'en'}

p2.get_date_data('02-23-04')
{'date_obj': datetime.datetime(2023, 4, 2, 0, 0),
 'period': 'day',
 'locale': 'en'}

My understanding is that with settings={'DATE_ORDER': 'DMY','PREFER_LANGUAGE_DATE_ORDER':False}, the parser should return None if it cannot parse with the specified DATE_ORDER.

The reason I need the parser to honor the DATE_ORDER (or fail the parse otherwise) is I have documents where the parse format is unknown, but it is known that all the dates in that document have the same parse format. If the DATE_ORDER was honored, I could parse all the dates with different DATE_ORDERs, and pick the DATE_ORDER which is consistent with all the dates in the document.

Activity

  1. Gallaecio commented on Oct 5, 2020

    @Gallaecio
    Contributor

    Could you also check with master? The issue probably remains, but @noviluni has made lots of changes everywhere, so there’s a chance it has been fixed.

  2. ArthurKantor commented on Oct 5, 2020

    @ArthurKantor
    Author

    Still happens with master as of now.

  3. noviluni commented on Oct 6, 2020

    @noviluni
    Contributor

    Hi @ArthurKantor Thank you for raising this.

    First of all, I would like to mention that PREFER_LANGUAGE_DATE_ORDER it's not doing anything, as it's wrong, it should be PREFER_LOCALE_DATE_ORDER. There is an error in the documentation that will be fixed once we merge this: https://github.com/scrapinghub/dateparser/pull/722/files. The new version will be released in a few days, and those wrong settings will raise an error, so this kind of error won't happen again in the future. I apologize for this confusion.

    However, even if we change this setting, it fails, and I think it's because the absolute-parser is not using this setting properly.

    After releasing the next version (1.0.0) we will check all the settings with all the parsers to see that all are respecting them (see the last point here: #796). But if I have the chance I will try to fix this before.

  4. added this to the 1.1.0 milestone on Oct 6, 2020
  5. alex-triphonov commented on Oct 26, 2020

    @alex-triphonov

    @noviluni
    I found cause of this issue:
    dateparser/date.py

        def _try_parser(self):
            _order = self._settings.DATE_ORDER
            try:
                if self._settings.PREFER_LOCALE_DATE_ORDER:
                    if self._settings._default:        <- only default..
                        self._settings.DATE_ORDER = self.locale.info.get('date_order', _order)
                date_obj, period = date_parser.parse(
                    self._get_translated_date(), settings=self._settings)
                self._settings.DATE_ORDER = _order
                return {
                    'date_obj': date_obj,
                    'period': period,
                }
            except ValueError:
                self._settings.DATE_ORDER = _order
                return None
    

    Date order from locales only works with _default settings. Hope this would be fixed soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions