@@ -63,6 +63,34 @@ def f():
6363
6464 pytest .raises (ValueError , f )
6565
66+ def test_inf_upcast (self ):
67+ # GH 16957
68+ # We should be able to use np.inf as a key
69+ # np.inf should cause an index to convert to float
70+
71+ # Test with np.inf in rows
72+ df = pd .DataFrame (columns = [0 ])
73+ df .loc [1 ] = 1
74+ df .loc [2 ] = 2
75+ df .loc [np .inf ] = 3
76+
77+ # make sure we can look up the value
78+ assert df .loc [np .inf , 0 ] == 3
79+
80+ result = df .index
81+ expected = pd .Float64Index ([1 , 2 , np .inf ])
82+ tm .assert_index_equal (result , expected )
83+
84+ # Test with np.inf in columns
85+ df = pd .DataFrame ()
86+ df .loc [0 , 0 ] = 1
87+ df .loc [1 , 1 ] = 2
88+ df .loc [0 , np .inf ] = 3
89+
90+ result = df .columns
91+ expected = pd .Float64Index ([0 , 1 , np .inf ])
92+ tm .assert_index_equal (result , expected )
93+
6694 def test_setitem_dtype_upcast (self ):
6795
6896 # GH3216
@@ -542,6 +570,34 @@ def test_astype_assignment_with_dups(self):
542570 # result = df.get_dtype_counts().sort_index()
543571 # expected = Series({'float64': 2, 'object': 1}).sort_index()
544572
573+ @pytest .mark .parametrize ("index,val" , [
574+ (pd .Index ([0 , 1 , 2 ]), 2 ),
575+ (pd .Index ([0 , 1 , '2' ]), '2' ),
576+ (pd .Index ([0 , 1 , 2 , np .inf , 4 ]), 4 ),
577+ (pd .Index ([0 , 1 , 2 , np .nan , 4 ]), 4 ),
578+ (pd .Index ([0 , 1 , 2 , np .inf ]), np .inf ),
579+ (pd .Index ([0 , 1 , 2 , np .nan ]), np .nan ),
580+ ])
581+ def test_index_contains (self , index , val ):
582+ assert val in index
583+
584+ @pytest .mark .parametrize ("index,val" , [
585+ (pd .Index ([0 , 1 , 2 ]), '2' ),
586+ (pd .Index ([0 , 1 , '2' ]), 2 ),
587+ (pd .Index ([0 , 1 , 2 , np .inf ]), 4 ),
588+ (pd .Index ([0 , 1 , 2 , np .nan ]), 4 ),
589+ (pd .Index ([0 , 1 , 2 , np .inf ]), np .nan ),
590+ (pd .Index ([0 , 1 , 2 , np .nan ]), np .inf ),
591+ # Checking if np.inf in Int64Index should not cause an OverflowError
592+ # Related to GH 16957
593+ (pd .Int64Index ([0 , 1 , 2 ]), np .inf ),
594+ (pd .Int64Index ([0 , 1 , 2 ]), np .nan ),
595+ (pd .UInt64Index ([0 , 1 , 2 ]), np .inf ),
596+ (pd .UInt64Index ([0 , 1 , 2 ]), np .nan ),
597+ ])
598+ def test_index_not_contains (self , index , val ):
599+ assert val not in index
600+
545601 def test_index_type_coercion (self ):
546602
547603 with catch_warnings (record = True ):
0 commit comments