我想像这样计算经过的天数:
df["elapsed_days"] = df.apply(lambda x: (x.logged_day - x.registered_day).days, axis=1)
logged_day和registered_day的类型是datetime.date()。 计算天数(可能)需要很长时间 - 每30,0000条记录需要40秒。 我还能做些什么来加快速度吗?
谢谢。
答案 0 :(得分:1)
pandas documentation让你满意:
In [252]: s - s.max()
Out[252]:
0 -2 days
1 -1 days
2 0 days
dtype: timedelta64[ns]
In [253]: s - datetime(2011,1,1,3,5)
Out[253]:
0 364 days, 20:55:00
1 365 days, 20:55:00
2 366 days, 20:55:00
dtype: timedelta64[ns]
In [254]: s + timedelta(minutes=5)
Out[254]:
0 2012-01-01 00:05:00
1 2012-01-02 00:05:00
2 2012-01-03 00:05:00
dtype: datetime64[ns]
In [255]: s + Minute(5)
Out[255]:
0 2012-01-01 00:05:00
1 2012-01-02 00:05:00
2 2012-01-03 00:05:00
dtype: datetime64[ns]
In [256]: s + Minute(5) + Milli(5)
Out[256]:
0 2012-01-01 00:05:00.005000
1 2012-01-02 00:05:00.005000
2 2012-01-03 00:05:00.005000
dtype: datetime64[ns]