我有一个大约有100万行的数据集,我想将12列转换为日期时间。目前它们是“对象”类型。我之前读过,我可以这样做:
data.iloc[:,7:19] = data.iloc[:,7:19].apply(pd.to_datetime, errors='coerce')
这确实有效,但性能极差。其他人提到表现可以加快:
def lookup(s):
"""
This is an extremely fast approach to datetime parsing.
For large data, the same dates are often repeated. Rather than
re-parse these, we store all unique dates, parse them, and
use a lookup to convert all dates.
"""
dates = {date:pd.to_datetime(date) for date in s.unique()}
return s.apply(lambda v: dates[v])
但是,我不确定如何将此代码应用于我的数据(我是初学者)。有谁知道如何使用此代码或任何其他方法加快将许多列更改为datetime?谢谢!
答案 0 :(得分:0)
如果所有日期都具有相同的格式,则可以定义dateparse函数,然后在导入时将其作为参数传递。首先导入日期时间,然后使用datetime.strf(#define your format here)。
定义了该函数后,在pandas中将解析日期选项设置为True,然后您可以选择调用日期解析器。你会把日期解析器=你的功能。
我会查找pandas api以获取特定语法