【问题标题】:How to efficiently resample a DatetimeIndex如何有效地重新采样 DatetimeIndex
【发布时间】:2016-06-17 21:12:17
【问题描述】:

Pandas 在系列/数据帧上有一个 resample 方法,但似乎没有办法自行重新采样 DatetimeIndex

具体来说,我有一个可能缺少日期的每日Datetimeindex,我想按小时频率重新采样它,但只包括原始每日索引中的日期。

有没有比我下面的尝试更好的方法?

In [56]: daily_index = pd.period_range('01-Jan-2017', '31-Jan-2017', freq='B').asfreq('D')

In [57]: daily_index
Out[57]: 
PeriodIndex(['2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05',
             '2017-01-06', '2017-01-09', '2017-01-10', '2017-01-11',
             '2017-01-12', '2017-01-13', '2017-01-16', '2017-01-17',
             '2017-01-18', '2017-01-19', '2017-01-20', '2017-01-23',
             '2017-01-24', '2017-01-25', '2017-01-26', '2017-01-27',
             '2017-01-30', '2017-01-31'],
            dtype='int64', freq='D')

In [58]: daily_index.shape
Out[58]: (22,)

In [59]: hourly_index = pd.DatetimeIndex([]).union_many(
    ...:     pd.date_range(day.to_timestamp('H','S'), day.to_timestamp('H','E'), freq='H')
    ...:     for day in daily_index
    ...: )

In [60]: hourly_index
Out[60]: 
DatetimeIndex(['2017-01-02 00:00:00', '2017-01-02 01:00:00',
               '2017-01-02 02:00:00', '2017-01-02 03:00:00',
               '2017-01-02 04:00:00', '2017-01-02 05:00:00',
               '2017-01-02 06:00:00', '2017-01-02 07:00:00',
               '2017-01-02 08:00:00', '2017-01-02 09:00:00',
               ...
               '2017-01-31 14:00:00', '2017-01-31 15:00:00',
               '2017-01-31 16:00:00', '2017-01-31 17:00:00',
               '2017-01-31 18:00:00', '2017-01-31 19:00:00',
               '2017-01-31 20:00:00', '2017-01-31 21:00:00',
               '2017-01-31 22:00:00', '2017-01-31 23:00:00'],
              dtype='datetime64[ns]', length=528, freq=None)

In [61]: 22*24
Out[61]: 528

In [62]: %%timeit
    ...: hourly_index = pd.DatetimeIndex([]).union_many(
    ...:     pd.date_range(day.to_timestamp('H','S'), day.to_timestamp('H','E'), freq='H')
    ...:     for day in daily_index
    ...: )
100 loops, best of 3: 13.7 ms per loop

更新:

我对@NTAWolf 的答案略有不同,它具有相似的性能,但不会重新排序输入日期以防它们未排序

def resample_index(index, freq):
    """Resamples each day in the daily `index` to the specified `freq`.

    Parameters
    ----------
    index : pd.DatetimeIndex
        The daily-frequency index to resample
    freq : str
        A pandas frequency string which should be higher than daily

    Returns
    -------
    pd.DatetimeIndex
        The resampled index

    """
    assert isinstance(index, pd.DatetimeIndex)
    start_date = index.min()
    end_date = index.max() + pd.DateOffset(days=1)
    resampled_index = pd.date_range(start_date, end_date, freq=freq)[:-1]
    series = pd.Series(resampled_index, resampled_index.floor('D'))
    return pd.DatetimeIndex(series.loc[index].values)
In [184]: %%timeit
     ...: hourly_index3 = pd.date_range(daily_index.start_time.min(), 
     ...:                               daily_index.end_time.max() + 1, 
     ...:                               normalize=True, freq='H')
     ...: hourly_index3 = hourly_index3[hourly_index3.floor('D').isin(daily_index.start_time)]
100 loops, best of 3: 2.97 ms per loop

In [185]: %timeit resample_index(daily_index.to_timestamp('D','S'), freq='H')
100 loops, best of 3: 2.93 ms per loop

【问题讨论】:

    标签: python pandas


    【解决方案1】:
    |方法 |时间 |相对 | |------------------|---------|------ -----| | OP 的更新方法 | 1.31 毫秒 | 17.6% | |生成日期范围,np.in1d | 1.75 毫秒 | 23.5% | |生成日期范围,Series.isin | 1.90 毫秒 | 25.5% | |用虚拟系列重采样 | 4.37 毫秒 | 58.7% | | OP的初步做法| 7.45 毫秒 | 100.0 % |

    更新 2:生成日期范围,np.in1d

    再次,@IanS 激发了更多优化!这有点可读性差,但速度更快:

    %%timeit -r 10
    hourly_index4 = pd.date_range(daily_index.start_time.min(), 
                                  daily_index.end_time.max() + pd.DateOffset(days=1), 
                                  normalize=True, freq='H')
    overlap = np.in1d(np.array(hourly_index4.values, dtype='datetime64[D]'),
                      np.array(daily_index.start_time.values, dtype='datetime64[D]'))
    hourly_index4 = hourly_index4[overlap]
    
    1000 loops, best of 10: 1.75 ms per loop
    

    在这里,通过将两个 Series 的值转换为相同的 numpy datetime 类型来获得加速(在此过程中地板 hourly_index)。将 .values 传递给 numpy 会加快一点速度。

    更新 1:生成日期范围,Series.isin

    比初始出价更快的方法,受@IanS 方法的启发:每小时为数据中的完整日期范围生成日期范围,并仅选择与数据中现有日期匹配的条目:

    %%timeit
    hourly_index3 = pd.date_range(daily_index.start_time.min(), 
                                  # The following line should use 
                                  # +pd.DateOffset(days=1) in place of +1
                                  # but is left as is to show the option.
                                  daily_index.end_time.max() + 1, 
                                  normalize=True, freq='H')
    hourly_index3 = hourly_index3[hourly_index3.floor('D').isin(daily_index.start_time)]
    
    100 loops, best of 3: 1.9 ms per loop
    

    这会减少大约 75% 的处理时间。

    原始答案:使用虚拟系列重新采样

    使用虚拟系列,您可以避免循环。在我的电脑上,它会减少大约 40% 的运行时间。

    您的方法我得到以下时间:

    In [14]: %%timeit -o -r 10
       ....: hourly_index = pd.DatetimeIndex([]).union_many(   
       ....:     pd.date_range(day.to_timestamp('H','S'), day.to_timestamp('H','E'), freq='H')
       ....:     for day in daily_index
       ....: )
       ....: 
    100 loops, best of 10: 7.45 ms per loop
    

    对于更快的方法:

    In [13]: %%timeit -o -r 10
    s = pd.Series(0, index=daily_index)
    s = s.resample('H').last()
    s = s[s.index.start_time.floor('D').isin(daily_index.start_time)]
    hourly_index2 = s.index.start_time
       ....: 
    100 loops, best of 10: 4.37 ms per loop
    

    请注意,我们并不真正关心该系列的价值;这里我只是默认为int

    表达式s.index.start_time.floor('D').isin(daily_index.start_time) 为我们提供了一个布尔向量,其中s.index 中的值与daily_index 中的天数匹配。

    【讨论】:

    • 使用 floor 的奖励积分 - 我对 pandas api 相当熟悉,但对我来说这是一个新的!
    • 不知道hourly_index3.date会不会比hourly_index3.floor('D')快。
    • @IanS,你的评论让我找到了一种更快的方法,直接使用 numpy 类型 :-) hourly_index3.date 不能直接插入,因为它是一个 numpy 数组,它没有.isin。此外,它还有dtype=object,这对我们的目的来说效率不高。因此在上面的更新 2 中看到了修改。
    • @DaveHirschfeld 这是一个非常好的解决方案。 pd.DateOffset(days=1) 也不错;这对我来说是一个新的!感觉比只使用 +1 并希望数据类型正确要安全得多。
    • @NTAWolf,很好!太糟糕了,我不能赞成你的答案,我昨天已经做过了:)
    【解决方案2】:

    另一种选择是直接生成每小时索引,然后删除非工作日:

    hourly_index = pd.date_range('01-Jan-2017', '31-Jan-2017', freq='H')
    hourly_index = hourly_index[hourly_index.dayofweek < 5]
    

    性能对比:

    • OP的解决方案:10 loops, best of 3: 44.2 ms per loop
    • EdChum 的解决方案:1000 loops, best of 3: 1.46 ms per loop
    • 我的解决方案:1000 loops, best of 3: 598 µs per loop

    【讨论】:

    • 您应该添加 OP 尝试比较时间的时间,因为机器速度不同 +1
    • pd.date_range('01-Jan-2017', '31-Jan-2017', freq='H')Timestamp('2017-01-31 00:00:00', offset='H') 结尾,这意味着我们没有捕获范围内最后一天的小时数。加一:-)
    • 工作日只是为了模拟一个不连续的系列。一般来说,缺失的日期可能是随机的,因此后过滤操作不会那么简单
    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2014-06-12
    相关资源
    最近更新 更多