熊猫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
Run Code Online (Sandbox Code Playgroud)
更新:
我对@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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
另一种选择是直接生成每小时索引,然后删除非工作日:
\n\nhourly_index = pd.date_range('01-Jan-2017', '31-Jan-2017', freq='H')\nhourly_index = hourly_index[hourly_index.dayofweek < 5]\nRun Code Online (Sandbox Code Playgroud)\n\n性能对比:
\n\n10 loops, best of 3: 44.2 ms per loop1000 loops, best of 3: 1.46 ms per loop1000 loops, best of 3: 598 \xc2\xb5s per loop| 方法 时间| 相对| | --------------------------------- | --------- | ----- ----- | | OP的更新方法| 1.31毫秒| 17.6%| | 生成日期范围,np.in1d | 1.75毫秒| 23.5%| | 生成daterange,Series.isin | 1.90毫秒| 25.5%| | 用虚拟系列重新采样| 4.37毫秒| 58.7%| | OP的初始方法| 7.45毫秒| 100.0%|
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
Run Code Online (Sandbox Code Playgroud)
在这里,通过将两个Series的值转换为相同的numpy datetime类型(hourly_index过程中充满),可以提高速度。传递.values给numpy加快了速度。
Series.isin受@IanS方法启发,比初始出价更快的方法:每小时为数据中整个日期范围生成daterange,并仅选择与数据中现有日期匹配的那些条目:
%%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
Run Code Online (Sandbox Code Playgroud)
减少了大约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
Run Code Online (Sandbox Code Playgroud)
对于更快的方法:
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
Run Code Online (Sandbox Code Playgroud)
请注意,我们并不真正在乎该系列的价值。在这里我只是默认为int。
该表达式s.index.start_time.floor('D').isin(daily_index.start_time)为我们提供了布尔向量,该向量的s.index匹配天数为daily_index。
| 归档时间: |
|
| 查看次数: |
3699 次 |
| 最近记录: |