代码之家  ›  专栏  ›  技术社区  ›  spitfiredd

熊猫重采样从最近的年终日期偏移?

  •  0
  • spitfiredd  · 技术社区  · 6 年前

    例如,

       In [53]: daily_3mo_treasury.resample('5Y').mean()
        Out[53]:
        1993-12-31    2.997120
        1998-12-31    4.917730
        2003-12-31    3.297176
        2008-12-31    2.997204
        2013-12-31    0.097330
        2018-12-31    0.534476
    

    我时间序列中的最后一个日期是 2018-08-23 2.04

    2017-12-31 2012-12-31

    end = daily_3mo_treasury.index.searchsorted(date(2017,12,31))
    daily_3mo_treasury.iloc[:end].resample('5Y').mean()
    
    In [66]: daily_3mo_treasury.iloc[:end].resample('5Y').mean()
    Out[66]:
    1993-12-31    2.997120
    1998-12-31    4.917730
    2003-12-31    3.297176
    2008-12-31    2.997204
    2013-12-31    0.097330
    2018-12-31    0.333467
    dtype: float64
    

    其中最后一个值 daily_3mo_treasury.iloc[:end] 2017-12-29 1.37

    为什么我的第二个5年重采样没有结束 2017年12月31日 ?

    1 回复  |  直到 6 年前
        1
  •  0
  •   spitfiredd    6 年前

    From@ALollz-重新采样时,存储箱基于索引中的第一个日期。

    sistart = daily_3mo_treasury.index.searchsorted(date(1992,12,31))
    siend = daily_3mo_treasury.index.searchsorted(date(2017,12,31))
    
    In [95]: daily_3mo_treasury.iloc[sistart:siend].resample('5Y').mean()
    Out[95]:
    1992-12-31    3.080000
    1997-12-31    4.562246
    2002-12-31    4.050696
    2007-12-31    2.925971
    2012-12-31    0.360775
    2017-12-31    0.278233
    dtype: float64