我在pandas Dataframe中有一个时间序列,如下所示:
time A B
2012-06-11 09:25:00.005001 2572.4 2.589
2012-06-11 09:30:00.005004 2573.2 2.592
2012-06-11 09:31:00.005000 2572.6 2.592
2012-06-11 09:32:00.004996 2572.2 2.591
2012-06-11 09:33:00.005003 2570.0 2.589
2012-06-18 09:34:00.004999 2571.2 2.590
2012-06-18 09:35:00.004996 2572.0 2.591
2012-06-18 09:36:00.005002 2572.2 2.590
有没有办法可以快速计算一个月下一个第三个星期五之前的天数?在这种情况下,它看起来像:
[4,4,4,4,4,32,32,32]
答案 0 :(得分:1)
您可以将Series.apply
函数与WeekOfMonth
offset class及其前滚方法一起使用,将日期向前或向后移动到下一个或上一个“偏移日期”。
from pandas.tseries.offsets import WeekOfMonth
wom = WeekOfMonth(week=2, weekday=4)
df['days'] = df['d'].apply(lambda x: (wom.rollforward(x) - x).days)
答案 1 :(得分:0)
第3个星期五? you mean when options expire?:)
不确定是否已有内置anchored offset,但可以解决这个问题:
>>> offset = dt.timedelta(days=60)
>>> first = df['time'].min().date() - offset
>>> last = df['time'].max().date() + offset
>>> ts = pd.date_range(first, last, freq='W-Fri').to_series()
>>> a = ts.groupby(ts.dt.month).nth(2).values
>>> i = np.searchsorted(a, df['time'], side='right')
>>> a[i] - df['time']
0 3 days 14:34:59.994999
1 3 days 14:29:59.994996
2 3 days 14:28:59.995000
3 3 days 14:27:59.995004
4 3 days 14:26:59.994997
5 31 days 14:25:59.995001
6 31 days 14:24:59.995004
7 31 days 14:23:59.994998
Name: time, dtype: timedelta64[ns]
修改最后3行可能会得到天数:
>>> a = ts.groupby(ts.dt.month).nth(2).dt.date.values
>>> i = np.searchsorted(a, df['time'].dt.date, side='right')
>>> a[i] - df['time'].dt.date
0 4 days
1 4 days
2 4 days
3 4 days
4 4 days
5 32 days
6 32 days
7 32 days
dtype: timedelta64[ns]
答案 2 :(得分:0)
使用rollforward
偏移类的WeekofMonth
方法:
from pandas.tseries.offsets import *
df['days'] = [(WeekOfMonth(week=2, weekday=4).rollforward(d)-d).days
for d in df['time']]