【发布时间】:2020-04-03 15:28:58
【问题描述】:
我有以下系列:
myseries = pd.Series([' Period : From 1 February 2020 to 31 January 2021',
' Period : 1 January 2020 to 31 December 2020',
' Period 67 months',
' Period: 8 Months'])
我想将有两个日期(只有前两个)的日期时间对象转换为日期时间格式,同时保持其他日期的原始格式。
即-[('02-01-2020', '01-31-2021'), ('01-01-2020', '12-31-2020'), 'Period: 67 Months', 'Period: 8 Months']
我尝试了以下方法,但我得到了一个日期时间对象,用于那些没有正确日期的对象。
for i,v in myseries.iteritems():
matches = list(datefinder.find_dates(v))
if len(matches) > 0:
print(matches)
我已经尝试在 datefinder 的 find_dates() 中使用 staticmethod 参数,这给了我以下信息。我可以使用它,但是我无法提取我需要的对象。
[(datetime.datetime(2020, 2, 1, 0, 0), '1 February 2020'), (datetime.datetime(2021, 1, 31, 0, 0), '31 January 2021')]
[(datetime.datetime(2020, 1, 1, 0, 0), '1 January 2020'), (datetime.datetime(2020, 12, 31, 0, 0), '31 December 2020')]
[(datetime.datetime(2067, 4, 4, 0, 0), '67 mon')]
[(datetime.datetime(2020, 4, 8, 0, 0), '8 Mon')]
我想要的输出是 2 个列表:
date_1 = ['1 February 2020', '1 January 2020', '67 mon', '8 Mon']
date_2 = ['31 January 2021', '31 December 2020', '67 mon', '8 Mon']
【问题讨论】:
标签: python python-3.x pandas datetime