在python中重新采样和切片数据

问题描述 投票:0回答:1

数据看起来像这样:

    High    Low Open    Close   Volume  Adj Close
Date                        
1999-12-31  1472.420044 1458.189941 1464.469971 1469.250000 374050000   1469.250000
2000-01-03  1478.000000 1438.359985 1469.250000 1455.219971 931800000   1455.219971
2000-01-04  1455.219971 1397.430054 1455.219971 1399.420044 1009000000  1399.420044
2000-01-05  1413.270020 1377.680054 1399.420044 1402.109985 1085500000  1402.109985
2000-01-06  1411.900024 1392.099976 1402.109985 1403.449951 1092300000  1403.449951
... ... ... ... ... ... ...
2020-01-06  3246.840088 3214.639893 3217.550049 3246.280029 3674070000  3246.280029
2020-01-07  3244.909912 3232.429932 3241.860107 3237.179932 3420380000  3237.179932
2020-01-08  3267.070068 3236.669922 3238.590088 3253.050049 3720890000  3253.050049
2020-01-09  3275.580078 3263.669922 3266.030029 3274.699951 3638390000  3274.699951
2020-01-10  3282.989990 3268.010010 3281.810059 3273.739990 920449258   3273.739990
5039 rows × 6 columns

因为这是每日数据,所以我用:

weekly_resample = data.High.resample('M')这将生成一个DatetimeIndexResampler对象文件。现在,我想对这些数据进行切片以仅查看最近的10周,为此,我已经这样做:

weekly_resample = data.High.resample('M')[-1:10]

但是这会产生错误:

KeyError: 'Column not found: slice(-1, 10, None)'

我如何在最近10周内切片?

数据看起来像这样:高低开盘平仓成交量平仓日期1999-12-31 1472.420044 1458.189941 1464.469971 1469.250000 374050000 1469.250000 2000-01-03 ...

python-3.x pandas slice resampling
1个回答
1
投票
DataFrame.groupbyDataFrame.groupby一起使用,因此可能对最后10行使用Grouper
© www.soinside.com 2019 - 2024. All rights reserved.