有效使用熊猫系列,Numpy数组和Python列表

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

任务:我有一系列的每日收盘价,我想要实现以下目标:(i)计算任何5天窗口内任意两天之间的最大百分比变化(ii)将这些最大值存储在5天的滚动窗口中(iii)计算所有5天滚动窗口中这些最大值的第99个百分位数

相当简单的问题。我对Python很陌生,发现自己混合了Python列表,Numpy数组和Pandas系列来实现上述目标,我确信这不是有效编码的正确方法。出于VB的背景,一方面,我开始欣赏可以在Series和DataFrame对象上使用的非常方便的Pandas方法,但是我仍然在为选择的操作选择正确的“数组类型”对象而苦苦挣扎以上。

问题:可以对下面的代码进行哪些改进,以便仅使用Pandas对象和方法即可实现整个目标?还是只有Numpy对象和方法?非常感谢您的任何提示和技巧。

#pd_prices is a Pandas DataFrame containing daily open, high, low, and close prices

#store just closing prices into a Numpy array
np_prices = np.array(pd_prices['close'])

my_max = [0] # Python list that will be appended

# loop through Numpy array using ndenumerate
for i, price in np.ndenumerate(np_prices):

    local_max = 0.0
    j = min(i[0],(np_prices.size - 6))
    for k in range(1,6,1):
        # compute local % change between two days within a 5-day window
        pct_change = (np_prices[j+k] / np_prices[j]) - 1
        # store max over the current 5-day window 
        local_max = max(local_max,abs(pct_change))

     # save max from current 5-day window into  list before computing max over next 5-day window
     my_max.append(local_max)

#in order to use Pandas quantile function, the list is converted to Pandas Series object
pd_maxes = pd.Series(my_max)
print(pd_maxes.quantile(0.99))
python pandas numpy numpy-ndarray
1个回答
0
投票

几个建议:

© www.soinside.com 2019 - 2024. All rights reserved.