Calculating returns from a dataframe with financial data

后端 未结 4 1086
不知归路
不知归路 2021-02-02 11:44

I have a dataframe with monthly financial data:

In [89]: vfiax_monthly.head()
Out[89]: 
            year  month  day       d   open  close   high    low  volume          


        
4条回答
  •  孤城傲影
    2021-02-02 12:35

    Instead of slicing, use .shift to move the index position of values in a DataFrame/Series. For example:

    returns = (vfiax_monthly.open - vfiax_monthly.open.shift(1))/vfiax_monthly.open.shift(1)
    

    This is what pct_change is doing under the bonnet. You can also use it for other functions e.g.:

    (3*vfiax_monthly.open + 2*vfiax_monthly.open.shift(1))/5
    

    You might also want to looking into the rolling and window functions for other types of analysis of financial data.

提交回复
热议问题