簡體   English   中英

嘗試在pandas數據幀上執行ffill()時的IndexError

[英]IndexError when trying to perform ffill() on pandas dataframe

任何人都可以解釋這個錯誤的含義 我有一個包含大量NaN值的大型數據框。 我只是想用前一個值填充某些列。 這是代碼:

import tables as tb
import pandas as pd

在這里,我打開一些pytables並將表導入數據幀

FGBL = tb.open_file("C:\\Users\\SUPER\\Documents\\NewQSPythonSamples\\FGBL.h5")

FGBM = tb.open_file("C:\\Users\\SUPER\\Documents\\NewQSPythonSamples\\FGBM.h5")

FGBS = tb.open_file("C:\\Users\\SUPER\\Documents\\NewQSPythonSamples\\FGBS.h5")

FGBLtable = FGBL.root.trade.Z4
FGBMtable = FGBM.root.trade.Z4
FGBStable = FGBS.root.trade.Z4

FGBStableq = FGBS.root.quote.Z4
FGBMtableq = FGBM.root.quote.Z4
FGBLtableq = FGBL.root.quote.Z4

fgbltrade = pd.DataFrame.from_records(FGBLtable.read())
fgbmtrade = pd.DataFrame.from_records(FGBMtable.read())
fgbstrade = pd.DataFrame.from_records(FGBLtable.read())

fgblquote = pd.DataFrame.from_records(FGBLtableq.read())
fgbmquote = pd.DataFrame.from_records(FGBMtableq.read())
fgbsquote = pd.DataFrame.from_records(FGBStableq.read())

然后我將日期時間從時間戳轉換為日期時間格式

fgbltrade["DateTimes"] = pd.to_datetime(fgbltrade.dateTime, unit="s")
fgbmtrade["DateTimes"] = pd.to_datetime(fgbmtrade.dateTime, unit="s")
fgbstrade["DateTimes"] = pd.to_datetime(fgbstrade.dateTime, unit="s")

fgblquote["DateTimes"] = pd.to_datetime(fgblquote.dateTime, unit="s")
fgbmquote["DateTimes"] = pd.to_datetime(fgbmquote.dateTime, unit="s")
fgbsquote["DateTimes"] = pd.to_datetime(fgbsquote.dateTime, unit="s")

對幀執行一些簡單的數學運算然后刪除NaN和不需要的列

fgblquote["VWPfgbl"] = (fgblquote.askPrc*fgblquote.bidSize + fgblquote.bidPrc*fgblquote.askSize)/(fgblquote.askSize + fgblquote.bidSize)
fgbmquote["VWPfgbm"] = (fgbmquote.askPrc*fgbmquote.bidSize + fgbmquote.bidPrc*fgbmquote.askSize)/(fgbmquote.askSize + fgbmquote.bidSize)
fgbsquote["VWPfgbs"] = (fgbsquote.askPrc*fgbsquote.bidSize + fgbsquote.bidPrc*fgbsquote.askSize)/(fgbsquote.askSize + fgbsquote.bidSize)

fgblquote = fgblquote.dropna()
fgbmquote = fgbmquote.dropna()
fgbsquote = fgbsquote.dropna()

fgblquote = fgblquote.drop(["askPrc", "askSize", "bidPrc", "bidSize", "dateTime"], axis=1)
fgbmquote = fgbmquote.drop(["askPrc", "askSize", "bidPrc", "bidSize", "dateTime"], axis=1)
fgbsquote = fgbsquote.drop(["askPrc", "askSize", "bidPrc", "bidSize", "dateTime"], axis=1)

然后我將框架合並在一起

df = pd.merge(fgbltrade, fgbmtrade, on='DateTimes', how = "outer")
df = pd.merge(df, fgbstrade, on='DateTimes', how = "outer")
df = pd.merge(df, fgblquote, on='DateTimes', how = "outer")
df = pd.merge(df, fgbmquote, on='DateTimes', how = "outer")
df = pd.merge(df, fgbsquote, on='DateTimes', how = "outer")

並嘗試填補前鋒

df = df["VWPfgbl"].ffill()
df = df["VWPfgbm"].ffill()
df = df["VWPfgbs"].ffill()

和錯誤:

In [3]: df = df["VWPfgbl"].ffill()
   ...: df = df["VWPfgbm"].ffill()
   ...: df = df["VWPfgbs"].ffill()
   ...: 
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-3-20f62c2a5da9> in <module>()
      1 df = df["VWPfgbl"].ffill()
----> 2 df = df["VWPfgbm"].ffill()
      3 df = df["VWPfgbs"].ffill()
      4 

C:\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    482     def __getitem__(self, key):
    483         try:
--> 484             result = self.index.get_value(self, key)
    485 
    486             if not np.isscalar(result):

C:\Anaconda3\lib\site-packages\pandas\core\index.py in get_value(self, series, key)
   1214             # python 3
   1215             if np.isscalar(key):  # pragma: no cover
-> 1216                 raise IndexError(key)
   1217             raise InvalidIndexError(key)
   1218  

IndexError: VWPfgbm

您的錯誤是您使用前一個值中的單個列覆蓋df變量。

df = df["VWPfgbl"].ffill()
df = df["VWPfgbm"].ffill()
df = df["VWPfgbs"].ffill()

第一行將分配df變量,使其成為原始數據幀的單個(填充)列。 這就是它在第二行失敗的原因,因為df現在沒有任何其他列,所以你得到一個IndexError

你應該重新編寫代碼

df["VWPfgbl"] = df["VWPfgbl"].ffill()
df["VWPfgbm"] = df["VWPfgbm"].ffill()
df["VWPfgbs"] = df["VWPfgbs"].ffill()

錯誤,

IndexError: VWPfgbm

是說df沒有名為'VWPfgbm'列。 您可以通過檢查df.columns來檢查該事實。

你可能想知道,如果fgbmquote["VWPfgbm"] ,和

df = pd.merge(df, fgblquote, on='DateTimes', how = "outer")

怎么可能是df不包含列, "VWPfgbm"

這可能發生的一個原因是dffgblquote都有"VWPfgbm"列。 然后pd.merge通過在合並的DataFrame中命名列"VWPfgbm_x""VWPfgbm_y"消除它們的歧義 請參閱pd.merge函數suffixes參數

例如,

import pandas as pd
foo = pd.DataFrame({'VWPfgbm':range(3), 'baz':list('ABC')})
bar = pd.DataFrame({'VWPfgbm':range(3,6), 'baz':list('CAB')})
pd.merge(foo, bar, on='baz', how='outer')

產量

   VWPfgbm_x baz  VWPfgbm_y
0          0   A          4
1          1   B          5
2          2   C          3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM