Skip to content

How to know when a date is incomplete and the missing parts #700

Closed

Description

@marcelooyaneder

Is there a way to know when a date is incomplete. e.g "December 2015"

from dateparser import parse
parse(u'December 2015')  # default behavior
datetime.datetime(2015, 12, 16, 0, 0)
#is a way to do something like this
parse(u'December 2015', settings={'PREFER_DAY_OF_MONTH': 'incomplete'}) 
datetime.datetime(2015, 12, incomplete, 0, 0)
#the same with year or month

I need to parse data wich contains incomplete dates, in a nutshell i need the following to be done

from dateparser import parse
parse(u'December 2015', settings={'PREFER_DAY_OF_MONTH': 'incomplete'})  #it could be a string or other type
datetime.datetime(2015, 12, incomplete, 0, 0)

i´m sorry about my english, im self taught in it...

Activity

  1. Gallaecio commented on Jun 1, 2020

    @Gallaecio
    Contributor

    I think this is a duplicate of #553

  2. noviluni commented on Jun 26, 2020

    @noviluni
    Contributor

    Just added a PR trying to find a solution for this: #729

    Please, if you have any feedback let me know.

  3. marcelooyaneder commented on Jun 26, 2020

    @marcelooyaneder
    Author

    I've actually develop a little solution for that... on my testing is consistent, the code is the following.

    imported_list=[]
    for elements in df.verbatimEventDate:
        try:
            if math.isnan(elements):
                imported_list.append([None,None,None,None,None,"Empty"])
            else:
                date_element = dateparser.parse(str(elements), settings={'PREFER_DATES_FROM': 'past', 'STRICT_PARSING': True},languages=['pt', 'es'])
                imported_list.append([elements,date_element.strftime('%F'),date_element.strftime("%Y"),date_element.strftime("X%m").replace('X0','X').replace('X',''),date_element.strftime("X%d").replace('X0','X').replace('X',''),"High"])
        except TypeError: 
            date_element = dateparser.parse(elements, settings={'PREFER_DATES_FROM': 'past', 'STRICT_PARSING': True},languages=['pt', 'es'])
            if date_element is None: 
                date_element = dateparser.parse(elements, settings={'PREFER_DATES_FROM': 'past','DATE_ORDER': 'MYD'},languages=['pt', 'es'])
                imported_list.append([elements,date_element.strftime('%Y-%m'),date_element.strftime("%Y"),date_element.strftime("X%m").replace('X0','X').replace('X',''),None,"Medium"]) #just windows
            else: 
                imported_list.append([elements,date_element.strftime('%F'),date_element.strftime("%Y"),date_element.strftime("X%m").replace('X0','X').replace('X',''),date_element.strftime("X%d").replace('X0','X').replace('X',''),"Low"])
    

    in case that you need more feedback, just write me

  4. noviluni commented on Nov 26, 2020

    @noviluni
    Contributor

    Hi!

    This can be partially achived by using DateDataParser() and get_date_data() instead of using directly dateparser.parse().

    >>> from dateparser.date import DateDataParser
    >>> ddp = DateDataParser()
    >>> ddp.get_date_data('December 2015') 
    DateData(date_obj=datetime.datetime(2015, 12, 26, 0, 0), period='month', locale='en')

    The "period" indicates the smaller part (in this example "Month").

    However, if the missing part is the year, for example, we won't be able to know it, so we should probably implement something new. I personally think that the best would be removing the "period" in a next major release (as it gives little information and if you want to add the "time", for example, you have to use a specific setting) and adding something like "found_parts" or something similar, indicating which "parts" were found, but this is something that we should decide in a calm and consensual way :P

  5. changed the title [-]Question: is there a way to know when a date is incomplete.[/-] [+]How to know when a date is incomplete and the missing parts[/+] on Nov 26, 2020
  6. awiebe commented on May 29, 2023

    @awiebe

    I would like period left in, but found parts should probably be added.
    In my use case I am jamming a historical dates into a date field in a database, and using a second column to store the decision. I think a better name for period would be precision and it should have some constants

    e.g.

    from dateparser.date import DateDataParser,Precision
    ddp = DateDataParser()
    res=ddp.get_date_data('December 2015')
    p : Precision = res.precision
    if p == Precision.YEAR:
     #...
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions