test_factorize.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import numpy as np
  2. from pandas import (
  3. PeriodIndex,
  4. factorize,
  5. )
  6. import pandas._testing as tm
  7. class TestFactorize:
  8. def test_factorize(self):
  9. idx1 = PeriodIndex(
  10. ["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"], freq="M"
  11. )
  12. exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
  13. exp_idx = PeriodIndex(["2014-01", "2014-02", "2014-03"], freq="M")
  14. arr, idx = idx1.factorize()
  15. tm.assert_numpy_array_equal(arr, exp_arr)
  16. tm.assert_index_equal(idx, exp_idx)
  17. arr, idx = idx1.factorize(sort=True)
  18. tm.assert_numpy_array_equal(arr, exp_arr)
  19. tm.assert_index_equal(idx, exp_idx)
  20. idx2 = PeriodIndex(
  21. ["2014-03", "2014-03", "2014-02", "2014-01", "2014-03", "2014-01"], freq="M"
  22. )
  23. exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
  24. arr, idx = idx2.factorize(sort=True)
  25. tm.assert_numpy_array_equal(arr, exp_arr)
  26. tm.assert_index_equal(idx, exp_idx)
  27. exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
  28. exp_idx = PeriodIndex(["2014-03", "2014-02", "2014-01"], freq="M")
  29. arr, idx = idx2.factorize()
  30. tm.assert_numpy_array_equal(arr, exp_arr)
  31. tm.assert_index_equal(idx, exp_idx)
  32. def test_factorize_complex(self):
  33. # GH 17927
  34. array = [1, 2, 2 + 1j]
  35. labels, uniques = factorize(array)
  36. expected_labels = np.array([0, 1, 2], dtype=np.intp)
  37. tm.assert_numpy_array_equal(labels, expected_labels)
  38. # Should return a complex dtype in the future
  39. expected_uniques = np.array([(1 + 0j), (2 + 0j), (2 + 1j)], dtype=object)
  40. tm.assert_numpy_array_equal(uniques, expected_uniques)