Timex字符串到Python中的日期时间

时间:2019-03-08 15:15:02

标签: python datetime timex

我想将Timex日期格式字符串列表(来自SUTime)转换为正常的datetime格式。问题是,我有很多不同的类型:

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']

我有两个目标:

  1. 忽略所有不直接表示特定日期的条目
  2. 将其他所有格式都转换为'yyyy-mm-dd'格式,并总是引用年,月,日等的第一天。例如:'2018-02'应该变成'2018-02-01''2018-W15' 2018-04-09

我尝试使用熊猫的pd.to_datetime函数,但这不能将星期转换为日期

1 个答案:

答案 0 :(得分:1)

当数据收集不统一时,这是一个挑战。我不熟悉Timex,无法找到任何可能有帮助的软件包。

这可能会帮助您。我写了一些处理每种特殊情况的函数。

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)

输出:

[   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)]