如何在read_csv中指定日期时间格式 [英] how to specify the datetime format in read_csv
本文介绍了如何在read_csv中指定日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件,其中每一行都具有以下格式:
I have a file where each row has this format:
YYYY-MM-DD-HH-MM-SS uint64 float64 float64 uint64
我读过:
pd.read_csv('file.txt', sep=' ', header=None, index_col=0, names= ('C1', 'C2', 'C3', 'C4'), use_unsigned=True, parse_dates=True, infer_datetime_format=True)
构造的日期时间不正确. 我可以指定确切的格式吗?
The datetimes constructed are not correct. Can I specify the exact format?
推荐答案
您可以将解析正确格式的函数传递给read_csv
的date_parser
kwarg,但是另一种选择是不读取时解析日期,但随后使用to_datetime
(此函数允许指定格式,并且比自定义的date_parser
函数要快):
You can pass a function that parses the correct format to the date_parser
kwarg of read_csv
, but another option is to not parse the dates when reading, but afterwards with to_datetime
(this functions allows to specify a format, and will be faster than a custom date_parser
function):
df = pd.read_csv('file.txt', sep=' ', header=None, index_col=0, names= ('C1', 'C2', 'C3', 'C4'), use_unsigned=True)
df.index = pd.to_datetime(df.index, format="%Y-%m-%d-%H-%M-%S")
这篇关于如何在read_csv中指定日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文