简体   繁体   中英

Pandas Series float index

I am trying to use float numbers as index for pandas Series, but it does not seem to work appropriately. For example, in the following code, when I try to call the value policy[9.7], it returns error. I believe this is due to the fact that the float number 9.7 is not stored precisely as 9.7 in the computer (eg, 9.7000000001). Is there anyway to get around this? Or it is simply not a good idea to use float index?

Any input is greatly appreciated. Thanks!

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

Which version of Pandas are you using?

Per the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM