@@ -32,6 +32,106 @@ def test_index_construct_from_list():
3232 pd .testing .assert_index_equal (bf_result , pd_result )
3333
3434
35+ @pytest .mark .parametrize ("key, expected_loc" , [("a" , 0 ), ("b" , 1 ), ("c" , 2 )])
36+ def test_get_loc_should_return_int_for_unique_index (key , expected_loc ):
37+ """Behavior: get_loc on a unique index returns an integer position."""
38+ # The pandas result is used as the known-correct value.
39+ # We assert our implementation matches it and the expected type.
40+ bf_index = bpd .Index (["a" , "b" , "c" ])
41+
42+ result = bf_index .get_loc (key )
43+
44+ assert result == expected_loc
45+ assert isinstance (result , int )
46+
47+
48+ def test_get_loc_should_return_slice_for_monotonic_duplicates ():
49+ """Behavior: get_loc on a monotonic string index with duplicates returns a slice."""
50+ bf_index = bpd .Index (["a" , "b" , "b" , "c" ])
51+ pd_index = pd .Index (["a" , "b" , "b" , "c" ])
52+
53+ bf_result = bf_index .get_loc ("b" )
54+ pd_result = pd_index .get_loc ("b" )
55+
56+ assert isinstance (bf_result , slice )
57+ assert bf_result == pd_result # Should be slice(1, 3, None)
58+
59+
60+ def test_get_loc_should_return_slice_for_monotonic_numeric_duplicates ():
61+ """Behavior: get_loc on a monotonic numeric index with duplicates returns a slice."""
62+ bf_index = bpd .Index ([1 , 2 , 2 , 3 ])
63+ pd_index = pd .Index ([1 , 2 , 2 , 3 ])
64+
65+ bf_result = bf_index .get_loc (2 )
66+ pd_result = pd_index .get_loc (2 )
67+
68+ assert isinstance (bf_result , slice )
69+ assert bf_result == pd_result # Should be slice(1, 3, None)
70+
71+
72+ def test_get_loc_should_return_mask_for_non_monotonic_duplicates ():
73+ """Behavior: get_loc on a non-monotonic string index returns a boolean array."""
74+ bf_index = bpd .Index (["a" , "b" , "c" , "b" ])
75+ pd_index = pd .Index (["a" , "b" , "c" , "b" ])
76+
77+ bf_result = bf_index .get_loc ("b" )
78+ if hasattr (bf_result , "to_numpy" ):
79+ bf_array = bf_result .to_numpy ()
80+ else :
81+ bf_array = bf_result .to_pandas ().to_numpy ()
82+ pd_result = pd_index .get_loc ("b" )
83+
84+ numpy .testing .assert_array_equal (bf_array , pd_result )
85+
86+
87+ def test_get_loc_should_return_mask_for_non_monotonic_numeric_duplicates ():
88+ """Behavior: get_loc on a non-monotonic numeric index returns a boolean array."""
89+ bf_index = bpd .Index ([1 , 2 , 3 , 2 ])
90+ pd_index = pd .Index ([1 , 2 , 3 , 2 ])
91+
92+ bf_result = bf_index .get_loc (2 )
93+ if hasattr (bf_result , "to_numpy" ):
94+ bf_array = bf_result .to_numpy ()
95+ else :
96+ bf_array = bf_result .to_pandas ().to_numpy ()
97+ pd_result = pd_index .get_loc (2 )
98+
99+ numpy .testing .assert_array_equal (bf_array , pd_result )
100+
101+
102+ def test_get_loc_should_raise_error_for_missing_key ():
103+ """Behavior: get_loc raises KeyError when a string key is not found."""
104+ bf_index = bpd .Index (["a" , "b" , "c" ])
105+
106+ with pytest .raises (KeyError ):
107+ bf_index .get_loc ("d" )
108+
109+
110+ def test_get_loc_should_raise_error_for_missing_numeric_key ():
111+ """Behavior: get_loc raises KeyError when a numeric key is not found."""
112+ bf_index = bpd .Index ([1 , 2 , 3 ])
113+
114+ with pytest .raises (KeyError ):
115+ bf_index .get_loc (4 )
116+
117+
118+ def test_get_loc_should_work_for_single_element_index ():
119+ """Behavior: get_loc on a single-element index returns 0."""
120+ assert bpd .Index (["a" ]).get_loc ("a" ) == pd .Index (["a" ]).get_loc ("a" )
121+
122+
123+ def test_get_loc_should_return_slice_when_all_elements_are_duplicates ():
124+ """Behavior: get_loc returns a full slice if all elements match the key."""
125+ bf_index = bpd .Index (["a" , "a" , "a" ])
126+ pd_index = pd .Index (["a" , "a" , "a" ])
127+
128+ bf_result = bf_index .get_loc ("a" )
129+ pd_result = pd_index .get_loc ("a" )
130+
131+ assert isinstance (bf_result , slice )
132+ assert bf_result == pd_result # Should be slice(0, 3, None)
133+
134+
35135def test_index_construct_from_series ():
36136 bf_result = bpd .Index (
37137 bpd .Series ([3 , 14 , 159 ], dtype = pd .Float64Dtype (), name = "series_name" ),
0 commit comments