DataFrame数据分析:获取最大值的下表索引

dataframe
策略分享
rolling
标签: #<Tag:0x00007fb3d0e86008> #<Tag:0x00007fb3d0e85dd8> #<Tag:0x00007fb3d0e85c70>

(Hugo999) #1

我想请教一下,到现在为止如果我想计算某两个交易日之间(比如2018-08-20到2019-01-11)出现最高收盘价的是哪个交易日,然后最后还返回DataFrame格式(列的名称还是‘date’‘前复权’‘MACDsignal’之类的)。该怎么表达?

万分感谢

克隆策略
In [2]:
import talib
import pandas as pd
import numpy as np

df1 = DataSource('bar1d_CN_STOCK_A').read(instruments = ['600837.SHA'],
                                          start_date='2018-08-02',end_date='2019-02-01',
                                          fields=['close','adjust_factor']).set_index('date')

df1['前复权'] = df1['close'] / df1['adjust_factor']

#通过rolling_mean 函数来计算移动平均值

df1['MA10_rolling'] = pd.rolling_mean(df1['close'],10)


#将价格数据转化成float类型

close = [float(x) for x in df1['close']]

df1['MA10_rolling']

#  计算海通的日bar的macd和signal数值

df1['MACD'],df1['MACDsignal'],df1['MACDhist'] = talib.MACD(np.array(close),
                            fastperiod=6, slowperiod=12, signalperiod=9)   

df1
Out[2]:
instrument adjust_factor close 前复权 MA10_rolling MACD MACDsignal MACDhist
date
2018-08-02 600837.SHA 22.269615 197.308792 8.860000 NaN NaN NaN NaN
2018-08-03 600837.SHA 22.269615 196.640717 8.830001 NaN NaN NaN NaN
2018-08-06 600837.SHA 22.269615 191.741394 8.610001 NaN NaN NaN NaN
2018-08-07 600837.SHA 22.269615 196.640717 8.830001 NaN NaN NaN NaN
2018-08-08 600837.SHA 22.269615 193.745667 8.700001 NaN NaN NaN NaN
2018-08-09 600837.SHA 22.269615 197.086105 8.850000 NaN NaN NaN NaN
2018-08-10 600837.SHA 22.269615 196.195312 8.810000 NaN NaN NaN NaN
2018-08-13 600837.SHA 22.269615 194.191055 8.720000 NaN NaN NaN NaN
2018-08-14 600837.SHA 22.269615 193.745667 8.700001 NaN NaN NaN NaN
2018-08-15 600837.SHA 22.269615 188.400955 8.460000 194.569638 NaN NaN NaN
2018-08-16 600837.SHA 22.269615 188.178253 8.450000 193.656584 NaN NaN NaN
2018-08-17 600837.SHA 22.269615 186.396683 8.370000 192.632181 NaN NaN NaN
2018-08-20 600837.SHA 22.269615 189.291733 8.500000 192.387215 NaN NaN NaN
2018-08-21 600837.SHA 22.269615 191.073303 8.580000 191.830473 NaN NaN NaN
2018-08-22 600837.SHA 22.269615 189.737122 8.520000 191.429619 NaN NaN NaN
2018-08-23 600837.SHA 22.269615 191.296005 8.590000 190.850609 NaN NaN NaN
2018-08-24 600837.SHA 22.269615 192.409485 8.640000 190.472026 NaN NaN NaN
2018-08-27 600837.SHA 22.269615 194.859146 8.750001 190.538835 NaN NaN NaN
2018-08-28 600837.SHA 22.269615 194.413742 8.730000 190.605643 NaN NaN NaN
2018-08-29 600837.SHA 22.269615 193.522964 8.690001 191.117844 0.181080 -1.056776 1.237856
2018-08-30 600837.SHA 22.269615 191.073303 8.580000 191.407349 -0.107543 -0.866929 0.759387
2018-08-31 600837.SHA 22.269615 190.850616 8.570001 191.852742 -0.306623 -0.754868 0.448245
2018-09-03 600837.SHA 22.269615 189.959824 8.530001 191.919551 -0.530936 -0.710082 0.179146
2018-09-04 600837.SHA 22.269615 192.186783 8.630000 192.030899 -0.349506 -0.637967 0.288460
2018-09-05 600837.SHA 22.269615 188.846344 8.480000 191.941821 -0.664986 -0.643370 -0.021615
2018-09-06 600837.SHA 22.269615 188.846344 8.480000 191.696855 -0.826430 -0.679982 -0.146448
2018-09-07 600837.SHA 22.269615 189.291733 8.500000 191.385080 -0.828947 -0.709775 -0.119172
2018-09-10 600837.SHA 22.269615 184.169724 8.270000 190.316138 -1.469461 -0.861712 -0.607749
2018-09-11 600837.SHA 22.269615 185.728592 8.340000 189.447623 -1.586428 -1.006656 -0.579772
2018-09-12 600837.SHA 22.269615 187.510162 8.420000 188.846342 -1.352457 -1.075816 -0.276641
... ... ... ... ... ... ... ... ...
2018-12-20 600837.SHA 22.269615 208.666306 9.370001 213.097955 -1.430461 -0.276195 -1.154267
2018-12-21 600837.SHA 22.269615 203.321594 9.130000 211.939935 -2.163027 -0.653561 -1.509466
2018-12-24 600837.SHA 22.269615 200.871933 9.020000 210.403331 -2.833741 -1.089597 -1.744144
2018-12-25 600837.SHA 22.269615 194.413742 8.730000 208.309987 -3.966187 -1.664915 -2.301272
2018-12-26 600837.SHA 22.269615 193.077576 8.670000 206.038487 -4.652492 -2.262431 -2.390062
2018-12-27 600837.SHA 22.269615 192.854874 8.660000 203.432942 -4.892154 -2.788375 -2.103779
2018-12-28 600837.SHA 22.269615 195.972626 8.800000 201.807260 -4.410833 -3.112867 -1.297966
2019-01-02 600837.SHA 22.269615 195.081833 8.760000 200.070230 -4.043509 -3.298995 -0.744514
2019-01-03 600837.SHA 22.269615 199.090363 8.940001 199.090367 -3.115166 -3.262230 0.147063
2019-01-04 600837.SHA 22.269615 213.120224 9.570001 199.647107 -0.567058 -2.723195 2.156137
2019-01-07 600837.SHA 22.269615 213.788315 9.600000 200.159308 1.086033 -1.961349 3.047383
2019-01-08 600837.SHA 22.269615 212.674835 9.550000 201.094632 1.890584 -1.190963 3.081547
2019-01-09 600837.SHA 22.269615 215.569885 9.680000 202.564427 2.675513 -0.417668 3.093181
2019-01-10 600837.SHA 22.269615 211.561356 9.500001 204.279189 2.503719 0.166610 2.337109
2019-01-11 600837.SHA 22.269615 212.897522 9.560000 206.261183 2.466031 0.626494 1.839537
2019-01-14 600837.SHA 22.269615 213.565613 9.590000 208.332257 2.422956 0.985786 1.437169
2019-01-15 600837.SHA 22.269615 219.578415 9.860001 210.692836 3.083315 1.405292 1.678023
2019-01-16 600837.SHA 22.269615 218.019547 9.790001 212.986607 3.141337 1.752501 1.388836
2019-01-17 600837.SHA 22.269615 216.237976 9.710001 214.701369 2.803393 1.962679 0.840713
2019-01-18 600837.SHA 22.269615 219.355713 9.850000 215.324918 2.887045 2.147552 0.739492
2019-01-21 600837.SHA 22.269615 219.578415 9.860001 215.903928 2.840068 2.286055 0.554012
2019-01-22 600837.SHA 22.269615 214.901794 9.650001 216.126624 2.070140 2.242872 -0.172733
2019-01-23 600837.SHA 22.269615 216.015274 9.700001 216.171162 1.660636 2.126425 -0.465789
2019-01-24 600837.SHA 22.269615 220.691895 9.910001 217.084216 1.956836 2.092507 -0.135671
2019-01-25 600837.SHA 22.269615 224.032333 10.060000 218.197697 2.490341 2.172074 0.318267
2019-01-28 600837.SHA 22.269615 223.364243 10.030000 219.177560 2.615223 2.260704 0.354519
2019-01-29 600837.SHA 22.269615 222.473465 9.990001 219.467065 2.458281 2.300219 0.158062
2019-01-30 600837.SHA 22.269615 219.578415 9.860001 219.622952 1.873605 2.214896 -0.341291
2019-01-31 600837.SHA 22.269615 224.477722 10.080000 220.446927 2.083936 2.188704 -0.104768
2019-02-01 600837.SHA 22.269615 229.154343 10.290000 221.426790 2.736154 2.298194 0.437960

124 rows × 8 columns


(iQuant) #2

您好,收到您的提问,已提交给工程师,会尽快为您解答


(polll) #3

这个属于 Pandas DataFrame相关问题。可以看看:

  • idxmax:返回最大值的索引
  • rolling apply:如果需要在一个窗口内滚动计算最大值

(Hugo999) #4

好的,感谢


(Hugo999) #5

好的,我研究一下,谢谢


(hardsum) #6
df.ix[df[['close']].idxmax()]