大熊猫时间序列重新采样结束一天

com*_*mte 7 python time-series pandas

我怀疑许多研究时间序列数据的人已经遇到过这个问题,而且大熊猫似乎没有提供直接的解决方案(还有!):

假设:

  1. 您有一个时间序列的每日数据与关闭价格,按日期(日)索引.
  2. 今天是19JUN.最后收盘数据值为18JUN.
  3. 您希望将每日数据重新采样到OHLC条形图中,并以某个给定频率(假设M或2M)结束18JUN.

所以对于M freq,最后一个是19MAY-18JUN,前一个是19APR-18MAY,依此类推......

ts.resample('M', how='ohlc')
Run Code Online (Sandbox Code Playgroud)

将进行重新取样,但"M"为'end_of_month'期间,因此结果将给出2014-05的整月和2014-06的2周期间,因此您的最后一个栏将不是'月度栏' .那不是我们想要的!

有了2M频率,根据我的样本时间序列,我的测试给了我标记为2014-07-31的最后一个条(以前标记为2014-05-31),这是非常误导,因为没有关于JUL的数据....假设的最后2个月的酒吧再次覆盖了最近的2周.

使用以下命令可以轻松创建正确的DatetimeIndex:

pandas.date_range(end='2014-06-18', freq='2M', periods=300) + datetime.timedelta(days=18)
Run Code Online (Sandbox Code Playgroud)

(Pandas文档更喜欢通过做同样的事情

pandas.date_range(end='2014-06-18', freq='2M', periods=300) + pandas.tseries.offsets.DateOffset(days=18)
Run Code Online (Sandbox Code Playgroud)

但是我的测试显示这种方法,虽然更多'pandaïc'慢了2倍!)

无论哪种方式,我们都无法将正确的DatetimeIndex应用于ts.resample().

似乎pandas开发团队(Pandas中的日期范围)意识到了这个问题,但与此同时,你怎么能解决这个问题,让OHLC的滚动频率在时间序列的最后一天得到锚定?

chr*_*isb 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 = ''
Run Code Online (Sandbox Code Playgroud)

用法示例:

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
Run Code Online (Sandbox Code Playgroud)