Logarithmic returns in pandas dataframe

后端 未结 5 1209
悲&欢浪女
悲&欢浪女 2020-12-07 15:45

Python pandas has a pct_change function which I use to calculate the returns for stock prices in a dataframe:

ndf[\'Return\']= ndf[\'TypicalPrice\'].pct_chan         


        
5条回答
  •  遥遥无期
    2020-12-07 16:15

    Single line, and only calculating logs once. First convert to log-space, then take the 1-period diff.

        np.diff(np.log(df.price))
    

    In earlier versions of numpy:

        np.log(df.price)).diff()
    

提交回复
热议问题