2021年1月25日星期一

Iterate Between Dates Based on Value and Pull forward Quantity from Last Date

I have a data frame as follows:

import pandas as pd    #Data    data = {'Symbol':['MU', 'F', 'F', 'BX', 'BX', 'GE', 'BX', 'MU'],           'Date':['2018-08-20', '2018-08-21', '2018-08-22', '2018-08-24', '2018-08-25', '2018-08-27', '2018-08-27', '2018-08-27'],          'Quantity':[28, 30, 30, 3, 3, 5, 4, -28]}       # Create DataFrame   df = pd.DataFrame(data)       # Print the output.   df   
    Symbol  Date    Quantity  0   MU  2018-08-20  28  1   F   2018-08-21  30  2   F   2018-08-22  30  3   BX  2018-08-24  3  4   BX  2018-08-25  3  5   GE  2018-08-27  5  6   BX  2018-08-27  4  7   MU  2018-08-27  -28  

I used the following code to create a cumulative cum of the quantity column by symbol on each date:

df1 = df.groupby(by=['Symbol','Date'])['Quantity'].sum().groupby(level='Symbol').cumsum().reset_index(name='Cumsum')  print (df1)  
  Symbol        Date  Cumsum  0     BX  2018-08-24       3  1     BX  2018-08-25       6  2     BX  2018-08-27      10  3      F  2018-08-21      30  4      F  2018-08-22      60  5     GE  2018-08-27       5  6     MU  2018-08-20      28  7     MU  2018-08-27       0  

Now for each symbol, I want to list all the dates between the start date and the end date (or today if still held) and pull forward the last quantity on each date. The data would then look like this:

Symbol  Date    Quantity  BX  2018-08-24  3  BX  2018-08-25  6  BX  2018-08-26  6  BX  2018-08-27  10  F   2018-08-21  30  F   2018-08-22  60  F   2018-08-23  60  F   2018-08-24  60  F   2018-08-25  60  F   2018-08-26  60  F   2018-08-27  60  GE  2018-08-27  5  MU  2018-08-20  28  MU  2018-08-21  28  MU  2018-08-22  28  MU  2018-08-23  28  MU  2018-08-24  28  MU  2018-08-25  28  MU  2018-08-26  28  MU  2018-08-27  0  

How would I go about doing this?

https://stackoverflow.com/questions/65895247/iterate-between-dates-based-on-value-and-pull-forward-quantity-from-last-date January 26, 2021 at 10:47AM

没有评论:

发表评论