test_lib.py 7.7 KB

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