python pandas: how to calculate derivative/gradient

后端 未结 5 2138
野的像风
野的像风 2021-02-01 19:16

Given that I have the following two vectors:

In [99]: time_index
Out[99]: 
[1484942413,
 1484942712,
 1484943012,
 1484943312,
 1484943612,
 1484943912,
 1484944         


        
5条回答
  •  名媛妹妹
    2021-02-01 19:41

    pd.Series.diff() only takes the differences. It doesn't divide by the delta of the index as well.

    This gets you the answer

    recv.diff() / recv.index.to_series().diff().dt.total_seconds()
    
    2017-01-20 20:00:00            NaN
    2017-01-20 20:05:00    4521.493333
    2017-01-20 20:10:00    4533.760000
    2017-01-20 20:15:00    4557.493333
    2017-01-20 20:20:00    4536.053333
    2017-01-20 20:25:00    4567.813333
    2017-01-20 20:30:00    4406.160000
    2017-01-20 20:35:00    4366.720000
    2017-01-20 20:40:00    4407.520000
    2017-01-20 20:45:00    4421.173333
    Freq: 300S, dtype: float64
    

    You could also use numpy.gradient passing the bytes_in and the delta you expect to have. This will not reduce the length by one, instead making assumptions about the edges.

    np.gradient(bytes_in, 300) * 8
    
    array([ 4521.49333333,  4527.62666667,  4545.62666667,  4546.77333333,
            4551.93333333,  4486.98666667,  4386.44      ,  4387.12      ,
            4414.34666667,  4421.17333333])
    

提交回复
热议问题