test_reindex.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. Categorical,
  5. CategoricalIndex,
  6. Index,
  7. Interval,
  8. )
  9. import pandas._testing as tm
  10. class TestReindex:
  11. def test_reindex_list_non_unique(self):
  12. # GH#11586
  13. msg = "cannot reindex on an axis with duplicate labels"
  14. ci = CategoricalIndex(["a", "b", "c", "a"])
  15. with pytest.raises(ValueError, match=msg):
  16. ci.reindex(["a", "c"])
  17. def test_reindex_categorical_non_unique(self):
  18. msg = "cannot reindex on an axis with duplicate labels"
  19. ci = CategoricalIndex(["a", "b", "c", "a"])
  20. with pytest.raises(ValueError, match=msg):
  21. ci.reindex(Categorical(["a", "c"]))
  22. def test_reindex_list_non_unique_unused_category(self):
  23. msg = "cannot reindex on an axis with duplicate labels"
  24. ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
  25. with pytest.raises(ValueError, match=msg):
  26. ci.reindex(["a", "c"])
  27. def test_reindex_categorical_non_unique_unused_category(self):
  28. msg = "cannot reindex on an axis with duplicate labels"
  29. ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
  30. with pytest.raises(ValueError, match=msg):
  31. ci.reindex(Categorical(["a", "c"]))
  32. def test_reindex_duplicate_target(self):
  33. # See GH25459
  34. cat = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c", "d"])
  35. res, indexer = cat.reindex(["a", "c", "c"])
  36. exp = Index(["a", "c", "c"], dtype="object")
  37. tm.assert_index_equal(res, exp, exact=True)
  38. tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
  39. res, indexer = cat.reindex(
  40. CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
  41. )
  42. exp = CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
  43. tm.assert_index_equal(res, exp, exact=True)
  44. tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
  45. def test_reindex_empty_index(self):
  46. # See GH16770
  47. c = CategoricalIndex([])
  48. res, indexer = c.reindex(["a", "b"])
  49. tm.assert_index_equal(res, Index(["a", "b"]), exact=True)
  50. tm.assert_numpy_array_equal(indexer, np.array([-1, -1], dtype=np.intp))
  51. def test_reindex_categorical_added_category(self):
  52. # GH 42424
  53. ci = CategoricalIndex(
  54. [Interval(0, 1, closed="right"), Interval(1, 2, closed="right")],
  55. ordered=True,
  56. )
  57. ci_add = CategoricalIndex(
  58. [
  59. Interval(0, 1, closed="right"),
  60. Interval(1, 2, closed="right"),
  61. Interval(2, 3, closed="right"),
  62. Interval(3, 4, closed="right"),
  63. ],
  64. ordered=True,
  65. )
  66. result, _ = ci.reindex(ci_add)
  67. expected = ci_add
  68. tm.assert_index_equal(expected, result)