pandas时间序列重采样结束于指定日期
我猜很多处理时间序列数据的人都遇到过这个问题,而pandas目前似乎没有提供一个简单的解决方案(还没有!):
假设:
- 你有一组按日期(天)索引的每日收盘价的时间序列数据。
- 今天是6月19日,最后的收盘数据是6月18日。
- 你想把每日数据重新采样成OHLC(开盘、最高、最低、收盘)柱状图,按照某个给定的频率(比如M或2M),并且以6月18日为结束。
所以对于M频率,最后一个柱状图会是5月19日到6月18日,前一个柱状图是4月19日到5月18日,依此类推……
ts.resample('M', how='ohlc')
这个方法会进行重新采样,但'M'代表的是“月末”周期,所以结果会给出2014年5月的完整一个月和2014年6月的两周时间段,这样你的最后一个柱状图就不是一个“月柱”。这不是我们想要的!
使用2M
频率,根据我的样本时间序列,我的测试给出的最后一个柱状图标记为2014年7月31日(前一个标记为2014年5月31日),这非常误导,因为7月没有数据……本该是最后的2个月柱状图实际上只是覆盖了最近的两周。
正确的日期时间索引可以很容易地创建:
pandas.date_range(end='2014-06-18', freq='2M', periods=300) + datetime.timedelta(days=18)
(Pandas文档更倾向于通过
pandas.date_range(end='2014-06-18', freq='2M', periods=300) + pandas.tseries.offsets.DateOffset(days=18)
来做同样的事情,但我的测试显示这种方法虽然更“符合pandas风格”,却慢了2倍!)
无论如何,我们无法将正确的日期时间索引应用到ts.resample()中。
看起来pandas的开发团队(Pandas中的日期范围)已经意识到这个问题,但在此期间,你如何解决这个问题,以便在时间序列的最后一天上获取滚动频率的OHLC呢?
1 个回答
2
这段代码基本上是通过复制和粘贴拼凑起来的,我相信在某些情况下可能会出问题。不过,下面是一些起始代码,用于创建一个自定义的偏移量,这个偏移量是固定在一个特定的月份的某一天。
from pandas.tseries.offsets import (as_datetime, as_timestamp, apply_nat,
DateOffset, relativedelta, datetime)
class MonthAnchor(DateOffset):
"""DateOffset Anchored to day in month
Arguments:
day_anchor: day to be anchored to
"""
def __init__(self, n=1, **kwds):
super(MonthAnchor, self).__init__(n)
self.kwds = kwds
self._dayanchor = self.kwds['day_anchor']
@apply_nat
def apply(self, other):
n = self.n
if other.day > self._dayanchor and n <= 0: # then roll forward if n<=0
n += 1
elif other.day < self._dayanchor and n > 0:
n -= 1
other = as_datetime(other) + relativedelta(months=n)
other = datetime(other.year, other.month, self._dayanchor)
return as_timestamp(other)
def onOffset(self, dt):
return dt.day == self._dayanchor
_prefix = ''
使用示例:
In [28]: df = pd.DataFrame(data=np.linspace(50, 100, 200), index=pd.date_range(end='2014-06-18', periods=200), columns=['value'])
In [29]: df.head()
Out[29]:
value
2013-12-01 50.000000
2013-12-02 50.251256
2013-12-03 50.502513
2013-12-04 50.753769
2013-12-05 51.005025
In [61]: month_offset = MonthAnchor(day_anchor = df.index[-1].day + 1)
In [62]: df.resample(month_offset, how='ohlc')
Out[62]:
value
open high low close
2013-11-19 50.000000 54.271357 50.000000 54.271357
2013-12-19 54.522613 62.060302 54.522613 62.060302
2014-01-19 62.311558 69.849246 62.311558 69.849246
2014-02-19 70.100503 76.884422 70.100503 76.884422
2014-03-19 77.135678 84.673367 77.135678 84.673367
2014-04-19 84.924623 92.211055 84.924623 92.211055
2014-05-19 92.462312 100.000000 92.462312 100.000000