【问题标题】:parse_dates in all the columns (unspecified number) when using pd.read_csv使用 pd.read_csv 时所有列中的 parse_dates(未指定数字)
【发布时间】:2023-02-11 04:19:50
【问题描述】:
我正在从包含多级索引和许多带日期的列的 csv 文件中读取数据。列数会随时间变化。如果我不知道列数或列名,如何解析所有这些日期?
pd.read_csv('file.csv', header=0, parse_dates=[what_do_I_put_here?], dayfirst=True, index_col=[0,1,2,3])
你会怎么解决?如果我不解析日期,它们将作为“对象”出现,而不是我想要的 datetime64。
【问题讨论】:
标签:
pandas
csv
python-datetime
【解决方案1】:
我建议逐行阅读文件,直到读完为止。在每一行更改中,您都会更改一个计数器。
尝试这样的事情(它是用 C 而不是 python 编写的。):
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '
') // Increment count if this character is newline
count++;