[英]Pandas Dataframe - Lookup Error
我正在嘗試使用日期和股票行情自動收錄器組合在pandas(0.14.1版)數據框中查找一行,並且收到一個奇怪的錯誤。
我的熊貓數據框如下所示:
AAPL IBM GOOG XOM Date
2011-01-10 16:00:00 340.99 143.41 614.21 72.02 2011-01-10
2011-01-11 16:00:00 340.18 143.06 616.01 72.56 2011-01-11
2011-01-12 16:00:00 342.95 144.82 616.87 73.41 2011-01-12
2011-01-13 16:00:00 344.20 144.55 616.69 73.54 2011-01-13
2011-01-14 16:00:00 346.99 145.70 624.18 74.62 2011-01-14
2011-01-18 16:00:00 339.19 146.33 639.63 75.45 2011-01-18
2011-01-19 16:00:00 337.39 151.22 631.75 75.00 2011-01-19
當我嘗試使用日期/字符串組合進行查找時,出現以下錯誤:
>>> df_data.lookup(date,ticker)
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2820, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-2-31ab981e2184>", line 1, in <module>
df_data.lookup(date,ticker)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2207, in lookup
n = len(row_labels)
TypeError: object of type 'datetime.datetime' has no len()
從我在pandas文檔中看到的內容來看,這應該可以正常工作,並且我的date變量是常規日期時間
>>> date
Out[5]: datetime.datetime(2011, 1, 10, 16, 0)
我在做明顯不正確的事情嗎?
df.lookup
需要2個df.lookup
數組的數組(而不是標量)作為參數:
In [25]: df.lookup(row_labels=[DT.datetime(2011,1,10,16,0)], col_labels=['AAPL'])
Out[25]: array([ 340.99])
如果只想查找一個值,請改用df.get_value
:
In [30]: df.get_value(DT.datetime(2011,1,10,16,0), 'AAPL')
Out[30]: 340.99000000000001
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.