大 pandas 可以自动从 CSV 文件中读取日期吗? [英] Can pandas automatically read dates from a CSV file?
问题描述
今天,当从数据文件(例如)读取数据时,pandas 能够识别值的类型这一事实让我感到非常惊讶:
Today I was positively surprised by the fact that while reading data from a data file (for example) pandas is able to recognize types of values:
df = pandas.read_csv('test.dat', delimiter=r"s+", names=['col1','col2','col3'])
比如可以这样检查:
for i, r in df.iterrows():
print type(r['col1']), type(r['col2']), type(r['col3'])
特别是整数、浮点数和字符串被正确识别.但是,我有一列日期格式如下:2013-6-4
.这些日期被识别为字符串(而不是 python 日期对象).有没有办法让熊猫学习"识别日期?
In particular integer, floats and strings were recognized correctly. However, I have a column that has dates in the following format: 2013-6-4
. These dates were recognized as strings (not as python date-objects). Is there a way to "learn" pandas to recognized dates?
推荐答案
阅读时应添加parse_dates=True
,或parse_dates=['column name']
,这通常足以神奇地解析它.但总有一些奇怪的格式需要手动定义.在这种情况下,您还可以添加日期解析器功能,这是最灵活的方式.
You should add parse_dates=True
, or parse_dates=['column name']
when reading, thats usually enough to magically parse it. But there are always weird formats which need to be defined manually. In such a case you can also add a date parser function, which is the most flexible way possible.
假设您有一个包含字符串的datetime"列,那么:
Suppose you have a column 'datetime' with your string, then:
from datetime import datetime
dateparse = lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
df = pd.read_csv(infile, parse_dates=['datetime'], date_parser=dateparse)
通过这种方式,您甚至可以将多列合并为一个日期时间列,从而将日期"和时间"列合并为一个日期时间"列:
This way you can even combine multiple columns into a single datetime column, this merges a 'date' and a 'time' column into a single 'datetime' column:
dateparse = lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
df = pd.read_csv(infile, parse_dates={'datetime': ['date', 'time']}, date_parser=dateparse)
您可以找到 strptime
和 strftime
的指令(即用于不同格式的字母)在本页.
You can find directives (i.e. the letters to be used for different formats) for strptime
and strftime
in this page.
这篇关于大 pandas 可以自动从 CSV 文件中读取日期吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!