16
16
from pandas .util .decorators import cache_readonly , deprecate
17
17
from pandas .core .common import isnull , array_equivalent
18
18
import pandas .core .common as com
19
- from pandas .core .common import _values_from_object , is_float , is_integer , ABCSeries
19
+ from pandas .core .common import (_values_from_object , is_float , is_integer ,
20
+ ABCSeries )
20
21
from pandas .core .config import get_option
21
22
22
23
# simplify
27
28
__all__ = ['Index' ]
28
29
29
30
31
+ def _try_get_item (x ):
32
+ try :
33
+ return x .item ()
34
+ except AttributeError :
35
+ return x
36
+
37
+
30
38
def _indexOp (opname ):
31
39
"""
32
40
Wrapper function for index comparison operations, to avoid
@@ -1911,11 +1919,17 @@ class Float64Index(Index):
1911
1919
1912
1920
Notes
1913
1921
-----
1914
- An Index instance can **only** contain hashable objects
1922
+ An Float64Index instance can **only** contain hashable objects
1915
1923
"""
1916
1924
1917
1925
# when this is not longer object dtype this can be changed
1918
- #_engine_type = _index.Float64Engine
1926
+ _engine_type = _index .Float64Engine
1927
+ _groupby = _algos .groupby_float64
1928
+ _arrmap = _algos .arrmap_float64
1929
+ _left_indexer_unique = _algos .left_join_indexer_unique_float64
1930
+ _left_indexer = _algos .left_join_indexer_float64
1931
+ _inner_indexer = _algos .inner_join_indexer_float64
1932
+ _outer_indexer = _algos .outer_join_indexer_float64
1919
1933
1920
1934
def __new__ (cls , data , dtype = None , copy = False , name = None , fastpath = False ):
1921
1935
@@ -1938,9 +1952,9 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False):
1938
1952
raise TypeError ('Unsafe NumPy casting, you must '
1939
1953
'explicitly cast' )
1940
1954
1941
- # coerce to object for storage
1942
- if not subarr .dtype == np .object_ :
1943
- subarr = subarr .astype (object )
1955
+ # coerce to float64 for storage
1956
+ if subarr .dtype != np .float64 :
1957
+ subarr = subarr .astype (np . float64 )
1944
1958
1945
1959
subarr = subarr .view (cls )
1946
1960
subarr .name = name
@@ -1951,13 +1965,12 @@ def inferred_type(self):
1951
1965
return 'floating'
1952
1966
1953
1967
def astype (self , dtype ):
1954
- if np .dtype (dtype ) != np .object_ :
1955
- raise TypeError ('Setting %s dtype to anything other than object '
1956
- 'is not supported' % self .__class__ )
1957
- return Index (self .values , name = self .name , dtype = object )
1968
+ if np .dtype (dtype ) not in ( np .object , np . float64 ) :
1969
+ raise TypeError ('Setting %s dtype to anything other than '
1970
+ 'float64 or object is not supported' % self .__class__ )
1971
+ return Index (self .values , name = self .name , dtype = dtype )
1958
1972
1959
1973
def _convert_scalar_indexer (self , key , typ = None ):
1960
-
1961
1974
if typ == 'iloc' :
1962
1975
return super (Float64Index , self )._convert_scalar_indexer (key ,
1963
1976
typ = typ )
@@ -1968,8 +1981,6 @@ def _convert_slice_indexer(self, key, typ=None):
1968
1981
unless we are iloc """
1969
1982
if typ == 'iloc' :
1970
1983
return self ._convert_slice_indexer_iloc (key )
1971
- elif typ == 'getitem' :
1972
- pass
1973
1984
1974
1985
# allow floats here
1975
1986
self ._validate_slicer (
@@ -2008,13 +2019,75 @@ def equals(self, other):
2008
2019
try :
2009
2020
if not isinstance (other , Float64Index ):
2010
2021
other = self ._constructor (other )
2011
- if self .dtype != other .dtype or self .shape != other .shape : return False
2022
+ if self .dtype != other .dtype or self .shape != other .shape :
2023
+ return False
2012
2024
left , right = self .values , other .values
2013
- return ((left == right ) | (isnull ( left ) & isnull ( right ) )).all ()
2025
+ return ((left == right ) | (self . _isnan & other . _isnan )).all ()
2014
2026
except TypeError :
2015
2027
# e.g. fails in numpy 1.6 with DatetimeIndex #1681
2016
2028
return False
2017
2029
2030
+ def __contains__ (self , other ):
2031
+ if super (Float64Index , self ).__contains__ (other ):
2032
+ return True
2033
+
2034
+ try :
2035
+ # if other is a sequence this throws a ValueError
2036
+ return np .isnan (other ) and self ._hasnans
2037
+ except ValueError :
2038
+ try :
2039
+ return len (other ) <= 1 and _try_get_item (other ) in self
2040
+ except TypeError :
2041
+ return False
2042
+
2043
+ def get_loc (self , key ):
2044
+ if np .isnan (key ):
2045
+ try :
2046
+ return self ._nan_idxs .item ()
2047
+ except ValueError :
2048
+ return self ._nan_idxs
2049
+ return super (Float64Index , self ).get_loc (key )
2050
+
2051
+ @property
2052
+ def is_all_dates (self ):
2053
+ """
2054
+ Checks that all the labels are datetime objects
2055
+ """
2056
+ return False
2057
+
2058
+ @cache_readonly
2059
+ def _nan_idxs (self ):
2060
+ w , = self ._isnan .nonzero ()
2061
+ return w
2062
+
2063
+ @cache_readonly
2064
+ def _isnan (self ):
2065
+ return np .isnan (self .values )
2066
+
2067
+ @cache_readonly
2068
+ def _hasnans (self ):
2069
+ return self ._isnan .any ()
2070
+
2071
+ @cache_readonly
2072
+ def is_unique (self ):
2073
+ return super (Float64Index , self ).is_unique and self ._nan_idxs .size < 2
2074
+
2075
+ def isin (self , values ):
2076
+ """
2077
+ Compute boolean array of whether each index value is found in the
2078
+ passed set of values
2079
+
2080
+ Parameters
2081
+ ----------
2082
+ values : set or sequence of values
2083
+
2084
+ Returns
2085
+ -------
2086
+ is_contained : ndarray (boolean dtype)
2087
+ """
2088
+ value_set = set (values )
2089
+ return lib .ismember_nans (self ._array_values (), value_set ,
2090
+ self ._hasnans )
2018
2091
2019
2092
class MultiIndex (Index ):
2020
2093
0 commit comments