Calculate Daily Returns with Pandas DataFrame

前端 未结 3 1713
不思量自难忘°
不思量自难忘° 2021-01-31 18:41

Here is my Pandas data frame:

prices = pandas.DataFrame([1035.23, 1032.47, 1011.78, 1010.59, 1016.03, 1007.95, 
              1022.75, 1021.52, 1026.11, 1027.04,         


        
相关标签:
3条回答
  • 2021-01-31 19:07

    Because operations will do alignment on index, you can convert one of the DataFrames to array:

    prices[:-1].values / prices[1:] - 1
    

    or

    prices[:-1] / prices[1:].values - 1
    

    depends on what the index of the result you want.

    or use shift() method:

    prices.shift(1) / prices - 1
    

    and:

    prices / prices.shift(1) - 1
    
    0 讨论(0)
  • 2021-01-31 19:11

    Just a bit of complement to @YaOzl 's answer, and in case if someone would read this. If your return data is a panel spreadsheet with several stocks:

    >>> prices = pandas.DataFrame(
    {"StkCode":["StockA","StockA","StockA","StockA","StockA","StockB","StockB","StockB","StockB","StockB","StockC","StockC","StockC","StockC","StockC",], 
    "Price":[1035.23, 1032.47, 1011.78, 1010.59, 1016.03, 1007.95, 1022.75, 1021.52, 1026.11, 1027.04, 1030.58, 1030.42, 1036.24, 1015.00, 1015.20]}
    )
    

    Which gives you:

          Price StkCode
    0   1035.23  StockA
    1   1032.47  StockA
    2   1011.78  StockA
    3   1010.59  StockA
    4   1016.03  StockA
    5   1007.95  StockB
    6   1022.75  StockB
    7   1021.52  StockB
    8   1026.11  StockB
    9   1027.04  StockB
    10  1030.58  StockC
    11  1030.42  StockC
    12  1036.24  StockC
    13  1015.00  StockC
    14  1015.20  StockC
    

    Then you could simply jointly use .pct_change(k) with .groupby(StkCode). And it is thound times faster than using an iterator...(I tried on my dataset, successfully shrink the process time from 10 hrs to 20 seconds!!)

    >>> prices["Return"] = prices.groupby("StkCode")["Price"].pct_change(1)
    

    Gives you:

          Price StkCode    Return
    0   1035.23  StockA       NaN
    1   1032.47  StockA -0.002666
    2   1011.78  StockA -0.020039
    3   1010.59  StockA -0.001176
    4   1016.03  StockA  0.005383
    5   1007.95  StockB       NaN
    6   1022.75  StockB  0.014683
    7   1021.52  StockB -0.001203
    8   1026.11  StockB  0.004493
    9   1027.04  StockB  0.000906
    10  1030.58  StockC       NaN
    11  1030.42  StockC -0.000155
    12  1036.24  StockC  0.005648
    13  1015.00  StockC -0.020497
    14  1015.20  StockC  0.000197
    
    0 讨论(0)
  • 2021-01-31 19:13

    Why not use the very convenient pct_change method provided by pandas by default:

    import pandas as pd
    
    prices = pandas.DataFrame([1035.23, 1032.47, 1011.78, 1010.59, 1016.03, 1007.95, 
              1022.75, 1021.52, 1026.11, 1027.04, 1030.58, 1030.42,
              1036.24, 1015.00, 1015.20])
    
    daily_return = prices.pct_change(1) # 1 for ONE DAY lookback
    monthly_return = prices.pct_change(21) # 21 for ONE MONTH lookback
    annual_return = prices.pct_change(252) # 252 for ONE YEAR lookback
    

    Original prices:

    print(prices)
              0                                                                    
    0   1035.23                                                                    
    1   1032.47                                                                    
    2   1011.78                                                                    
    3   1010.59                                                                    
    4   1016.03                                                                    
    5   1007.95                                                                    
    6   1022.75                                                                    
    7   1021.52                                                                    
    8   1026.11                                                                    
    9   1027.04                                                                    
    10  1030.58                                                                    
    11  1030.42                                                                    
    12  1036.24                                                                    
    13  1015.00                                                                    
    14  1015.20                                                                    
    

    Daily Return as prices.pct_change(1):

    print(prices.pct_change(1))
               0                                                                   
    0        NaN                                                                   
    1  -0.002666                                                                   
    2  -0.020039                                                                   
    3  -0.001176                                                                   
    4   0.005383                                                                   
    5  -0.007953                                                                   
    6   0.014683                                                                   
    7  -0.001203                                                                   
    8   0.004493                                                                   
    9   0.000906                                                                   
    10  0.003447                                                                   
    11 -0.000155                                                                   
    12  0.005648                                                                   
    13 -0.020497                                                                   
    14  0.000197 
    
    0 讨论(0)
提交回复
热议问题