File tree 4 files changed +48
-10
lines changed
4 files changed +48
-10
lines changed Original file line number Diff line number Diff line change @@ -490,3 +490,13 @@ def f(x):
490
490
f = mapper
491
491
492
492
return f
493
+
494
+
495
+ def ensure_python_int (value : Union [int , Any ]) -> int :
496
+ msg = "Wrong type {} for value {}"
497
+ try :
498
+ new_value = int (value )
499
+ assert (new_value == value )
500
+ except (TypeError , ValueError , AssertionError ):
501
+ raise TypeError (msg .format (type (value ), value ))
502
+ return new_value
Original file line number Diff line number Diff line change @@ -4013,11 +4013,7 @@ def __contains__(self, key):
4013
4013
4014
4014
@Appender (_index_shared_docs ['contains' ] % _index_doc_kwargs )
4015
4015
def contains (self , key ):
4016
- hash (key )
4017
- try :
4018
- return key in self ._engine
4019
- except (TypeError , ValueError ):
4020
- return False
4016
+ return key in self
4021
4017
4022
4018
def __hash__ (self ):
4023
4019
raise TypeError ("unhashable type: %r" % type (self ).__name__ )
Original file line number Diff line number Diff line change @@ -334,6 +334,14 @@ def is_monotonic_decreasing(self):
334
334
def has_duplicates (self ):
335
335
return False
336
336
337
+ def __contains__ (self , key ):
338
+ hash (key )
339
+ try :
340
+ key = com .ensure_python_int (key )
341
+ except TypeError :
342
+ return False
343
+ return key in self ._range
344
+
337
345
@Appender (_index_shared_docs ['get_loc' ])
338
346
def get_loc (self , key , method = None , tolerance = None ):
339
347
if is_integer (key ) and method is None and tolerance is None :
@@ -640,6 +648,14 @@ def __floordiv__(self, other):
640
648
return self ._simple_new (start , start + 1 , 1 , name = self .name )
641
649
return self ._int64index // other
642
650
651
+ def all (self ) -> bool :
652
+ if 0 in self ._range :
653
+ return False
654
+ return True
655
+
656
+ def any (self ) -> bool :
657
+ return any (self ._range )
658
+
643
659
@classmethod
644
660
def _add_numeric_methods_binary (cls ):
645
661
""" add in numeric methods, specialized to RangeIndex """
@@ -725,4 +741,3 @@ def _evaluate_numeric_binop(self, other):
725
741
726
742
727
743
RangeIndex ._add_numeric_methods ()
728
- RangeIndex ._add_logical_methods ()
Original file line number Diff line number Diff line change @@ -245,10 +245,9 @@ def test_dtype(self):
245
245
assert self .index .dtype == np .int64
246
246
247
247
def test_cached_data (self ):
248
- # GH 26565
249
- # Calling RangeIndex._data caches an int64 array of the same length as
250
- # self at self._cached_data.
251
- # This tests whether _cached_data is being set by various operations.
248
+ # GH 26565, GH26617
249
+ # Calling RangeIndex._data caches an int64 array of the same length at
250
+ # self._cached_data. This tests whether _cached_data has been set.
252
251
idx = RangeIndex (0 , 100 , 10 )
253
252
254
253
assert idx ._cached_data is None
@@ -262,6 +261,24 @@ def test_cached_data(self):
262
261
idx .get_loc (20 )
263
262
assert idx ._cached_data is None
264
263
264
+ 90 in idx
265
+ assert idx ._cached_data is None
266
+
267
+ 91 in idx
268
+ assert idx ._cached_data is None
269
+
270
+ idx .contains (90 )
271
+ assert idx ._cached_data is None
272
+
273
+ idx .contains (91 )
274
+ assert idx ._cached_data is None
275
+
276
+ idx .all ()
277
+ assert idx ._cached_data is None
278
+
279
+ idx .any ()
280
+ assert idx ._cached_data is None
281
+
265
282
df = pd .DataFrame ({'a' : range (10 )}, index = idx )
266
283
267
284
df .loc [50 ]
You can’t perform that action at this time.
0 commit comments