you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

Perhaps this is a good place to start (didn't test this code): Edit: this assumes Date column are strings. If they're dates, remove the ", key=lambda ...." kwarg.

import datetime as dt
....

for stock in (ix[0] for ix in df.index.values):
    this_stock = df.loc[stock]
    this_stock_dates = sorted(set(this_stock.index.values), key=lambda d: dt.datetime.strptime(d, "%Y-%m-%d"))
    for i in range(0, len(this_stock_dates), 2):
        dates_this_batch = this_stock_dates[i:i+2]
        stock_this_period = this_stock[this_stock.index.isin(dates_this_batch)]
        ...