Skip to content

Commit 399c413

Browse files
jbrockmendeljreback
authored andcommitted
REF: combine IndexEngine test files (#31193)
1 parent 08f2d64 commit 399c413

File tree

3 files changed

+181
-186
lines changed

3 files changed

+181
-186
lines changed

pandas/tests/indexes/test_engines.py

+181
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
import re
22

3+
import numpy as np
34
import pytest
45

6+
from pandas._libs import algos as libalgos, index as libindex
7+
58
import pandas as pd
9+
import pandas._testing as tm
10+
11+
12+
@pytest.fixture(
13+
params=[
14+
(libindex.Int64Engine, np.int64),
15+
(libindex.Int32Engine, np.int32),
16+
(libindex.Int16Engine, np.int16),
17+
(libindex.Int8Engine, np.int8),
18+
(libindex.UInt64Engine, np.uint64),
19+
(libindex.UInt32Engine, np.uint32),
20+
(libindex.UInt16Engine, np.uint16),
21+
(libindex.UInt8Engine, np.uint8),
22+
(libindex.Float64Engine, np.float64),
23+
(libindex.Float32Engine, np.float32),
24+
],
25+
ids=lambda x: x[0].__name__,
26+
)
27+
def numeric_indexing_engine_type_and_dtype(request):
28+
return request.param
629

730

831
class TestDatetimeEngine:
@@ -55,3 +78,161 @@ def test_not_contains_requires_timestamp(self, scalar):
5578

5679
with pytest.raises(KeyError, match=msg):
5780
tdi._engine.get_loc(scalar)
81+
82+
83+
class TestNumericEngine:
84+
def test_is_monotonic(self, numeric_indexing_engine_type_and_dtype):
85+
engine_type, dtype = numeric_indexing_engine_type_and_dtype
86+
num = 1000
87+
arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype)
88+
89+
# monotonic increasing
90+
engine = engine_type(lambda: arr, len(arr))
91+
assert engine.is_monotonic_increasing is True
92+
assert engine.is_monotonic_decreasing is False
93+
94+
# monotonic decreasing
95+
engine = engine_type(lambda: arr[::-1], len(arr))
96+
assert engine.is_monotonic_increasing is False
97+
assert engine.is_monotonic_decreasing is True
98+
99+
# neither monotonic increasing or decreasing
100+
arr = np.array([1] * num + [2] * num + [1] * num, dtype=dtype)
101+
engine = engine_type(lambda: arr[::-1], len(arr))
102+
assert engine.is_monotonic_increasing is False
103+
assert engine.is_monotonic_decreasing is False
104+
105+
def test_is_unique(self, numeric_indexing_engine_type_and_dtype):
106+
engine_type, dtype = numeric_indexing_engine_type_and_dtype
107+
108+
# unique
109+
arr = np.array([1, 3, 2], dtype=dtype)
110+
engine = engine_type(lambda: arr, len(arr))
111+
assert engine.is_unique is True
112+
113+
# not unique
114+
arr = np.array([1, 2, 1], dtype=dtype)
115+
engine = engine_type(lambda: arr, len(arr))
116+
assert engine.is_unique is False
117+
118+
def test_get_loc(self, numeric_indexing_engine_type_and_dtype):
119+
engine_type, dtype = numeric_indexing_engine_type_and_dtype
120+
121+
# unique
122+
arr = np.array([1, 2, 3], dtype=dtype)
123+
engine = engine_type(lambda: arr, len(arr))
124+
assert engine.get_loc(2) == 1
125+
126+
# monotonic
127+
num = 1000
128+
arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype)
129+
engine = engine_type(lambda: arr, len(arr))
130+
assert engine.get_loc(2) == slice(1000, 2000)
131+
132+
# not monotonic
133+
arr = np.array([1, 2, 3] * num, dtype=dtype)
134+
engine = engine_type(lambda: arr, len(arr))
135+
expected = np.array([False, True, False] * num, dtype=bool)
136+
result = engine.get_loc(2)
137+
assert (result == expected).all()
138+
139+
def test_get_backfill_indexer(self, numeric_indexing_engine_type_and_dtype):
140+
engine_type, dtype = numeric_indexing_engine_type_and_dtype
141+
142+
arr = np.array([1, 5, 10], dtype=dtype)
143+
engine = engine_type(lambda: arr, len(arr))
144+
145+
new = np.arange(12, dtype=dtype)
146+
result = engine.get_backfill_indexer(new)
147+
148+
expected = libalgos.backfill(arr, new)
149+
tm.assert_numpy_array_equal(result, expected)
150+
151+
def test_get_pad_indexer(self, numeric_indexing_engine_type_and_dtype):
152+
engine_type, dtype = numeric_indexing_engine_type_and_dtype
153+
154+
arr = np.array([1, 5, 10], dtype=dtype)
155+
engine = engine_type(lambda: arr, len(arr))
156+
157+
new = np.arange(12, dtype=dtype)
158+
result = engine.get_pad_indexer(new)
159+
160+
expected = libalgos.pad(arr, new)
161+
tm.assert_numpy_array_equal(result, expected)
162+
163+
164+
class TestObjectEngine:
165+
engine_type = libindex.ObjectEngine
166+
dtype = np.object_
167+
values = list("abc")
168+
169+
def test_is_monotonic(self):
170+
171+
num = 1000
172+
arr = np.array(["a"] * num + ["a"] * num + ["c"] * num, dtype=self.dtype)
173+
174+
# monotonic increasing
175+
engine = self.engine_type(lambda: arr, len(arr))
176+
assert engine.is_monotonic_increasing is True
177+
assert engine.is_monotonic_decreasing is False
178+
179+
# monotonic decreasing
180+
engine = self.engine_type(lambda: arr[::-1], len(arr))
181+
assert engine.is_monotonic_increasing is False
182+
assert engine.is_monotonic_decreasing is True
183+
184+
# neither monotonic increasing or decreasing
185+
arr = np.array(["a"] * num + ["b"] * num + ["a"] * num, dtype=self.dtype)
186+
engine = self.engine_type(lambda: arr[::-1], len(arr))
187+
assert engine.is_monotonic_increasing is False
188+
assert engine.is_monotonic_decreasing is False
189+
190+
def test_is_unique(self):
191+
# unique
192+
arr = np.array(self.values, dtype=self.dtype)
193+
engine = self.engine_type(lambda: arr, len(arr))
194+
assert engine.is_unique is True
195+
196+
# not unique
197+
arr = np.array(["a", "b", "a"], dtype=self.dtype)
198+
engine = self.engine_type(lambda: arr, len(arr))
199+
assert engine.is_unique is False
200+
201+
def test_get_loc(self):
202+
# unique
203+
arr = np.array(self.values, dtype=self.dtype)
204+
engine = self.engine_type(lambda: arr, len(arr))
205+
assert engine.get_loc("b") == 1
206+
207+
# monotonic
208+
num = 1000
209+
arr = np.array(["a"] * num + ["b"] * num + ["c"] * num, dtype=self.dtype)
210+
engine = self.engine_type(lambda: arr, len(arr))
211+
assert engine.get_loc("b") == slice(1000, 2000)
212+
213+
# not monotonic
214+
arr = np.array(self.values * num, dtype=self.dtype)
215+
engine = self.engine_type(lambda: arr, len(arr))
216+
expected = np.array([False, True, False] * num, dtype=bool)
217+
result = engine.get_loc("b")
218+
assert (result == expected).all()
219+
220+
def test_get_backfill_indexer(self):
221+
arr = np.array(["a", "e", "j"], dtype=self.dtype)
222+
engine = self.engine_type(lambda: arr, len(arr))
223+
224+
new = np.array(list("abcdefghij"), dtype=self.dtype)
225+
result = engine.get_backfill_indexer(new)
226+
227+
expected = libalgos.backfill["object"](arr, new)
228+
tm.assert_numpy_array_equal(result, expected)
229+
230+
def test_get_pad_indexer(self):
231+
arr = np.array(["a", "e", "j"], dtype=self.dtype)
232+
engine = self.engine_type(lambda: arr, len(arr))
233+
234+
new = np.array(list("abcdefghij"), dtype=self.dtype)
235+
result = engine.get_pad_indexer(new)
236+
237+
expected = libalgos.pad["object"](arr, new)
238+
tm.assert_numpy_array_equal(result, expected)

pandas/tests/indexing/conftest.py

-23
This file was deleted.

pandas/tests/indexing/test_indexing_engines.py

-163
This file was deleted.

0 commit comments

Comments
 (0)