在pandas 19中使用自定义形状的rolling_window

时间:2016-10-14 20:40:05

标签: python pandas

我曾经在我的代码中使用了0.17来获得pd.rolling_window(s,window = np.array(l),....)。 新的series.rolling(window,win_type)现在不支持窗口上的整数以外的任何内容,而win_type仅限于一组固定的形状。

如何将旧的自定义rolling_window均值迁移到0.19?

由于

1 个答案:

答案 0 :(得分:0)

似乎Rolling丢失了自定义窗口功能。幸运的是,它仍然保留了Rolling.apply方法。

如果你想要一个特殊的窗口意味着,一种方法是将numpy.correlate.apply(只是组合数组乘法和求和)中的任何一个归一化数组取>>> import numpy as np >>> import pandas as pd >>> df = pd.DataFrame({'a':[0,1,0,0,0,0],'b':[0,0,0,1,0,0]}) >>> print df a b 0 0 0 1 1 0 2 0 0 3 0 1 4 0 0 5 0 0 >>> my_array = np.array([1,2,3]) >>> # Below implements a weighted mean >>> df.rolling(len(my_array)).apply(lambda column: np.correlate(column,my_array/sum(my_array))) a b 0 NaN NaN 1 NaN NaN 2 0.333333 0.000000 3 0.166667 0.500000 4 0.000000 0.333333 5 0.000000 0.166667 >>> # Same thing, but thanks to '[0]' we can have min_periods < len(my_array) >>> df.rolling(len(my_array),min_periods=1).apply(lambda column: np.correlate(column,my_array/sum(my_array))[0]) a b 0 0.000000 0.000000 1 0.500000 0.000000 2 0.333333 0.000000 3 0.166667 0.500000 4 0.000000 0.333333 5 0.000000 0.166667 average() 1}}方法:

public class Digital_Analog_Clock_Beta_1 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        double outerBoxWidth = 500, outerBoxHeight = outerBoxWidth / 2.5;
        Rectangle outerBox = new Rectangle(0, 0, outerBoxWidth, outerBoxHeight);
        outerBox.setId("outer-box");

        Rectangle part1 = new Rectangle(0, 0, outerBoxWidth * .35, outerBoxHeight);
        part1.setId("partition");
        Rectangle part2 = new Rectangle(outerBoxWidth * .35, 0, outerBoxWidth * .05, outerBoxHeight);
        part2.setId("partition-alternate");
        Rectangle part3 = new Rectangle(outerBoxWidth * .4, 0, outerBoxWidth * .35, outerBoxHeight);
        part3.setId("partition");
        Rectangle part4 = new Rectangle(outerBoxWidth * .75, 0, outerBoxWidth * .35, outerBoxHeight);
        part4.setId("partition-alternate");

        double bigNumWidth = outerBoxWidth * .35;
        double digitWidth = (.9 * bigNumWidth / 2) * 0.95;
        double digitHeight = .9*outerBoxHeight;


        digit Digit1 = new digit(outerBoxWidth*.1,outerBoxHeight*.1,digitWidth,digitHeight);
        Digit1.bottom.setId("digits");
        Digit1.c.setFill(Color.AQUA);

        Pane pane = new Pane();
        pane.getChildren().add(outerBox);
        pane.getChildren().addAll(part1, part2, part3, part4);
        //pane.setId("background");

        pane.getChildren().add(Digit1);

        Scene scene = new Scene(pane, outerBoxWidth, outerBoxHeight);
        scene.getStylesheets().add(getClass().getResource("styleSheet.css").toExternalForm());

        primaryStage.setTitle("DIgital Analog Clock Beta 1");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }

}

编辑:Numpy也有一个带有权重参数的public class digit extends Pane { double startX, startY, digitWidth, digitHeight, lineWidth; public digit(double startX, double startY, double digitWidth, double digitHeight) { this.startX = startX; this.startY = startY; this.digitWidth = digitWidth; this.digitHeight = digitHeight; lineWidth = digitHeight / 20; getChildren().addAll(top,middle,bottom,upperLeft,upperRight,lowerLeft,lowerRight, c); } Polygon top = new Polygon(startX, startY, startX + digitWidth, startY, startX + digitWidth - lineWidth, startY + lineWidth * .95, startX + lineWidth, startY + lineWidth * .95); Polygon middle = new Polygon(startX + lineWidth, startY + digitHeight / 2 - lineWidth / 2 + lineWidth * .05, startX + digitWidth - lineWidth, startY + digitHeight / 2 - lineWidth / 2 + lineWidth * .05, startX + digitWidth - lineWidth * .05, startY + digitHeight, startX + digitWidth - lineWidth, startY + digitHeight / 2 + lineWidth / 2 - lineWidth * .05, startX + lineWidth, startY + digitHeight / 2 + lineWidth / 2 - lineWidth * .05, startX + lineWidth * .05, startY + digitHeight / 2); Polygon bottom = new Polygon(startX, startY + digitHeight, startX + digitWidth, startY + digitHeight, startX + digitWidth - lineWidth, startY + digitHeight - lineWidth * .95, startX + lineWidth, startY + digitHeight - lineWidth * .95); Polygon upperLeft = new Polygon(startX, startY, startX + lineWidth * .95, startY + lineWidth, startX + lineWidth * .95, startY + digitHeight / 2 - lineWidth / 2, startX, startY + digitHeight / 2); Polygon lowerLeft = new Polygon(startX, startY + digitHeight / 2, startX + lineWidth * .95, startY + digitHeight / 2 + lineWidth / 2, startX + lineWidth * .95, startY + digitHeight - lineWidth, startX, startY + digitHeight); Polygon upperRight = new Polygon(startX + digitWidth, startY, startX + digitWidth, startY + digitHeight / 2, startX + digitWidth - lineWidth * .95, startY + digitHeight / 2 - lineWidth / 2, startX + digitWidth - lineWidth * .95, startY + lineWidth); Polygon lowerRight = new Polygon(startX+digitWidth,startY+digitHeight/2,startX+digitWidth,startY+digitHeight,startX+digitWidth-lineWidth*.95,startY+digitHeight-lineWidth, startX+digitWidth-lineWidth*.95,startY+lineWidth/2+digitHeight/2); Circle c = new Circle(100.0f,100.0f,1000.0f); } 方法。如果只做一个简单的加权平均值,那就去吧。