I have two tables:
- A three-level index of stock symbols
cik, buy datest0, and sell datest1. - A blank position DataFrame with range of dates in the index column and stock symbols across the columns.
I need to iterate through the first index, and set all of the values in the position matrix to 1 where the date is in the range of [t0, t1] to 1. The rest should be left at zero.
Sell Index sell_idx
MultiIndex([('AAPL', '2020-03-12', '2020-03-13'),
( 'IBM', '2020-03-13', '2020-03-16')],
)
Position Matrix pos
FB AAPL IBM
2020-03-12 0 0 0
2020-03-13 0 0 0
2020-03-16 0 0 0
Expected output
FB AAPL IBM
2020-03-12 0 1 0
2020-03-13 0 1 1
2020-03-16 0 0 1
I have done this successfully iteratively and frankly it's not even that slow:
idx = pd.MultiIndex.from_tuples(
(
('AAPL', pd.Timestamp('2020-03-12'), pd.Timestamp('2020-03-13'))
, ('IBM', pd.Timestamp('2020-03-13'), pd.Timestamp('2020-03-16'))
)
)
pos = pd.DataFrame(0, columns=['FB', 'AAPL', 'IBM']
, index=[pd.Timestamp('2020-03-12')
, pd.Timestamp('2020-03-13')
, pd.Timestamp('2020-03-16')])
for i in idx:
pos.loc[i[1]:i[2], i[0]] = 1
I would like to vectorize this code. How would I use advanced pandas slicing/indexing to do this without apply or for?
posare supposed to represent(1800, '2020-03-12', '2020-03-13')then expected output forposwould be2020-03-12 0 1 0 2020-03-13 0 1 0 2020-03-16 0 0 0pd.MultiIndex.from_tuples...or dictionary, or sometihing reproducible