简体   繁体   中英

Timex strings to datetime in python

I want to convert a list of Timex date format strings (from SUTime) into normal datetime format. The problem is, that I have numerous different types:

dates = ['2018-07-09', 
         '2018-W15', 
         '2018-02', 
         '2018-04-06',
         '2018-W15',
         '2018-02',
         '2015-09',
         '2018-09-27 INTERSECT P5D',
         'FUTURE_REF',
         'FUTURE_REF',
         'PXY',
         'THIS P1D INTERSECT 2018-09-28',
         {'end': 'XXXX-06', 'begin': 'XXXX-04'}, 
         '2014-03-19',
         '2018-08-02']

I have two objectives:

  1. Ignore all entries not directly indicating a certain date
  2. Convert all other to a 'yyyy-mm-dd' format, citing always the first day of the year, month, week, etc. Eg: '2018-02' should become '2018-02-01' or '2018-W15' 2018-04-09

I tried with pandas' pd.to_datetime function, but this does not convert weeks to dates

It's a bit of a challenge when the data collection isn't uniform. I am unfamiliar with Timex and was unable to find any packages that might help.

This might help you out. I wrote some functions that handle each particular case.

import datetime
from pprint import pprint

dates = ['2018-07-09',
         '2018-W15',
         '2018-02',
         '2018-04-06',
         '2018-W15',
         '2018-02',
         '2015-09',
         '2018-09-27 INTERSECT P5D',
         'FUTURE_REF',
         'FUTURE_REF',
         'PXY',
         'THIS P1D INTERSECT 2018-09-28',
         {'end': 'XXXX-06', 'begin': 'XXXX-04'},
         '2014-03-19',
         '2018-08-02']

FORMAT = '%Y-%m-%d'
def get_simple_date(item, strformat=FORMAT):
    try:
        return (True, datetime.datetime.strptime(item, strformat))
    except (ValueError, TypeError):
        return (False, item)

def get_from_split(is_resolved, item):
    if is_resolved:
        return (is_resolved, item)
    try:
        tokens = item.split(' ')
        are_resolved, items = zip(*(get_simple_date(token) for token in tokens))
        if any(are_resolved):
            # assume one valid token
            result, = (item for item in items if isinstance(item, datetime.datetime))
            return (True, result)
    except (ValueError, AttributeError):
        pass
    return (False, item)

def get_from_no_day(is_resolved, item):
    if is_resolved:
        return (is_resolved, item)
    if not 'W' in item:
        try:
            return (True, datetime.datetime.strptime(f'{item}-01', FORMAT))
        except ValueError:
            pass
    return (False, item)

def get_from_w_date(is_resolved, item):
    if is_resolved:
        return (is_resolved, item)
    if 'W' in item:
        return (True, datetime.datetime.strptime(f'{item}-1', "%Y-W%W-%w"))
    return (is_resolved, item)

collection1 = (get_simple_date(item) for item in dates)
collection2 = (get_from_split(*args) for args in collection1)
collection3 = (get_from_no_day(*args) for args in collection2)
collection4 = (get_from_w_date(*args) for args in collection3)
pprint([d for is_resolved, d in collection4 if is_resolved], indent=4)

OUTPUT:

[   datetime.datetime(2018, 7, 9, 0, 0),
    datetime.datetime(2018, 4, 9, 0, 0),
    datetime.datetime(2018, 2, 1, 0, 0),
    datetime.datetime(2018, 4, 6, 0, 0),
    datetime.datetime(2018, 4, 9, 0, 0),
    datetime.datetime(2018, 2, 1, 0, 0),
    datetime.datetime(2015, 9, 1, 0, 0),
    datetime.datetime(2018, 9, 27, 0, 0),
    datetime.datetime(2018, 9, 28, 0, 0),
    datetime.datetime(2014, 3, 19, 0, 0),
    datetime.datetime(2018, 8, 2, 0, 0)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM