用[0,1,…]作为值创建一个序列,然后调用resample:
s = pd.Series(np.arange(adj_close.shape[0]), index=adj_close.index)
locs = s.resample("M", how="max")
print locs
输出为:
Date
2002-07-31 0
2002-08-31 22
2002-09-30 42
2002-10-31 65
2002-11-30 85
2002-12-31 106
2003-01-31 127
2003-02-28 146
2003-03-31 167
2003-04-30 188
2003-05-31 209
2003-06-30 230
2003-07-31 252
2003-08-31 273
2003-09-30 294
...
2012-09-30 2561
2012-10-31 2582
2012-11-30 2603
2012-12-31 2623
2013-01-31 2644
2013-02-28 2663
2013-03-31 2683
2013-04-30 2705
2013-05-31 2727
2013-06-30 2747
2013-07-31 2769
2013-08-31 2791
2013-09-30 2811
2013-10-31 2834
2013-11-30 2844
Freq: M, Length: 137, dtype: int64
获取行:
print adj_close.iloc[locs, :].head(10)
输出:
IEF SPY
Date
2002-07-31 55.49 73.01
2002-08-30 56.89 73.51
2002-09-30 59.08 65.80
2002-10-31 58.34 71.22
2002-11-29 56.93 75.61
2002-12-31 58.95 71.33
2003-01-31 58.50 69.58
2003-02-28 59.79 68.64
2003-03-31 59.56 68.79
2003-04-30 59.64 74.61