<p>Pandas有一个<a href="http://pandas.pydata.org/pandas-docs/stable/cookbook.html#resampling" rel="nofollow noreferrer">TimeGrouper object</a>,它可以帮助您对具有
按时间间隔的DatetimeIndex。
<a href="https://stackoverflow.com/q/16982370/190597">Groupby operations can then be
nested</a>按<code>'id'</code>进一步分组:</p>
<pre><code>import numpy as np
import pandas as pd
df = pd.DataFrame([['2014-07-24 17:49:00', 5, 1046.0, -3, -239, 2800],
['2015-05-05 15:00:00', 2, np.nan, np.nan,np.nan, 2680],
['2015-05-05 15:00:00', 3, 989, 20, -11, 2680],
['2015-05-05 15:00:00', 4, 1022, 60, -76, 2600],
['2015-05-05 15:00:00', 5, np.nan, np.nan, np.nan, 2623]],
columns='timestamp id A B C D'.split())
df['timestamp'] = pd.DatetimeIndex(df['timestamp'])
df = df.set_index(['timestamp'])
print(df.groupby(pd.TimeGrouper('300D'), group_keys=False)
.apply(lambda grp: grp.set_index(['id'], append=True)
.groupby(level='id').ffill()))
</code></pre>
<p>收益率</p>
<pre><code> A B C D
timestamp id
2014-07-24 17:49:00 5 1046 -3 -239 2800
2015-05-05 15:00:00 2 NaN NaN NaN 2680
3 989 20 -11 2680
4 1022 60 -76 2600
5 1046 -3 -239 2623
</code></pre>