簡體   English   中英

熊貓系列浮動指數

[英]Pandas Series float index

我試圖使用浮點數作為pandas系列的索引,但它似乎沒有正常工作。 例如,在下面的代碼中,當我嘗試調用值策略[9.7]時,它返回錯誤。 我認為這是因為浮點數9.7在計算機中沒有精確地存儲為9.7(例如,9.7000000001)。 反正有沒有繞過這個? 或者使用浮點索引根本不是一個好主意?

任何輸入都非常感謝。 謝謝!

import numpy as np
import pandas as pd
W_grid  = np.arange(100)*0.1
policy = pd.Series(np.random.rand(100), index = W_grid)

policy[9.7]

KeyError                                  Traceback (most recent call last)
<ipython-input-224-89dfc3470c3d> in <module>()
----> 1 policy[9.7]

D:\Warren\Anaconda\lib\site-packages\pandas\core\series.pyc 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):

D:\Warren\Anaconda\lib\site-packages\pandas\core\index.pyc in get_value(self, series, key)
   2038 
   2039         k = _values_from_object(key)
-> 2040         loc = self.get_loc(k)
   2041         new_values = series.values[loc]
   2042         if np.isscalar(new_values) or new_values is None:

D:\Warren\Anaconda\lib\site-packages\pandas\core\index.pyc in get_loc(self, key)
   2091         except (TypeError, NotImplementedError):
   2092             pass
-> 2093         return super(Float64Index, self).get_loc(key)
   2094 
   2095     @property

D:\Warren\Anaconda\lib\site-packages\pandas\core\index.pyc in get_loc(self, key)
   1179         loc : int if unique index, possibly slice or mask if not
   1180         """
-> 1181         return self._engine.get_loc(_values_from_object(key))
   1182 
   1183     def get_value(self, series, key):

D:\Warren\Anaconda\lib\site-packages\pandas\index.pyd in pandas.index.IndexEngine.get_loc (pandas\index.c:3656)()

D:\Warren\Anaconda\lib\site-packages\pandas\index.pyd in pandas.index.IndexEngine.get_loc (pandas\index.c:3534)()

D:\Warren\Anaconda\lib\site-packages\pandas\hashtable.pyd in pandas.hashtable.Float64HashTable.get_item (pandas\hashtable.c:9645)()

D:\Warren\Anaconda\lib\site-packages\pandas\hashtable.pyd in pandas.hashtable.Float64HashTable.get_item (pandas\hashtable.c:9586)()

KeyError: 9.7

您使用的是哪個版本的熊貓?

根據文件

Float64Index

注意從0.14.0開始,Float64Index由本機float64 dtype數組支持。 在0.14.0之前,Float64Index由對象dtype數組支持。 在后端使用float64 dtype可將算術運算速度提高約30倍,而Float64Index本身的布爾索引操作速度提高約2倍。 版本0.13.0中的新功能。

默認情況下,在索引創建中傳遞浮點值或混合整數浮點值時,將自動創建Float64Index。 這使得基於標簽的純切片范例能夠使標量索引和切片的[],ix,loc完全相同。

暫無
暫無

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

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