3
3
import numpy as np
4
4
import pytest
5
5
6
- from pandas ._libs .missing import (
7
- NA ,
8
- is_matching_na ,
9
- )
6
+ from pandas ._libs .missing import is_matching_na
10
7
11
- import pandas as pd
12
8
from pandas import Index
13
9
import pandas ._testing as tm
14
10
@@ -22,13 +18,14 @@ class TestGetIndexer:
22
18
],
23
19
)
24
20
def test_get_indexer_strings (self , method , expected ):
25
- index = Index (["b" , "c" ])
21
+ expected = np .array (expected , dtype = np .intp )
22
+ index = Index (["b" , "c" ], dtype = object )
26
23
actual = index .get_indexer (["a" , "b" , "c" , "d" ], method = method )
27
24
28
25
tm .assert_numpy_array_equal (actual , expected )
29
26
30
- def test_get_indexer_strings_raises (self , using_infer_string ):
31
- index = Index (["b" , "c" ])
27
+ def test_get_indexer_strings_raises (self ):
28
+ index = Index (["b" , "c" ], dtype = object )
32
29
33
30
msg = "|" .join (
34
31
[
@@ -67,13 +64,9 @@ def test_get_indexer_with_NA_values(
67
64
68
65
69
66
class TestGetIndexerNonUnique :
70
- def test_get_indexer_non_unique_nas (
71
- self , nulls_fixture , request , using_infer_string
72
- ):
67
+ def test_get_indexer_non_unique_nas (self , nulls_fixture ):
73
68
# even though this isn't non-unique, this should still work
74
- if using_infer_string and (nulls_fixture is None or nulls_fixture is NA ):
75
- request .applymarker (pytest .mark .xfail (reason = "NAs are cast to NaN" ))
76
- index = Index (["a" , "b" , nulls_fixture ])
69
+ index = Index (["a" , "b" , nulls_fixture ], dtype = object )
77
70
indexer , missing = index .get_indexer_non_unique ([nulls_fixture ])
78
71
79
72
expected_indexer = np .array ([2 ], dtype = np .intp )
@@ -82,7 +75,7 @@ def test_get_indexer_non_unique_nas(
82
75
tm .assert_numpy_array_equal (missing , expected_missing )
83
76
84
77
# actually non-unique
85
- index = Index (["a" , nulls_fixture , "b" , nulls_fixture ])
78
+ index = Index (["a" , nulls_fixture , "b" , nulls_fixture ], dtype = object )
86
79
indexer , missing = index .get_indexer_non_unique ([nulls_fixture ])
87
80
88
81
expected_indexer = np .array ([1 , 3 ], dtype = np .intp )
@@ -91,10 +84,10 @@ def test_get_indexer_non_unique_nas(
91
84
92
85
# matching-but-not-identical nans
93
86
if is_matching_na (nulls_fixture , float ("NaN" )):
94
- index = Index (["a" , float ("NaN" ), "b" , float ("NaN" )])
87
+ index = Index (["a" , float ("NaN" ), "b" , float ("NaN" )], dtype = object )
95
88
match_but_not_identical = True
96
89
elif is_matching_na (nulls_fixture , Decimal ("NaN" )):
97
- index = Index (["a" , Decimal ("NaN" ), "b" , Decimal ("NaN" )])
90
+ index = Index (["a" , Decimal ("NaN" ), "b" , Decimal ("NaN" )], dtype = object )
98
91
match_but_not_identical = True
99
92
else :
100
93
match_but_not_identical = False
@@ -155,59 +148,3 @@ def test_get_indexer_non_unique_np_nats(self, np_nat_fixture, np_nat_fixture2):
155
148
expected_indexer = np .array ([1 , 3 ], dtype = np .intp )
156
149
tm .assert_numpy_array_equal (indexer , expected_indexer )
157
150
tm .assert_numpy_array_equal (missing , expected_missing )
158
-
159
-
160
- class TestSliceLocs :
161
- @pytest .mark .parametrize (
162
- "in_slice,expected" ,
163
- [
164
- # error: Slice index must be an integer or None
165
- (pd .IndexSlice [::- 1 ], "yxdcb" ),
166
- (pd .IndexSlice ["b" :"y" :- 1 ], "" ), # type: ignore[misc]
167
- (pd .IndexSlice ["b" ::- 1 ], "b" ), # type: ignore[misc]
168
- (pd .IndexSlice [:"b" :- 1 ], "yxdcb" ), # type: ignore[misc]
169
- (pd .IndexSlice [:"y" :- 1 ], "y" ), # type: ignore[misc]
170
- (pd .IndexSlice ["y" ::- 1 ], "yxdcb" ), # type: ignore[misc]
171
- (pd .IndexSlice ["y" ::- 4 ], "yb" ), # type: ignore[misc]
172
- # absent labels
173
- (pd .IndexSlice [:"a" :- 1 ], "yxdcb" ), # type: ignore[misc]
174
- (pd .IndexSlice [:"a" :- 2 ], "ydb" ), # type: ignore[misc]
175
- (pd .IndexSlice ["z" ::- 1 ], "yxdcb" ), # type: ignore[misc]
176
- (pd .IndexSlice ["z" ::- 3 ], "yc" ), # type: ignore[misc]
177
- (pd .IndexSlice ["m" ::- 1 ], "dcb" ), # type: ignore[misc]
178
- (pd .IndexSlice [:"m" :- 1 ], "yx" ), # type: ignore[misc]
179
- (pd .IndexSlice ["a" :"a" :- 1 ], "" ), # type: ignore[misc]
180
- (pd .IndexSlice ["z" :"z" :- 1 ], "" ), # type: ignore[misc]
181
- (pd .IndexSlice ["m" :"m" :- 1 ], "" ), # type: ignore[misc]
182
- ],
183
- )
184
- def test_slice_locs_negative_step (self , in_slice , expected , any_string_dtype ):
185
- index = Index (list ("bcdxy" ), dtype = any_string_dtype )
186
-
187
- s_start , s_stop = index .slice_locs (in_slice .start , in_slice .stop , in_slice .step )
188
- result = index [s_start : s_stop : in_slice .step ]
189
- expected = Index (list (expected ), dtype = any_string_dtype )
190
- tm .assert_index_equal (result , expected )
191
-
192
- def test_slice_locs_negative_step_oob (self , any_string_dtype ):
193
- index = Index (list ("bcdxy" ), dtype = any_string_dtype )
194
-
195
- result = index [- 10 :5 :1 ]
196
- tm .assert_index_equal (result , index )
197
-
198
- result = index [4 :- 10 :- 1 ]
199
- expected = Index (list ("yxdcb" ), dtype = any_string_dtype )
200
- tm .assert_index_equal (result , expected )
201
-
202
- def test_slice_locs_dup (self ):
203
- index = Index (["a" , "a" , "b" , "c" , "d" , "d" ])
204
- assert index .slice_locs ("a" , "d" ) == (0 , 6 )
205
- assert index .slice_locs (end = "d" ) == (0 , 6 )
206
- assert index .slice_locs ("a" , "c" ) == (0 , 4 )
207
- assert index .slice_locs ("b" , "d" ) == (2 , 6 )
208
-
209
- index2 = index [::- 1 ]
210
- assert index2 .slice_locs ("d" , "a" ) == (0 , 6 )
211
- assert index2 .slice_locs (end = "a" ) == (0 , 6 )
212
- assert index2 .slice_locs ("d" , "b" ) == (0 , 4 )
213
- assert index2 .slice_locs ("c" , "a" ) == (2 , 6 )
0 commit comments