Skip to content Skip to sidebar Skip to footer

Implementing Pandas Function To Numpy Functions

Is there a way I could convert the xy_mean function to be computed using the pandas library just like the y_mean function. I found out that the pandas function Y_mean = pd.Series(P

Solution 1:

You would need to define a custom function for that, and pass it to rolling.apply:

>>>multiplier=np.arange(0,number)>>>defxymean(series):returnseries.mul(multiplier).sum()>>>pd.Series(PC_list).rolling(number).apply(xymean).dropna().to_numpy()[:-1]array([2490.601989,2440.743958,2409.067016,2413.002044,2510.497985,2543.348939,2516.922974,2459.627961,2418.983948,2335.007966,2280.283019,2288.94702,2300.19998,2279.389953,2212.294951,2080.693968,1978.774017,1960.123047,1989.229066,2061.27304,2137.145019,2167.67804,2175.047058,2221.807067,2290.639036,2361.986998,2376.473021])>>>(np.convolve(PC_list,np.arange(number,0,-1),mode='valid'))[:-1]array([2490.601989,2440.743958,2409.067016,2413.002044,2510.497985,2543.348939,2516.922974,2459.627961,2418.983948,2335.007966,2280.283019,2288.94702,2300.19998,2279.389953,2212.294951,2080.693968,1978.774017,1960.123047,1989.229066,2061.27304,2137.145019,2167.67804,2175.047058,2221.807067,2290.639036,2361.986998,2376.473021])

However, this will be a little slower, owing to the apply. Furthermore, it seems like your numpy version creates xy_sum as opposed to xy_mean, to make it calculate mean you would need:

>>>(np.convolve(PC_list, np.arange(number, 0, -1), mode='valid')/number)[:-1]

array([830.200663  , 813.58131933, 803.02233867, 804.33401467,
       836.83266167, 847.78297967, 838.97432467, 819.875987  ,
       806.32798267, 778.33598867, 760.09433967, 762.98234   ,
       766.73332667, 759.796651  , 737.43165033, 693.564656  ,
       659.591339  , 653.374349  , 663.07635533, 687.09101333,
       712.381673  , 722.55934667, 725.015686  , 740.60235567,
       763.54634533, 787.32899933, 792.15767367])

>>>defxymean(series):
        return series.mul(multiplier).mean()

>>>pd.Series(PC_list).rolling(number).apply(xymean).dropna().to_numpy()[:-1]

array([830.200663  , 813.58131933, 803.02233867, 804.33401467,
       836.83266167, 847.78297967, 838.97432467, 819.875987  ,
       806.32798267, 778.33598867, 760.09433967, 762.98234   ,
       766.73332667, 759.796651  , 737.43165033, 693.564656  ,
       659.591339  , 653.374349  , 663.07635533, 687.09101333,
       712.381673  , 722.55934667, 725.015686  , 740.60235567,
       763.54634533, 787.32899933, 792.15767367])

Post a Comment for "Implementing Pandas Function To Numpy Functions"