test_indexing.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas import (
  5. DatetimeIndex,
  6. Index,
  7. )
  8. import pandas._testing as tm
  9. dtlike_dtypes = [
  10. np.dtype("timedelta64[ns]"),
  11. np.dtype("datetime64[ns]"),
  12. pd.DatetimeTZDtype("ns", "Asia/Tokyo"),
  13. pd.PeriodDtype("ns"),
  14. ]
  15. @pytest.mark.parametrize("ldtype", dtlike_dtypes)
  16. @pytest.mark.parametrize("rdtype", dtlike_dtypes)
  17. def test_get_indexer_non_unique_wrong_dtype(ldtype, rdtype):
  18. vals = np.tile(3600 * 10**9 * np.arange(3), 2)
  19. def construct(dtype):
  20. if dtype is dtlike_dtypes[-1]:
  21. # PeriodArray will try to cast ints to strings
  22. return DatetimeIndex(vals).astype(dtype)
  23. return Index(vals, dtype=dtype)
  24. left = construct(ldtype)
  25. right = construct(rdtype)
  26. result = left.get_indexer_non_unique(right)
  27. if ldtype is rdtype:
  28. ex1 = np.array([0, 3, 1, 4, 2, 5] * 2, dtype=np.intp)
  29. ex2 = np.array([], dtype=np.intp)
  30. tm.assert_numpy_array_equal(result[0], ex1)
  31. tm.assert_numpy_array_equal(result[1], ex2)
  32. else:
  33. no_matches = np.array([-1] * 6, dtype=np.intp)
  34. missing = np.arange(6, dtype=np.intp)
  35. tm.assert_numpy_array_equal(result[0], no_matches)
  36. tm.assert_numpy_array_equal(result[1], missing)