如何将日期查找器输出转换为列表? [英] How do I turn datefinder output into a list?

查看:115
本文介绍了如何将日期查找器输出转换为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这里已回答: 将datefinder的输出放入列表 不幸的是,我的代表人数太少,因此我无法发表评论以阐明其为何无法按预期运行.我想将文件名字符串转换为日期列表,以便随后将其用作日期列的填充.所有文件名都包含事件日期,但它们本身不在工作表上. 格式为:CompanyNameEventLocationDDMMYYYY.xlsx

So this has been answered here: making output of datefinder into list Unfortunately my rep is too low so I can't comment to get clarity on why it's not functioning as expected. I want to take filename strings and turn them into a list of dates so that I can then use them as a fill for the date column. All of the filenames include event dates, but they are not on the sheets themselves. The format is: CompanyNameEventLocationDDMMYYYY.xlsx

import glob
import datefinder
#get all Excel files within folder
path = r"C:\Users\me\Documents\Events\Spreadsheets\Consolidated\*.xlsx"
filename = glob.glob(path)
#get dates from filenames
event_dates = (datefinder.find_dates(f) for f in filename)
#check output
for days in event_dates:
    print(days.strftime("%Y-%m-%d %H:%M:%S"))

我没有得到转换的对象,而是出现了以下错误: AttributeError:

Instead of getting converted objects I get the following error: AttributeError:

'generator' object has no attribute 'strftime'

当我尝试将其直接转换为列表时

When I try converting it directly to a list with

date_list = list(event_dates)
print(event_dates)

我仍然得到输出,说它仍然是一个生成器对象:

I still get output saying that it's still a generator object:

<generator object <genexpr> at 0x00000230571AE660>

我需要更改什么,以便它实际上成为日期时间列表,可用于通过向前填充来填充附加的日期"列?

What do I need to change so that it actually becomes a list of datetimes that I can use to populate an appended 'Date' column by forward filling?

推荐答案

通过进行以下更改最终使它起作用:

Ended up getting it working by making these changes:

import pandas as pd
import glob
import datefinder
from datetime import datetime
#get all Excel files within folder
path = r"C:\Users\me\Documents\Events\Spreadsheets\Consolidated\*.xlsx"
filename = glob.glob(path)
#get dates from filenames and convert to datetime objects
event_dates = (datefinder.find_dates(f) for f in filename)
event_dates_dto = []
for dates in event_dates:
    event_dates_dto.append(pd.to_datetime(list(dates)))
#check output
print(event_dates_dto)

这现在将创建一个从每个文件名中提取的DateTimeIndexes列表,这些列表可根据需要用于我需要构造的远期日期列,用于建立索引等.

This now creates a list of DateTimeIndexes extracted from each filename which can be used as needed for the forward date column I need to construct, for indexing, etc.

这篇关于如何将日期查找器输出转换为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆