Pandas reverse of diff()

前端 未结 3 841
小蘑菇
小蘑菇 2021-01-11 18:33

I have calculated the differences between consecutive values in a series, but I cannot reverse / undifference them using diffinv():

ds_sqrt =         


        
3条回答
  •  北海茫月
    2021-01-11 18:56

    You can use diff_inv from pmdarima.Docs link

    # genarating random table
      np.random.seed(10)
      vals = np.random.randint(1, 10, 6)
      df_t = pd.DataFrame({"a":vals})
    
      #creating two columns with diff 1 and diff 2
      df_t['dif_1'] = df_t.a.diff(1)
      df_t['dif_2'] = df_t.a.diff(2)
    
      df_t
    
        a   dif_1   dif_2
      0 5   NaN     NaN
      1 1   -4.0    NaN
      2 2   1.0    -3.0
      3 1   -1.0    0.0
      4 2   1.0     0.0
      5 9   7.0     8.0
    

    Then create a function that will return an array with inverse values of diff.

    from pmdarima.utils import diff_inv
    
    def inv_diff (df_orig_column,df_diff_column, periods):
    # Generate np.array for the diff_inv function - it includes first n values(n = 
    # periods) of original data & further diff values of given periods
    value = np.array(df_orig_column[:periods].tolist()+df_diff_column[periods:].tolist())
    
    # Generate np.array with inverse diff
    inv_diff_vals = diff_inv(value, periods,1 )[periods:]
    return inv_diff_vals
    

    Example of Use:

    # df_orig_column - column with original values
    # df_diff_column - column with differentiated values
    # periods - preiods for pd.diff()
    inv_diff(df_t.a, df_t.dif_2, 2) 
    

    Output:

    array([5., 1., 2., 1., 2., 9.])
    

提交回复
热议问题