test_indexing.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import numpy as np
  2. from pandas.core.dtypes.common import is_scalar
  3. import pandas as pd
  4. import pandas._testing as tm
  5. class TestSearchsorted:
  6. def test_searchsorted_string(self, string_dtype):
  7. arr = pd.array(["a", "b", "c"], dtype=string_dtype)
  8. result = arr.searchsorted("a", side="left")
  9. assert is_scalar(result)
  10. assert result == 0
  11. result = arr.searchsorted("a", side="right")
  12. assert is_scalar(result)
  13. assert result == 1
  14. def test_searchsorted_numeric_dtypes_scalar(self, any_real_numpy_dtype):
  15. arr = pd.array([1, 3, 90], dtype=any_real_numpy_dtype)
  16. result = arr.searchsorted(30)
  17. assert is_scalar(result)
  18. assert result == 2
  19. result = arr.searchsorted([30])
  20. expected = np.array([2], dtype=np.intp)
  21. tm.assert_numpy_array_equal(result, expected)
  22. def test_searchsorted_numeric_dtypes_vector(self, any_real_numpy_dtype):
  23. arr = pd.array([1, 3, 90], dtype=any_real_numpy_dtype)
  24. result = arr.searchsorted([2, 30])
  25. expected = np.array([1, 2], dtype=np.intp)
  26. tm.assert_numpy_array_equal(result, expected)
  27. def test_searchsorted_sorter(self, any_real_numpy_dtype):
  28. arr = pd.array([3, 1, 2], dtype=any_real_numpy_dtype)
  29. result = arr.searchsorted([0, 3], sorter=np.argsort(arr))
  30. expected = np.array([0, 2], dtype=np.intp)
  31. tm.assert_numpy_array_equal(result, expected)