在Pandas-Python中将许多变量更改为datetime

在Pandas-Python中将许多变量更改为datetime,python,pandas,Python,Pandas,我有一个大约有一百万行的数据集,我想把12列转换成datetime。目前它们是“对象”类型。我之前读过,我可以通过以下方式实现: 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.

我有一个大约有一百万行的数据集,我想把12列转换成datetime。目前它们是“对象”类型。我之前读过,我可以通过以下方式实现:

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吗?谢谢

如果所有日期的格式相同,则可以定义dateparse函数,然后在导入时将其作为参数传递。首先导入datetime,然后使用datetime.strf(#在此处定义格式)

一旦定义了该函数,您可以将parse dates选项设置为True,然后可以选择调用日期解析器。您可以将dateparser=yourfunction


我会查找pandas api,以获取大型数据帧的特定语法,在读取CSV、pd时使用parse_dates参数。read_CSV(parse_dates=True)工作得更好所有日期的格式都相同吗?是的,所有日期都相同。