Consider the following DataFrame returned by the following dictionary(code given below):
I want to create a new column in this DataFrame which tells us since how many days the values continuously True. (for each ticker i.e. groupby(ticker)).
For e.g. the values that should be there in the new column are written as comments in the below code (for the first few rows). Please comment if you've any doubts regarding the output that's needed:
{'DaysWithGain': {(Timestamp('2019-10-01 04:00:00+0000', tz='UTC'),
'AAPL'): True,
(Timestamp('2019-10-01 10:00:00+0000', tz='UTC'), 'AAPL'): True, #1
(Timestamp('2019-10-01 10:00:00+0000', tz='UTC'), 'FSLY'): False, #0
(Timestamp('2019-10-01 10:00:00+0000', tz='UTC'), 'LVGO'): False, #0
(Timestamp('2019-10-01 10:00:00+0000', tz='UTC'), 'SHOP'): True, #1
(Timestamp('2019-10-01 10:00:00+0000', tz='UTC'), 'UPLD'): False, #0
(Timestamp('2019-10-01 10:00:00+0000', tz='UTC'), 'ZM'): True, #1
(Timestamp('2019-10-01 16:00:00+0000', tz='UTC'), 'AAPL'): True, #1
(Timestamp('2019-10-01 16:00:00+0000', tz='UTC'), 'FSLY'): False,#0
(Timestamp('2019-10-01 16:00:00+0000', tz='UTC'), 'LVGO'): False, #0
(Timestamp('2019-10-01 16:00:00+0000', tz='UTC'), 'SHOP'): True, #1
(Timestamp('2019-10-01 16:00:00+0000', tz='UTC'), 'UPLD'): False, #0
(Timestamp('2019-10-01 16:00:00+0000', tz='UTC'), 'ZM'): True, #1
(Timestamp('2019-10-01 22:00:00+0000', tz='UTC'), 'AAPL'): True, #1
(Timestamp('2019-10-01 22:00:00+0000', tz='UTC'), 'FSLY'): False, #0
(Timestamp('2019-10-01 22:00:00+0000', tz='UTC'), 'LVGO'): False, #0
(Timestamp('2019-10-01 22:00:00+0000', tz='UTC'), 'SHOP'): True, # 1
(Timestamp('2019-10-01 22:00:00+0000', tz='UTC'), 'UPLD'): False, #0
(Timestamp('2019-10-01 22:00:00+0000', tz='UTC'), 'ZM'): True,#1
(Timestamp('2019-10-02 04:00:00+0000', tz='UTC'), 'AAPL'): True,#2
(Timestamp('2019-10-02 04:00:00+0000', tz='UTC'), 'FSLY'): False,#0
(Timestamp('2019-10-02 04:00:00+0000', tz='UTC'), 'LVGO'): False,#0
(Timestamp('2019-10-02 04:00:00+0000', tz='UTC'), 'SHOP'): True,#2
(Timestamp('2019-10-02 04:00:00+0000', tz='UTC'), 'UPLD'): False, #0
(Timestamp('2019-10-02 04:00:00+0000', tz='UTC'), 'ZM'): True, #2
(Timestamp('2019-10-02 10:00:00+0000', tz='UTC'), 'AAPL'): True, #2
(Timestamp('2019-10-02 10:00:00+0000', tz='UTC'), 'FSLY'): False, #0
(Timestamp('2019-10-02 10:00:00+0000', tz='UTC'), 'LVGO'): False,#0
(Timestamp('2019-10-02 10:00:00+0000', tz='UTC'), 'SHOP'): True, #0
(Timestamp('2019-10-02 10:00:00+0000', tz='UTC'), 'UPLD'): False, #0
(Timestamp('2019-10-02 10:00:00+0000', tz='UTC'), 'ZM'): True,#2
(Timestamp('2019-10-02 16:00:00+0000', tz='UTC'), 'AAPL'): True,#2
(Timestamp('2019-10-02 16:00:00+0000', tz='UTC'), 'FSLY'): False,#0
(Timestamp('2019-10-02 16:00:00+0000', tz='UTC'), 'LVGO'): False,#0
(Timestamp('2019-10-02 16:00:00+0000', tz='UTC'), 'SHOP'): True,#2
(Timestamp('2019-10-02 16:00:00+0000', tz='UTC'), 'UPLD'): False,#0
(Timestamp('2019-10-02 16:00:00+0000', tz='UTC'), 'ZM'): True, # 2
(Timestamp('2019-10-02 22:00:00+0000', tz='UTC'), 'AAPL'): True, #2
(Timestamp('2019-10-02 22:00:00+0000', tz='UTC'), 'FSLY'): False,#0
(Timestamp('2019-10-02 22:00:00+0000', tz='UTC'), 'LVGO'): False, #0
(Timestamp('2019-10-02 22:00:00+0000', tz='UTC'), 'SHOP'): True, #2
(Timestamp('2019-10-02 22:00:00+0000', tz='UTC'), 'UPLD'): False,#0
(Timestamp('2019-10-02 22:00:00+0000', tz='UTC'), 'ZM'): True,#2
(Timestamp('2019-10-03 04:00:00+0000', tz='UTC'), 'AAPL'): False,
(Timestamp('2019-10-03 04:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-03 04:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-03 04:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-03 04:00:00+0000', tz='UTC'), 'UPLD'): False,
(Timestamp('2019-10-03 04:00:00+0000', tz='UTC'), 'ZM'): True,
(Timestamp('2019-10-03 10:00:00+0000', tz='UTC'), 'AAPL'): False,
(Timestamp('2019-10-03 10:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-03 10:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-03 10:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-03 10:00:00+0000', tz='UTC'), 'UPLD'): False,
(Timestamp('2019-10-03 10:00:00+0000', tz='UTC'), 'ZM'): True,
(Timestamp('2019-10-03 16:00:00+0000', tz='UTC'), 'AAPL'): False,
(Timestamp('2019-10-03 16:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-03 16:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-03 16:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-03 16:00:00+0000', tz='UTC'), 'UPLD'): False,
(Timestamp('2019-10-03 16:00:00+0000', tz='UTC'), 'ZM'): True,
(Timestamp('2019-10-03 22:00:00+0000', tz='UTC'), 'AAPL'): False,
(Timestamp('2019-10-03 22:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-03 22:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-03 22:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-03 22:00:00+0000', tz='UTC'), 'UPLD'): False,
(Timestamp('2019-10-03 22:00:00+0000', tz='UTC'), 'ZM'): True,
(Timestamp('2019-10-04 04:00:00+0000', tz='UTC'), 'AAPL'): True,
(Timestamp('2019-10-04 04:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-04 04:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-04 04:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-04 04:00:00+0000', tz='UTC'), 'UPLD'): False,
(Timestamp('2019-10-04 04:00:00+0000', tz='UTC'), 'ZM'): True,
(Timestamp('2019-10-04 10:00:00+0000', tz='UTC'), 'AAPL'): True,
(Timestamp('2019-10-04 10:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-04 10:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-04 10:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-04 10:00:00+0000', tz='UTC'), 'UPLD'): False,
(Timestamp('2019-10-04 10:00:00+0000', tz='UTC'), 'ZM'): True,
(Timestamp('2019-10-04 16:00:00+0000', tz='UTC'), 'AAPL'): True,
(Timestamp('2019-10-04 16:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-04 16:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-04 16:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-04 16:00:00+0000', tz='UTC'), 'UPLD'): False,
(Timestamp('2019-10-04 16:00:00+0000', tz='UTC'), 'ZM'): False,
(Timestamp('2019-10-04 22:00:00+0000', tz='UTC'), 'AAPL'): True,
(Timestamp('2019-10-04 22:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-04 22:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-04 22:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-04 22:00:00+0000', tz='UTC'), 'UPLD'): False,
(Timestamp('2019-10-04 22:00:00+0000', tz='UTC'), 'ZM'): False,
(Timestamp('2019-10-07 04:00:00+0000', tz='UTC'), 'AAPL'): True,
(Timestamp('2019-10-07 04:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-07 04:00:00+0000', tz='UTC'), 'LVGO'): False,
(Timestamp('2019-10-07 04:00:00+0000', tz='UTC'), 'SHOP'): True,
(Timestamp('2019-10-07 04:00:00+0000', tz='UTC'), 'UPLD'): True,
(Timestamp('2019-10-07 04:00:00+0000', tz='UTC'), 'ZM'): False,
(Timestamp('2019-10-07 10:00:00+0000', tz='UTC'), 'AAPL'): True,
(Timestamp('2019-10-07 10:00:00+0000', tz='UTC'), 'FSLY'): False,
(Timestamp('2019-10-07 10:00:00+0000', tz='UTC'), 'LVGO'): False}}