test_indexing_engines.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import numpy as np
  2. from pandas._libs import algos as libalgos, index as libindex
  3. from pandas import compat
  4. import pandas.util.testing as tm
  5. class TestNumericEngine(object):
  6. def test_is_monotonic(self, numeric_indexing_engine_type_and_dtype):
  7. engine_type, dtype = numeric_indexing_engine_type_and_dtype
  8. num = 1000
  9. arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype)
  10. # monotonic increasing
  11. engine = engine_type(lambda: arr, len(arr))
  12. assert engine.is_monotonic_increasing is True
  13. assert engine.is_monotonic_decreasing is False
  14. # monotonic decreasing
  15. engine = engine_type(lambda: arr[::-1], len(arr))
  16. assert engine.is_monotonic_increasing is False
  17. assert engine.is_monotonic_decreasing is True
  18. # neither monotonic increasing or decreasing
  19. arr = np.array([1] * num + [2] * num + [1] * num, dtype=dtype)
  20. engine = engine_type(lambda: arr[::-1], len(arr))
  21. assert engine.is_monotonic_increasing is False
  22. assert engine.is_monotonic_decreasing is False
  23. def test_is_unique(self, numeric_indexing_engine_type_and_dtype):
  24. engine_type, dtype = numeric_indexing_engine_type_and_dtype
  25. # unique
  26. arr = np.array([1, 3, 2], dtype=dtype)
  27. engine = engine_type(lambda: arr, len(arr))
  28. assert engine.is_unique is True
  29. # not unique
  30. arr = np.array([1, 2, 1], dtype=dtype)
  31. engine = engine_type(lambda: arr, len(arr))
  32. assert engine.is_unique is False
  33. def test_get_loc(self, numeric_indexing_engine_type_and_dtype):
  34. engine_type, dtype = numeric_indexing_engine_type_and_dtype
  35. # unique
  36. arr = np.array([1, 2, 3], dtype=dtype)
  37. engine = engine_type(lambda: arr, len(arr))
  38. assert engine.get_loc(2) == 1
  39. # monotonic
  40. num = 1000
  41. arr = np.array([1] * num + [2] * num + [3] * num, dtype=dtype)
  42. engine = engine_type(lambda: arr, len(arr))
  43. assert engine.get_loc(2) == slice(1000, 2000)
  44. # not monotonic
  45. arr = np.array([1, 2, 3] * num, dtype=dtype)
  46. engine = engine_type(lambda: arr, len(arr))
  47. expected = np.array([False, True, False] * num, dtype=bool)
  48. result = engine.get_loc(2)
  49. assert (result == expected).all()
  50. def test_get_backfill_indexer(
  51. self, numeric_indexing_engine_type_and_dtype):
  52. engine_type, dtype = numeric_indexing_engine_type_and_dtype
  53. arr = np.array([1, 5, 10], dtype=dtype)
  54. engine = engine_type(lambda: arr, len(arr))
  55. new = np.array(compat.range(12), dtype=dtype)
  56. result = engine.get_backfill_indexer(new)
  57. expected = libalgos.backfill(arr, new)
  58. tm.assert_numpy_array_equal(result, expected)
  59. def test_get_pad_indexer(
  60. self, numeric_indexing_engine_type_and_dtype):
  61. engine_type, dtype = numeric_indexing_engine_type_and_dtype
  62. arr = np.array([1, 5, 10], dtype=dtype)
  63. engine = engine_type(lambda: arr, len(arr))
  64. new = np.array(compat.range(12), dtype=dtype)
  65. result = engine.get_pad_indexer(new)
  66. expected = libalgos.pad(arr, new)
  67. tm.assert_numpy_array_equal(result, expected)
  68. class TestObjectEngine(object):
  69. engine_type = libindex.ObjectEngine
  70. dtype = np.object_
  71. values = list('abc')
  72. def test_is_monotonic(self):
  73. num = 1000
  74. arr = np.array(['a'] * num + ['a'] * num + ['c'] * num,
  75. dtype=self.dtype)
  76. # monotonic increasing
  77. engine = self.engine_type(lambda: arr, len(arr))
  78. assert engine.is_monotonic_increasing is True
  79. assert engine.is_monotonic_decreasing is False
  80. # monotonic decreasing
  81. engine = self.engine_type(lambda: arr[::-1], len(arr))
  82. assert engine.is_monotonic_increasing is False
  83. assert engine.is_monotonic_decreasing is True
  84. # neither monotonic increasing or decreasing
  85. arr = np.array(['a'] * num + ['b'] * num + ['a'] * num,
  86. dtype=self.dtype)
  87. engine = self.engine_type(lambda: arr[::-1], len(arr))
  88. assert engine.is_monotonic_increasing is False
  89. assert engine.is_monotonic_decreasing is False
  90. def test_is_unique(self):
  91. # unique
  92. arr = np.array(self.values, dtype=self.dtype)
  93. engine = self.engine_type(lambda: arr, len(arr))
  94. assert engine.is_unique is True
  95. # not unique
  96. arr = np.array(['a', 'b', 'a'], dtype=self.dtype)
  97. engine = self.engine_type(lambda: arr, len(arr))
  98. assert engine.is_unique is False
  99. def test_get_loc(self):
  100. # unique
  101. arr = np.array(self.values, dtype=self.dtype)
  102. engine = self.engine_type(lambda: arr, len(arr))
  103. assert engine.get_loc('b') == 1
  104. # monotonic
  105. num = 1000
  106. arr = np.array(['a'] * num + ['b'] * num + ['c'] * num,
  107. dtype=self.dtype)
  108. engine = self.engine_type(lambda: arr, len(arr))
  109. assert engine.get_loc('b') == slice(1000, 2000)
  110. # not monotonic
  111. arr = np.array(self.values * num, dtype=self.dtype)
  112. engine = self.engine_type(lambda: arr, len(arr))
  113. expected = np.array([False, True, False] * num, dtype=bool)
  114. result = engine.get_loc('b')
  115. assert (result == expected).all()
  116. def test_get_backfill_indexer(self):
  117. arr = np.array(['a', 'e', 'j'], dtype=self.dtype)
  118. engine = self.engine_type(lambda: arr, len(arr))
  119. new = np.array(list('abcdefghij'), dtype=self.dtype)
  120. result = engine.get_backfill_indexer(new)
  121. expected = libalgos.backfill["object"](arr, new)
  122. tm.assert_numpy_array_equal(result, expected)
  123. def test_get_pad_indexer(self):
  124. arr = np.array(['a', 'e', 'j'], dtype=self.dtype)
  125. engine = self.engine_type(lambda: arr, len(arr))
  126. new = np.array(list('abcdefghij'), dtype=self.dtype)
  127. result = engine.get_pad_indexer(new)
  128. expected = libalgos.pad["object"](arr, new)
  129. tm.assert_numpy_array_equal(result, expected)