简体   繁体   English

为什么我的用户定义 function 在执行 groupby.apply 时只返回第一组的返回?

[英]Why my user define function returns only first group's return when executing groupby.apply?

Summary概括

When groupby is used, the result is as follows.使用 groupby 时,结果如下。 在此处输入图像描述

The value of 2.19 is the user-defined function's return value of the first group. 2.19 的值是用户定义函数的第一组返回值。 That is, when the function is implemented for mulCut[(mulCut['date'] == '2018-03-05') & (mulCut['moneyness'] == 'atm')], I get 2.19.也就是说,当为 mulCut[(mulCut['date'] == '2018-03-05') & (mulCut['moneyness'] == 'atm')] 实施 function 时,我得到 2.19。

Explanation解释

I'm trying to get different returns by different groups by using.groupby.apply().我试图通过 using.groupby.apply() 获得不同组的不同回报。 In my case, groups are split by two variables 'date' and 'moneyness' as below.在我的例子中,组被两个变量“日期”和“金钱”分开,如下所示。 As you can see DataFrame below, 'date' contains four categorical groups 'atm', 'itm', 'otm' and 'tot'.正如您在下面看到的 DataFrame 所示,“date”包含四个分类组“atm”、“itm”、“otm”和“tot”。 在此处输入图像描述

And my user-defined function is as follows.而我的用户自定义function如下。 The function calculates the return from trading kospi index between 9:05 and 14:50. function 计算 9:05 至 14:50 之间交易 kospi 指数的回报。 Briefly, trading strategy is buying or selling kospi index according to signal.简而言之,交易策略是根据信号买入或卖出 kospi 指数。 '>= sensitivity' is buy signal and '<= 1/sensitivity' is sell signal. '>= 敏感度' 是买入信号,'<= 1/敏感度' 是卖出信号。 Since I assume that I can sell or buy all my budget for each signal, when short selling occurred already, sell signal is ignored.由于我假设我可以为每个信号卖出或买入我所有的预算,所以当卖空已经发生时,卖出信号被忽略。 Similary, if I bought kospi index already, buy signal is ignored.同样,如果我已经买入 kospi 指数,买入信号将被忽略。 Lastly, at last minute (14:50), trade must be liquidated.最后,在最后一刻(14:50),交易必须被清算。 That is, if my status is short selling in 14:49, I must buy kospi200 no matter what signal I receive in 14:50.也就是说,如果我在 14:49 的状态是卖空,那么无论我在 14:50 收到什么信号,我都必须买入 kospi200。 Similarly, if my status is buying in 14:49, I must sell kospi200.同样,如果我的状态是在 14:49 买入,我必须卖出 kospi200。

def get_onedayRt(onedayDf, timeVrbl, cpVrbl, kospiVrbl, sensitivity):
    onedayDf['action'] = np.nan
    state = 0 # 0: can buy or short sell, 1: can only sell, -1: can only buy
    value = 0 # return of simulation
    targetDf = onedayDf.sort_values(timeVrbl)
    targetDf = targetDf.reset_index(drop = True)
    lastidx = len(onedayDf) - 1

    for idx, timeData in targetDf.iterrows():
        if timeData[cpVrbl] >= sensitivity:
            if state == -1:
                state += 1
                targetDf.loc[idx, 'action'] = 1 #buy
                value -= timeData[kospiVrbl]

            elif state == 0:
                state += 1
                targetDf.loc[idx, 'action'] = 1
                value -= timeData[kospiVrbl]

        elif timeData[cpVrbl] <= 1/sensitivity:
            if state == 1:
                state -= 1
                targetDf.loc[idx, 'action'] = -1 # sell
                value += timeData[kospiVrbl]

            elif state == 0:
                state -= 1
                targetDf.loc[idx, 'action'] = -1
                value += timeData[kospiVrbl]

        if lastidx - 1 == idx:
            break # last action needs to be determied as below

    if state == -1:
        targetDf.loc[lastidx, 'action'] = 1
        value -= targetDf.loc[lastidx, kospiVrbl]
    elif state == 1:
        targetDf.loc[lastidx, 'action'] = -1
        value += targetDf.loc[lastidx, kospiVrbl]

    return value

I found that my function works appropriately for each specific group.我发现我的 function 适用于每个特定组。 That is, the code below works.也就是说,下面的代码有效。 I could get 2.97 which I wanted to get.我可以得到我想要的 2.97。

tmp = mulCut[(mulCut['date'] == '2018-03-05') & (mulCut['moneyness'] == 'tot')]
get_onedayRt(tmp, 'time', 'call/put', 'kospi200', 1)

Therefore, I wonder why my user define function returns only first group's return when executing groupby.apply?因此,我想知道为什么我的用户定义 function 在执行 groupby.apply 时只返回第一组的返回? And how can I edit my code to fix the problem?以及如何编辑我的代码来解决问题?

Thank you for reading my long question.感谢您阅读我的长问题。

I solved my problem finally... the first line of my function was the source of my problem.我终于解决了我的问题......我的 function 的第一行是我的问题的根源。 After the line is deleted, my code works properly.删除该行后,我的代码可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM