2014-12-24 66 views
4

我想含有NaN秒(在這種情況下ImpVol字段)使用interpolate方法bfillffill多指數DataFrame。該DataFrame的部分可能是這樣的:填充多指數熊貓數據幀用內插法

Expiration OptionType Strike ImpVol 
2014-12-26 call  140.0   NaN 
         145.0   NaN 
         147.0   NaN 
         149.0   NaN 
         150.0   NaN 
         152.5   NaN 
         155.0  0.233631 
         157.5  0.206149 
         160.0  0.149118 
         162.5  0.110867 
         165.0  0.110047 
         167.5   NaN 
         170.0   NaN 
         172.5   NaN 
         175.0   NaN 
         177.5   NaN 
         180.0   NaN 
         187.5   NaN 
         192.5   NaN 
      put   132.0   NaN 
         135.0   NaN 
         140.0   NaN 
         141.0   NaN 
         142.0  0.541311 
         143.0   NaN 
         144.0  0.546672 
         145.0  0.504691 
         146.0  0.485586 
         147.0  0.426898 
         148.0  0.418084 
         149.0  0.405254 
         150.0  0.372353 
         152.5  0.311049 
         155.0  0.246892 
         157.5  0.187426 
         160.0  0.132475 
         162.5  0.098377 
         165.0   NaN 
         167.5  0.249519 
         170.0  0.270546 
         180.0   NaN 
         182.5  0.634539 
         185.0  0.656332 
         187.5  0.711593 
2015-01-02 call  145.0   NaN 
         146.0   NaN 
         149.0   NaN 
         150.0   NaN 
         152.5   NaN 
         155.0  0.213742 
         157.5  0.205705 
         160.0  0.160824 
         162.5  0.143180 
         165.0  0.129292 
         167.5  0.127415 
         170.0  0.148275 
         172.5   NaN 
         175.0   NaN 
         180.0   NaN 
         182.5   NaN 
         195.0   NaN 
      put   135.0  0.493639 
         140.0  0.463828 
         141.0  0.459619 
         142.0  0.442729 
         143.0  0.431823 
         145.0  0.391141 
         147.0  0.313090 
         148.0  0.310796 
         149.0  0.296146 
         150.0  0.280965 
         152.5  0.240727 
         155.0  0.203776 
         157.5  0.175431 
         160.0  0.143198 
         162.5  0.121621 
         165.0  0.105060 
         167.5  0.160085 
         170.0   NaN 

對於那些你不熟悉的領域,我插缺失(或壞)隱含期權波動率。這些需要通過到期期權和期權類型組合進行插值,並且不能在整個期權的整個期間內插。例如,我必須單獨插入整個2014-12-26call選項,而不是選擇2014-12-26put選項。

我以前選擇的值的切片像這樣的東西進行插值:

optype = 'call' 
expiry = '2014-12-26' 

s = df['ImpVol'][expiry][optype].interpolate().ffill().bfill() 

但框架可能非常大,我想,以避免通過各指標具有循環。如果我使用interpolate方法填充而不選擇切片(即在整個幀中),則interpolate將在所有子索引內插,這是我不想要的。例如:

print df['ImpVol'].interpolate().ffill().bfill() 

Expiration OptionType Strike ImpVol 
2014-12-26 call  140.0  0.233631 
         145.0  0.233631 
         147.0  0.233631 
         149.0  0.233631 
         150.0  0.233631 
         152.5  0.233631 
         155.0  0.233631 
         157.5  0.206149 
         160.0  0.149118 
         162.5  0.110867 
         165.0  0.110047 
         167.5  0.143222 
         170.0  0.176396 
         172.5  0.209570 
         175.0  0.242744 
         177.5  0.275918 
         180.0  0.309092 
         187.5  0.342267 
         192.5  0.375441 <-- interpolates from the 2014-12-26 call... 
      put   132.0  0.408615 <-- ... to the 2014-12-26 put, which is bad 
         135.0  0.441789 
         140.0  0.474963 
         141.0  0.508137 
         142.0  0.541311 
         143.0  0.543992 
         144.0  0.546672 
         145.0  0.504691 
         146.0  0.485586 
         147.0  0.426898 
         148.0  0.418084 
         149.0  0.405254 
         150.0  0.372353 
         152.5  0.311049 
         155.0  0.246892 
         157.5  0.187426 
         160.0  0.132475 
         162.5  0.098377 
         165.0  0.173948 
         167.5  0.249519 
         170.0  0.270546 
         180.0  0.452542 
         182.5  0.634539 
         185.0  0.656332 
         187.5  0.711593 

接下來的問題是,如何可以填寫基於所述索引多索引數據幀的每個子部分?

回答

3

我會嘗試在OptionType級別的索引處取出數據幀。

df.unstack(level=1) 

這樣你應該獲得一個單一的索引數據框,它將同時調用和把類別移動到列。也許這不是解決問題最優雅的方式,但它應該解決問題,不要讓投/罷手罷工重疊。

如果多索引df是進一步計算所需的數據,則可以使用堆棧方法恢復原始格式。

+1

您需要拆卸兩次才能在列標籤中包含日期;如果罷工按升序排列,則在日期間插入是沒有意義的。 – Luciano