From ae6300445ec65a7f1c646147d7091d2f43f89cef Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 21 Jan 2020 18:40:01 -0800 Subject: [PATCH] REF: combine IndexEngine test files --- pandas/tests/indexes/test_engines.py | 181 ++++++++++++++++++ pandas/tests/indexing/conftest.py | 23 --- .../tests/indexing/test_indexing_engines.py | 163 ---------------- 3 files changed, 181 insertions(+), 186 deletions(-) delete mode 100644 pandas/tests/indexing/conftest.py delete mode 100644 pandas/tests/indexing/test_indexing_engines.py diff --git a/pandas/tests/indexes/test_engines.py b/pandas/tests/indexes/test_engines.py index ee224c9c6ec89..9ea70a457e516 100644 --- a/pandas/tests/indexes/test_engines.py +++ b/pandas/tests/indexes/test_engines.py @@ -1,8 +1,31 @@ import re +import numpy as np import pytest +from pandas._libs import algos as libalgos, index as libindex + import pandas as pd +import pandas._testing as tm + + +@pytest.fixture( + params=[ + (libindex.Int64Engine, np.int64), + (libindex.Int32Engine, np.int32), + (libindex.Int16Engine, np.int16), + (libindex.Int8Engine, np.int8), + (libindex.UInt64Engine, np.uint64), + (libindex.UInt32Engine, np.uint32), + (libindex.UInt16Engine, np.uint16), + (libindex.UInt8Engine, np.uint8), + (libindex.Float64Engine, np.float64), + (libindex.Float32Engine, np.float32), + ], + ids=lambda x: x[0].__name__, +) +def numeric_indexing_engine_type_and_dtype(request): + return request.param class TestDatetimeEngine: @@ -55,3 +78,161 @@ def test_not_contains_requires_timestamp(self, scalar): with pytest.raises(KeyError, match=msg): tdi._engine.get_loc(scalar) + + +class TestNumericEngine: + def test_is_monotonic(self, numeric_indexing_engine_type_and_dtype): + engine_type, dtype = numeric_indexing_engine_type_and_dtype + num = 1000 + arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype) + + # monotonic increasing + engine = engine_type(lambda: arr, len(arr)) + assert engine.is_monotonic_increasing is True + assert engine.is_monotonic_decreasing is False + + # monotonic decreasing + engine = engine_type(lambda: arr[::-1], len(arr)) + assert engine.is_monotonic_increasing is False + assert engine.is_monotonic_decreasing is True + + # neither monotonic increasing or decreasing + arr = np.array([1] * num + [2] * num + [1] * num, dtype=dtype) + engine = engine_type(lambda: arr[::-1], len(arr)) + assert engine.is_monotonic_increasing is False + assert engine.is_monotonic_decreasing is False + + def test_is_unique(self, numeric_indexing_engine_type_and_dtype): + engine_type, dtype = numeric_indexing_engine_type_and_dtype + + # unique + arr = np.array([1, 3, 2], dtype=dtype) + engine = engine_type(lambda: arr, len(arr)) + assert engine.is_unique is True + + # not unique + arr = np.array([1, 2, 1], dtype=dtype) + engine = engine_type(lambda: arr, len(arr)) + assert engine.is_unique is False + + def test_get_loc(self, numeric_indexing_engine_type_and_dtype): + engine_type, dtype = numeric_indexing_engine_type_and_dtype + + # unique + arr = np.array([1, 2, 3], dtype=dtype) + engine = engine_type(lambda: arr, len(arr)) + assert engine.get_loc(2) == 1 + + # monotonic + num = 1000 + arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype) + engine = engine_type(lambda: arr, len(arr)) + assert engine.get_loc(2) == slice(1000, 2000) + + # not monotonic + arr = np.array([1, 2, 3] * num, dtype=dtype) + engine = engine_type(lambda: arr, len(arr)) + expected = np.array([False, True, False] * num, dtype=bool) + result = engine.get_loc(2) + assert (result == expected).all() + + def test_get_backfill_indexer(self, numeric_indexing_engine_type_and_dtype): + engine_type, dtype = numeric_indexing_engine_type_and_dtype + + arr = np.array([1, 5, 10], dtype=dtype) + engine = engine_type(lambda: arr, len(arr)) + + new = np.arange(12, dtype=dtype) + result = engine.get_backfill_indexer(new) + + expected = libalgos.backfill(arr, new) + tm.assert_numpy_array_equal(result, expected) + + def test_get_pad_indexer(self, numeric_indexing_engine_type_and_dtype): + engine_type, dtype = numeric_indexing_engine_type_and_dtype + + arr = np.array([1, 5, 10], dtype=dtype) + engine = engine_type(lambda: arr, len(arr)) + + new = np.arange(12, dtype=dtype) + result = engine.get_pad_indexer(new) + + expected = libalgos.pad(arr, new) + tm.assert_numpy_array_equal(result, expected) + + +class TestObjectEngine: + engine_type = libindex.ObjectEngine + dtype = np.object_ + values = list("abc") + + def test_is_monotonic(self): + + num = 1000 + arr = np.array(["a"] * num + ["a"] * num + ["c"] * num, dtype=self.dtype) + + # monotonic increasing + engine = self.engine_type(lambda: arr, len(arr)) + assert engine.is_monotonic_increasing is True + assert engine.is_monotonic_decreasing is False + + # monotonic decreasing + engine = self.engine_type(lambda: arr[::-1], len(arr)) + assert engine.is_monotonic_increasing is False + assert engine.is_monotonic_decreasing is True + + # neither monotonic increasing or decreasing + arr = np.array(["a"] * num + ["b"] * num + ["a"] * num, dtype=self.dtype) + engine = self.engine_type(lambda: arr[::-1], len(arr)) + assert engine.is_monotonic_increasing is False + assert engine.is_monotonic_decreasing is False + + def test_is_unique(self): + # unique + arr = np.array(self.values, dtype=self.dtype) + engine = self.engine_type(lambda: arr, len(arr)) + assert engine.is_unique is True + + # not unique + arr = np.array(["a", "b", "a"], dtype=self.dtype) + engine = self.engine_type(lambda: arr, len(arr)) + assert engine.is_unique is False + + def test_get_loc(self): + # unique + arr = np.array(self.values, dtype=self.dtype) + engine = self.engine_type(lambda: arr, len(arr)) + assert engine.get_loc("b") == 1 + + # monotonic + num = 1000 + arr = np.array(["a"] * num + ["b"] * num + ["c"] * num, dtype=self.dtype) + engine = self.engine_type(lambda: arr, len(arr)) + assert engine.get_loc("b") == slice(1000, 2000) + + # not monotonic + arr = np.array(self.values * num, dtype=self.dtype) + engine = self.engine_type(lambda: arr, len(arr)) + expected = np.array([False, True, False] * num, dtype=bool) + result = engine.get_loc("b") + assert (result == expected).all() + + def test_get_backfill_indexer(self): + arr = np.array(["a", "e", "j"], dtype=self.dtype) + engine = self.engine_type(lambda: arr, len(arr)) + + new = np.array(list("abcdefghij"), dtype=self.dtype) + result = engine.get_backfill_indexer(new) + + expected = libalgos.backfill["object"](arr, new) + tm.assert_numpy_array_equal(result, expected) + + def test_get_pad_indexer(self): + arr = np.array(["a", "e", "j"], dtype=self.dtype) + engine = self.engine_type(lambda: arr, len(arr)) + + new = np.array(list("abcdefghij"), dtype=self.dtype) + result = engine.get_pad_indexer(new) + + expected = libalgos.pad["object"](arr, new) + tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/indexing/conftest.py b/pandas/tests/indexing/conftest.py deleted file mode 100644 index 142bedaa943a6..0000000000000 --- a/pandas/tests/indexing/conftest.py +++ /dev/null @@ -1,23 +0,0 @@ -import numpy as np -import pytest - -from pandas._libs import index as libindex - - -@pytest.fixture( - params=[ - (libindex.Int64Engine, np.int64), - (libindex.Int32Engine, np.int32), - (libindex.Int16Engine, np.int16), - (libindex.Int8Engine, np.int8), - (libindex.UInt64Engine, np.uint64), - (libindex.UInt32Engine, np.uint32), - (libindex.UInt16Engine, np.uint16), - (libindex.UInt8Engine, np.uint8), - (libindex.Float64Engine, np.float64), - (libindex.Float32Engine, np.float32), - ], - ids=lambda x: x[0].__name__, -) -def numeric_indexing_engine_type_and_dtype(request): - return request.param diff --git a/pandas/tests/indexing/test_indexing_engines.py b/pandas/tests/indexing/test_indexing_engines.py deleted file mode 100644 index edb5d7d7f3a57..0000000000000 --- a/pandas/tests/indexing/test_indexing_engines.py +++ /dev/null @@ -1,163 +0,0 @@ -import numpy as np - -from pandas._libs import algos as libalgos, index as libindex - -import pandas._testing as tm - - -class TestNumericEngine: - def test_is_monotonic(self, numeric_indexing_engine_type_and_dtype): - engine_type, dtype = numeric_indexing_engine_type_and_dtype - num = 1000 - arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype) - - # monotonic increasing - engine = engine_type(lambda: arr, len(arr)) - assert engine.is_monotonic_increasing is True - assert engine.is_monotonic_decreasing is False - - # monotonic decreasing - engine = engine_type(lambda: arr[::-1], len(arr)) - assert engine.is_monotonic_increasing is False - assert engine.is_monotonic_decreasing is True - - # neither monotonic increasing or decreasing - arr = np.array([1] * num + [2] * num + [1] * num, dtype=dtype) - engine = engine_type(lambda: arr[::-1], len(arr)) - assert engine.is_monotonic_increasing is False - assert engine.is_monotonic_decreasing is False - - def test_is_unique(self, numeric_indexing_engine_type_and_dtype): - engine_type, dtype = numeric_indexing_engine_type_and_dtype - - # unique - arr = np.array([1, 3, 2], dtype=dtype) - engine = engine_type(lambda: arr, len(arr)) - assert engine.is_unique is True - - # not unique - arr = np.array([1, 2, 1], dtype=dtype) - engine = engine_type(lambda: arr, len(arr)) - assert engine.is_unique is False - - def test_get_loc(self, numeric_indexing_engine_type_and_dtype): - engine_type, dtype = numeric_indexing_engine_type_and_dtype - - # unique - arr = np.array([1, 2, 3], dtype=dtype) - engine = engine_type(lambda: arr, len(arr)) - assert engine.get_loc(2) == 1 - - # monotonic - num = 1000 - arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype) - engine = engine_type(lambda: arr, len(arr)) - assert engine.get_loc(2) == slice(1000, 2000) - - # not monotonic - arr = np.array([1, 2, 3] * num, dtype=dtype) - engine = engine_type(lambda: arr, len(arr)) - expected = np.array([False, True, False] * num, dtype=bool) - result = engine.get_loc(2) - assert (result == expected).all() - - def test_get_backfill_indexer(self, numeric_indexing_engine_type_and_dtype): - engine_type, dtype = numeric_indexing_engine_type_and_dtype - - arr = np.array([1, 5, 10], dtype=dtype) - engine = engine_type(lambda: arr, len(arr)) - - new = np.arange(12, dtype=dtype) - result = engine.get_backfill_indexer(new) - - expected = libalgos.backfill(arr, new) - tm.assert_numpy_array_equal(result, expected) - - def test_get_pad_indexer(self, numeric_indexing_engine_type_and_dtype): - engine_type, dtype = numeric_indexing_engine_type_and_dtype - - arr = np.array([1, 5, 10], dtype=dtype) - engine = engine_type(lambda: arr, len(arr)) - - new = np.arange(12, dtype=dtype) - result = engine.get_pad_indexer(new) - - expected = libalgos.pad(arr, new) - tm.assert_numpy_array_equal(result, expected) - - -class TestObjectEngine: - engine_type = libindex.ObjectEngine - dtype = np.object_ - values = list("abc") - - def test_is_monotonic(self): - - num = 1000 - arr = np.array(["a"] * num + ["a"] * num + ["c"] * num, dtype=self.dtype) - - # monotonic increasing - engine = self.engine_type(lambda: arr, len(arr)) - assert engine.is_monotonic_increasing is True - assert engine.is_monotonic_decreasing is False - - # monotonic decreasing - engine = self.engine_type(lambda: arr[::-1], len(arr)) - assert engine.is_monotonic_increasing is False - assert engine.is_monotonic_decreasing is True - - # neither monotonic increasing or decreasing - arr = np.array(["a"] * num + ["b"] * num + ["a"] * num, dtype=self.dtype) - engine = self.engine_type(lambda: arr[::-1], len(arr)) - assert engine.is_monotonic_increasing is False - assert engine.is_monotonic_decreasing is False - - def test_is_unique(self): - # unique - arr = np.array(self.values, dtype=self.dtype) - engine = self.engine_type(lambda: arr, len(arr)) - assert engine.is_unique is True - - # not unique - arr = np.array(["a", "b", "a"], dtype=self.dtype) - engine = self.engine_type(lambda: arr, len(arr)) - assert engine.is_unique is False - - def test_get_loc(self): - # unique - arr = np.array(self.values, dtype=self.dtype) - engine = self.engine_type(lambda: arr, len(arr)) - assert engine.get_loc("b") == 1 - - # monotonic - num = 1000 - arr = np.array(["a"] * num + ["b"] * num + ["c"] * num, dtype=self.dtype) - engine = self.engine_type(lambda: arr, len(arr)) - assert engine.get_loc("b") == slice(1000, 2000) - - # not monotonic - arr = np.array(self.values * num, dtype=self.dtype) - engine = self.engine_type(lambda: arr, len(arr)) - expected = np.array([False, True, False] * num, dtype=bool) - result = engine.get_loc("b") - assert (result == expected).all() - - def test_get_backfill_indexer(self): - arr = np.array(["a", "e", "j"], dtype=self.dtype) - engine = self.engine_type(lambda: arr, len(arr)) - - new = np.array(list("abcdefghij"), dtype=self.dtype) - result = engine.get_backfill_indexer(new) - - expected = libalgos.backfill["object"](arr, new) - tm.assert_numpy_array_equal(result, expected) - - def test_get_pad_indexer(self): - arr = np.array(["a", "e", "j"], dtype=self.dtype) - engine = self.engine_type(lambda: arr, len(arr)) - - new = np.array(list("abcdefghij"), dtype=self.dtype) - result = engine.get_pad_indexer(new) - - expected = libalgos.pad["object"](arr, new) - tm.assert_numpy_array_equal(result, expected)