pandas 如何在read_csv中指定日期时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28862956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to specify the datetime format in read_csv
提问by MMM
I have a file where each row has this format:
我有一个文件,其中每一行都有这种格式:
YYYY-MM-DD-HH-MM-SS uint64 float64 float64 uint64
I've read it with:
我读过它:
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?
构建的日期时间不正确。我可以指定确切的格式吗?
采纳答案by joris
You can pass a function that parses the correct format to the date_parserkwarg 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_parserfunction):
您可以将解析正确格式的函数传递给date_parserkwarg read_csv,但另一种选择是在读取时不解析日期,但之后使用to_datetime(此函数允许指定格式,并且比自定义date_parser函数更快):
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")
回答by MMM
I have found this method.
我找到了这个方法。
f = lambda s: datetime.datetime.strptime(s,'%Y-%m-%d-%H-%M-%S')
pd.read_csv('file.txt', sep=' ', header=None, index_col=0, names= ('C1', 'C2', 'C3', 'C4'), use_unsigned=True, date_parser=f)
that worked
那有效

