python - Pandas Series float 索引

标签 python indexing pandas series

我正在尝试使用 float 作为 pandas 系列的索引,但它似乎无法正常工作。例如,在下面的代码中,当我尝试调用值 policy[9.7] 时,它返回错误。我认为这是因为 float 9.7 在计算机中没有准确存储为 9.7(例如 9.7000000001)。无论如何要解决这个问题?或者使用 float 索引根本不是一个好主意?

非常感谢任何输入。谢谢!

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

最佳答案

您使用的是哪个版本的 Pandas?

根据 documents :

Float64Index

Note As of 0.14.0, Float64Index is backed by a native float64 dtype array. Prior to 0.14.0, Float64Index was backed by an object dtype array. Using a float64 dtype in the backend speeds up arithmetic operations by about 30x and boolean indexing operations on the Float64Index itself are about 2x as fast. New in version 0.13.0.

By default a Float64Index will be automatically created when passing floating, or mixed-integer-floating values in index creation. This enables a pure label-based slicing paradigm that makes [],ix,loc for scalar indexing and slicing work exactly the same.

关于python - Pandas Series float 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29237678/

相关文章:

python ctypes指针算法

python - 如何使用 .loc 和 MultiIndex 获取 Pandas DataFrame 中的值?

Python:如何检查值列表是否包含在某个范围内

python - 在 pandas 中使用索引来绘制数据

python - 如何检查绑定(bind)方法是否与未绑定(bind)方法相同

python - 我该怎么做才能知道 Beautifulsoup 中家长的类(class)名称?

python - SSD 和 SSD Lite 有什么区别??(Tensorflow)

statistics - SQL Server Index Usage Stats 多久更新一次,是什么触发了它?

python - 使用数组索引将 2D 数组函数应用于 3D 数组

python - 我们如何在列的每个值中对 pandas 数据框进行二次采样