dataframe parameters:为什么我的df上的更改是本地的?

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

我有一个函数应该在df上实现一些转换和计算,代码运行但是一旦完成,df保持与以前相同。

编辑:当我删除第2行(pd.merge)时问题解决了,但我想了解原因。

def Tilt_contribution(ptf, data_factors, data_universe, Sample_size_adjustment:bool):
    ptf['ActiveWeight'] = ptf['PortfolioWeight']- ptf['BenchmarkWeight']
    ptf = pd.merge(ptf, data_factors, on = ['ISIN','SecurityName'], how = 'left' )
    sample_size_adj = 1 #initialisation
    if Sample_size_adjustment == True:
        sample_size_adj = Sample_size_adj(ptf)
    for column in factor_list:
        #Weighted std
        universe_wght_std = Universe_weighted_std(data_universe, column)
        #Benchmark weighted average
        benchmk_wght_avg = Benchmark_weighted_avg(ptf, column)
        #Tilt Contrib   
        col_index = ptf.columns.get_loc(column)
        ptf.insert(col_index+1, column + ' Tilt Contribution', 0)    
        for idx in ptf.index:
            tilt_cont = (ptf.at[idx, column] - benchmk_wght_avg)*ptf.at[idx, 'ActiveWeight']/(sample_size_adj * universe_wght_std)
            if math.isnan(tilt_cont):
                tilt_cont=0
            ptf.at[idx, column + ' Tilt Contribution'] = tilt_cont
    global Tilt
    Tilt = ptf.sum()
python pandas
1个回答
0
投票

您正在使用Tilt的总和设置全局变量ptf,但不返回修改后的ptf DataFrame。

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