test_lib.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import numpy as np
  2. import pytest
  3. from pandas._libs import lib, writers as libwriters
  4. from pandas import Index
  5. import pandas._testing as tm
  6. class TestMisc:
  7. def test_max_len_string_array(self):
  8. arr = a = np.array(["foo", "b", np.nan], dtype="object")
  9. assert libwriters.max_len_string_array(arr) == 3
  10. # unicode
  11. arr = a.astype("U").astype(object)
  12. assert libwriters.max_len_string_array(arr) == 3
  13. # bytes for python3
  14. arr = a.astype("S").astype(object)
  15. assert libwriters.max_len_string_array(arr) == 3
  16. # raises
  17. with pytest.raises(TypeError):
  18. libwriters.max_len_string_array(arr.astype("U"))
  19. def test_fast_unique_multiple_list_gen_sort(self):
  20. keys = [["p", "a"], ["n", "d"], ["a", "s"]]
  21. gen = (key for key in keys)
  22. expected = np.array(["a", "d", "n", "p", "s"])
  23. out = lib.fast_unique_multiple_list_gen(gen, sort=True)
  24. tm.assert_numpy_array_equal(np.array(out), expected)
  25. gen = (key for key in keys)
  26. expected = np.array(["p", "a", "n", "d", "s"])
  27. out = lib.fast_unique_multiple_list_gen(gen, sort=False)
  28. tm.assert_numpy_array_equal(np.array(out), expected)
  29. class TestIndexing:
  30. def test_maybe_indices_to_slice_left_edge(self):
  31. target = np.arange(100)
  32. # slice
  33. indices = np.array([], dtype=np.int64)
  34. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  35. assert isinstance(maybe_slice, slice)
  36. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  37. for end in [1, 2, 5, 20, 99]:
  38. for step in [1, 2, 4]:
  39. indices = np.arange(0, end, step, dtype=np.int64)
  40. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  41. assert isinstance(maybe_slice, slice)
  42. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  43. # reverse
  44. indices = indices[::-1]
  45. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  46. assert isinstance(maybe_slice, slice)
  47. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  48. # not slice
  49. for case in [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2], [2, 0, -2]]:
  50. indices = np.array(case, dtype=np.int64)
  51. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  52. assert not isinstance(maybe_slice, slice)
  53. tm.assert_numpy_array_equal(maybe_slice, indices)
  54. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  55. def test_maybe_indices_to_slice_right_edge(self):
  56. target = np.arange(100)
  57. # slice
  58. for start in [0, 2, 5, 20, 97, 98]:
  59. for step in [1, 2, 4]:
  60. indices = np.arange(start, 99, step, dtype=np.int64)
  61. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  62. assert isinstance(maybe_slice, slice)
  63. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  64. # reverse
  65. indices = indices[::-1]
  66. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  67. assert isinstance(maybe_slice, slice)
  68. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  69. # not slice
  70. indices = np.array([97, 98, 99, 100], dtype=np.int64)
  71. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  72. assert not isinstance(maybe_slice, slice)
  73. tm.assert_numpy_array_equal(maybe_slice, indices)
  74. with pytest.raises(IndexError):
  75. target[indices]
  76. with pytest.raises(IndexError):
  77. target[maybe_slice]
  78. indices = np.array([100, 99, 98, 97], dtype=np.int64)
  79. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  80. assert not isinstance(maybe_slice, slice)
  81. tm.assert_numpy_array_equal(maybe_slice, indices)
  82. with pytest.raises(IndexError):
  83. target[indices]
  84. with pytest.raises(IndexError):
  85. target[maybe_slice]
  86. for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]:
  87. indices = np.array(case, dtype=np.int64)
  88. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  89. assert not isinstance(maybe_slice, slice)
  90. tm.assert_numpy_array_equal(maybe_slice, indices)
  91. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  92. def test_maybe_indices_to_slice_both_edges(self):
  93. target = np.arange(10)
  94. # slice
  95. for step in [1, 2, 4, 5, 8, 9]:
  96. indices = np.arange(0, 9, step, dtype=np.int64)
  97. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  98. assert isinstance(maybe_slice, slice)
  99. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  100. # reverse
  101. indices = indices[::-1]
  102. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  103. assert isinstance(maybe_slice, slice)
  104. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  105. # not slice
  106. for case in [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]]:
  107. indices = np.array(case, dtype=np.int64)
  108. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  109. assert not isinstance(maybe_slice, slice)
  110. tm.assert_numpy_array_equal(maybe_slice, indices)
  111. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  112. def test_maybe_indices_to_slice_middle(self):
  113. target = np.arange(100)
  114. # slice
  115. for start, end in [(2, 10), (5, 25), (65, 97)]:
  116. for step in [1, 2, 4, 20]:
  117. indices = np.arange(start, end, step, dtype=np.int64)
  118. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  119. assert isinstance(maybe_slice, slice)
  120. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  121. # reverse
  122. indices = indices[::-1]
  123. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  124. assert isinstance(maybe_slice, slice)
  125. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  126. # not slice
  127. for case in [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]:
  128. indices = np.array(case, dtype=np.int64)
  129. maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
  130. assert not isinstance(maybe_slice, slice)
  131. tm.assert_numpy_array_equal(maybe_slice, indices)
  132. tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
  133. def test_maybe_booleans_to_slice(self):
  134. arr = np.array([0, 0, 1, 1, 1, 0, 1], dtype=np.uint8)
  135. result = lib.maybe_booleans_to_slice(arr)
  136. assert result.dtype == np.bool_
  137. result = lib.maybe_booleans_to_slice(arr[:0])
  138. assert result == slice(0, 0)
  139. def test_get_reverse_indexer(self):
  140. indexer = np.array([-1, -1, 1, 2, 0, -1, 3, 4], dtype=np.int64)
  141. result = lib.get_reverse_indexer(indexer, 5)
  142. expected = np.array([4, 2, 3, 6, 7], dtype=np.int64)
  143. tm.assert_numpy_array_equal(result, expected)
  144. def test_cache_readonly_preserve_docstrings():
  145. # GH18197
  146. assert Index.hasnans.__doc__ is not None