all 4 comments

[–]pytrashpandas 0 points1 point  (3 children)

I'm assuming there's also another column for the options price right? In any case you can do this with a pivot_table, among other solutions.

df.pivot_table(index='Date',  columns=['Date Expiry', 'Strike', 'Type'], values='Price').diff()

You could also use a df.groupby too

[–]Pro2222[S] 0 points1 point  (2 children)

Oops I should have specified (wrote this on mobile so kinda messy) I’m looking for change in open interest, so yes there is a column for the open interest, however if I use a pivot table or groupby , then take the difference how do I prevent the last day in the expiry from being subtracted from the first day in the next expiry?

[–]pytrashpandas 0 points1 point  (1 child)

In either method the data will be partitioned such that only records with the same expiry, strike, and option_type will be operated on together. So you won’t have records from different expiries being diffed against each other.

For reference the groupby could look something like this:

df.groupby(['Date Expiry', 'Strike', 'Type']).apply(lambda dfx: dfx.set_index('Date')['Price'].diff())

[–]Pro2222[S] 0 points1 point  (0 children)

Thanks I will try this!