【问题标题】:Problems resampling pandas time series with time gap between indices使用索引之间的时间间隔重新采样 pandas 时间序列的问题
【发布时间】:2016-10-09 18:27:56
【问题描述】:

我想重新采样熊猫系列

import pandas as pd
index_1 = pd.date_range('1/1/2000', periods=4, freq='T')
index_2 = pd.date_range('1/2/2000', periods=3, freq='T')
series = pd.Series(range(4), index=index_1)
series=series.append(pd.Series(range(3), index=index_2))
print series
>>>2000-01-01 00:00:00    0
   2000-01-01 00:01:00    1
   2000-01-01 00:02:00    2
   2000-01-01 00:03:00    3
   2000-01-02 00:00:00    0
   2000-01-02 00:01:00    1
   2000-01-02 00:02:00    2

使得生成的 DataSeries 仅包含每隔一个条目,即

  >>>2000-01-01 00:00:00    0
     2000-01-01 00:02:00    2
     2000-01-02 00:00:00    0
     2000-01-02 00:02:00    2

以下列方式使用 pandas 的(记录不充分的)重采样方法:

resampled_series = series.resample('2T', closed='right')
print resampled_series

我明白了

>>>1999-12-31 23:58:00    0.0
   2000-01-01 00:00:00    1.5
   2000-01-01 00:02:00    3.0
   2000-01-01 00:04:00    NaN
   2000-01-01 00:56:00    NaN
                  ...
   2000-01-01 23:54:00    NaN
   2000-01-01 23:56:00    NaN
   2000-01-01 23:58:00    0.0
   2000-01-02 00:00:00    1.5
   2000-01-02 00:02:00    3.0

为什么它比原系列早 2 分钟开始?为什么它包含中间的所有时间步,而原始系列中不包含这些时间步?我怎样才能得到我想要的结果?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    resample() 不适合您的目的。

    试试这个:

    series[series.index.minute % 2 == 0]
    

    【讨论】:

    猜你喜欢
    • 2016-10-03
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2014-07-24
    • 2013-06-02
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多