Skip to content

Dateparser parses years as a date when STRICT_PARSING = True #356

Closed

Description

@wasabigeek

Dateparser with STRICT_PARSING parses a year wrongly:

>>> dateparser.parse('2017', settings={'STRICT_PARSING': True})
datetime.datetime(2020, 1, 7, 0, 0)

I would have expected it to return None instead. Did I understand the docs wrongly?

For reference, the parsing seems ok without STRICT_PARSING:

>>> dateparser.parse('2017')
datetime.datetime(2017, 10, 30, 0, 0)

Setup:

  • Python 3.5.2
  • dateparser==0.6.0

Activity

  1. lordblendi commented on Jan 30, 2019

    @lordblendi

    Any update on this?

  2. marcosmadr commented on Feb 4, 2019

    @marcosmadr

    I investigated a bit this issue and I'm sharing what I've found so far.

    Following there are two approaches to this problem:

    1. Parsing a date with or without STRICT_PARSING parameter should use the same parser, and return the same result in order to keep consistency. After parsing the date/time string, we check for STRICT_PARSING parameter and return None if it's incomplete.

    Currently, if the date is incomplete and STRICT_PARSING is set, it keeps trying to parse the data using other formats, so we end up with different results depending on if you are using STRICT_PARSING or not.

    1. Other approach would be to not parse complete dates from 4 digits data. Parsing 4 digits as complete dates is generating quite weird results, for example:

    dateparser.parse('2001', settings={'STRICT_PARSING': True})
    datetime.datetime(2000, 2, 1, 0, 0)
    MYD
    dateparser.parse('2019', settings={'STRICT_PARSING': True})
    datetime.datetime(2020, 1, 9, 0, 0)
    YMD
    dateparser.parse('1983', settings={'STRICT_PARSING': True})
    datetime.datetime(1983, 1, 9, 0, 0)
    MDY

    Hey @asadurski , Could you give your opinion, if it's the right way to go?

  3. nherger commented on Feb 12, 2019

    @nherger

    I'm having the exact same issue. Ideally, I would expect None to be returned when executing something like this:
    dateparser.parse('2011', settings={'STRICT_PARSING': True})
    Are there any updates on this?

  4. nvinchh commented on Aug 26, 2019

    @nvinchh

    Seeing this same issue. I personally think that without the date separators, an input of a number should return None if strict parsing has been set to true
    Right now, I see this

    `import sys
    import dateparser

    print(sys.version)
    print(dateparser.version)

    print("date = ", dateparser.parse('1024', settings={'STRICT_PARSING':True}))
    print("date = ", dateparser.parse('March', settings={'STRICT_PARSING':True}))
    print("date = ", dateparser.parse('1024823', settings={'STRICT_PARSING':True}))
    `
    With the output as:

    3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] 0.7.1 date = 2010-02-04 00:00:00 date = None date = 4823-10-02 00:00:00

    I think both the first and third cases should return None

  5. added this to the v1.0.0 milestone on Jun 23, 2020
  6. noviluni commented on Sep 16, 2020

    @noviluni
    Contributor

    From my perspective, the dateparser.parse() method should do the best to return dates.

    I know that 2017 doesn't seem a full date, but it theoretically fits for 2020-01-07.

    I added a new PR separating the no-spaces-time parser from the absolute-time parser: #786

    It would allow doing:

    from dateparser_data.settings import default_parsers
    
    parsers = [parser for parser in default_parsers if parser != 'no-spaces-time']
    
    dateparser.parse('2017', settings={'STRICT_PARSING': True, 'PARSERS': parsers})

    (and it wouldn't return anything).

    I know this doesn't fix the issue 100%, as you have to add specific settings, but in my opinion, we shouldn't change the default behavior, so we could consider this fixed when releasing that PR. Any strong opinion on this?

  7. noviluni commented on Sep 21, 2020

    @noviluni
    Contributor

    Hi guys, we finally decided to exclude the no-spaces-time parser from the default parsers (PR: #786), so it will work as expected in the next version!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions