在 Pandas DF 中使用 datetime timedelta 和系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35327450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using datetime timedelta with a series in a pandas DF
提问by Luis Miguel
I have a pandas sim_df that looks like this:
我有一个看起来像这样的Pandas sim_df:
Now, I want to add another column, "date" that is the date corresponding to 'now' plus 'cum_days' (a delta time).
现在,我想添加另一列“日期”,它是对应于“现在”加上“cum_days”(增量时间)的日期。
start = dt.datetime.now()
sim_df['date'] = start + dt.timedelta(sim_df['cum_days'])
But it looks like deltatime does not use a series, but a fixed scalar.
但看起来 deltatime 不使用系列,而是使用固定标量。
TypeError: unsupported type for timedelta days component: Series
Is there a way to solve this in a vectorized operation without iterating over each row of sim_df?
有没有办法在不迭代 sim_df 的每一行的情况下在矢量化操作中解决这个问题?
采纳答案by Kris
How about this?
这个怎么样?
start = dt.datetime.now()
sim_df['date'] = start + sim_df['cum_days'].map(dt.timedelta)
This applies dt.timedelta
to each element of the cum_days
column individually.
这分别适用dt.timedelta
于cum_days
列的每个元素。
回答by Alexander
Add the timedelta to now using a list comprehension.
使用列表理解将 timedelta 添加到 now。
sim_df = pd.DataFrame({'delta_time_days': [1.02, .09, 1.08, 1.7, 4.1, 0.3, .13, .01, .3, .7],
'cum_days': [1.1, 1.1, 2.2, 3.9, 8.0, 8.3, 8.4, 8.4, 8.8, 9.5]})
sim_df['date'] = [dt.datetime.now() + dt.timedelta(days=d) for d in sim_df.cum_days]
>>> sim_df
cum_days delta_time_days date
0 1.1 1.02 2016-02-11 17:36:11.320271
1 1.1 0.09 2016-02-11 17:36:11.320286
2 2.2 1.08 2016-02-12 20:00:11.320289
3 3.9 1.70 2016-02-14 12:48:11.320292
4 8.0 4.10 2016-02-18 15:12:11.320296
5 8.3 0.30 2016-02-18 22:24:11.320299
6 8.4 0.13 2016-02-19 00:48:11.320301
7 8.4 0.01 2016-02-19 00:48:11.320304
8 8.8 0.30 2016-02-19 10:24:11.320306
9 9.5 0.70 2016-02-20 03:12:11.320309
回答by EdChum
construct a TimedeltaIndex
from your column and add this to the scalar value:
TimedeltaIndex
从您的列中构造一个并将其添加到标量值中:
In [26]:
sim_df = pd.DataFrame({'delta_time_days': [1.02, .09, 1.08, 1.7, 4.1, 0.3, .13, .01, .3, .7],
'cum_days': [1.1, 1.1, 2.2, 3.9, 8.0, 8.3, 8.4, 8.4, 8.8, 9.5]})
start = dt.datetime.now()
sim_df['date'] = start + pd.TimedeltaIndex(sim_df['cum_days'], unit='D')
sim_df
Out[26]:
cum_days delta_time_days date
0 1.1 1.02 2016-02-12 01:40:32.413413
1 1.1 0.09 2016-02-12 01:40:32.413413
2 2.2 1.08 2016-02-13 04:04:32.413413
3 3.9 1.70 2016-02-14 20:52:32.413413
4 8.0 4.10 2016-02-18 23:16:32.413413
5 8.3 0.30 2016-02-19 06:28:32.413413
6 8.4 0.13 2016-02-19 08:52:32.413413
7 8.4 0.01 2016-02-19 08:52:32.413413
8 8.8 0.30 2016-02-19 18:28:32.413413
9 9.5 0.70 2016-02-20 11:16:32.413413