0
我有一个大约100万行的数据集,我想将12列转换为日期时间。目前他们是“对象”类型。我以前读过,我可以这样做:在熊猫中更改日期时间的很多变量 - Python
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])
但是,我不知道如何将此代码应用到我的数据(我是初学者)。有谁知道如何加快使用此代码或任何其他方法更改许多列日期时间?谢谢!
对于大型dataframes,使用parse_dates参数而读CSV,pd.read_csv(parse_dates = TRUE)效果更好 – Vaishali
贵公司的所有日期的格式都相同? – MaxU
是的,所有的日期都是一样的。 – user3910919