【发布时间】:2019-04-30 14:41:46
【问题描述】:
我正在尝试获取每组条目(时间序列)的时间跨度。条目没有排序。在 pandas 中是否有比使用 max-min 更有效的方法?我已经尝试了 timedelta 和 period 函数,但它们没有返回我需要的东西。
import pandas as pd
tt = pd.DataFrame([
[1,'2018-3-1' ],
[1,'2018-2-3' ],
[1,'2018-5-2' ],
[1,'2018-4-5' ],
[2,'2017-3-10' ],
[2,'2017-1-12' ],
[2,'2017-5-15' ],
[2,'2017-2-14' ]
],columns=['group','entry'])
tt.entry = pd.to_datetime(tt.entry)
tt.groupby('group')['entry'].apply(lambda x: max(x)-min(x))
# group
# 1 88 days
# 2 123 days
# Name: entry, dtype: timedelta64[ns]
【问题讨论】:
标签: python pandas time-series