test_assert_categorical_equal.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import pytest
  2. from pandas import Categorical
  3. import pandas._testing as tm
  4. @pytest.mark.parametrize(
  5. "c",
  6. [Categorical([1, 2, 3, 4]), Categorical([1, 2, 3, 4], categories=[1, 2, 3, 4, 5])],
  7. )
  8. def test_categorical_equal(c):
  9. tm.assert_categorical_equal(c, c)
  10. @pytest.mark.parametrize("check_category_order", [True, False])
  11. def test_categorical_equal_order_mismatch(check_category_order):
  12. c1 = Categorical([1, 2, 3, 4], categories=[1, 2, 3, 4])
  13. c2 = Categorical([1, 2, 3, 4], categories=[4, 3, 2, 1])
  14. kwargs = {"check_category_order": check_category_order}
  15. if check_category_order:
  16. msg = """Categorical\\.categories are different
  17. Categorical\\.categories values are different \\(100\\.0 %\\)
  18. \\[left\\]: Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
  19. \\[right\\]: Index\\(\\[4, 3, 2, 1\\], dtype='int64'\\)"""
  20. with pytest.raises(AssertionError, match=msg):
  21. tm.assert_categorical_equal(c1, c2, **kwargs)
  22. else:
  23. tm.assert_categorical_equal(c1, c2, **kwargs)
  24. def test_categorical_equal_categories_mismatch():
  25. msg = """Categorical\\.categories are different
  26. Categorical\\.categories values are different \\(25\\.0 %\\)
  27. \\[left\\]: Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
  28. \\[right\\]: Index\\(\\[1, 2, 3, 5\\], dtype='int64'\\)"""
  29. c1 = Categorical([1, 2, 3, 4])
  30. c2 = Categorical([1, 2, 3, 5])
  31. with pytest.raises(AssertionError, match=msg):
  32. tm.assert_categorical_equal(c1, c2)
  33. def test_categorical_equal_codes_mismatch():
  34. categories = [1, 2, 3, 4]
  35. msg = """Categorical\\.codes are different
  36. Categorical\\.codes values are different \\(50\\.0 %\\)
  37. \\[left\\]: \\[0, 1, 3, 2\\]
  38. \\[right\\]: \\[0, 1, 2, 3\\]"""
  39. c1 = Categorical([1, 2, 4, 3], categories=categories)
  40. c2 = Categorical([1, 2, 3, 4], categories=categories)
  41. with pytest.raises(AssertionError, match=msg):
  42. tm.assert_categorical_equal(c1, c2)
  43. def test_categorical_equal_ordered_mismatch():
  44. data = [1, 2, 3, 4]
  45. msg = """Categorical are different
  46. Attribute "ordered" are different
  47. \\[left\\]: False
  48. \\[right\\]: True"""
  49. c1 = Categorical(data, ordered=False)
  50. c2 = Categorical(data, ordered=True)
  51. with pytest.raises(AssertionError, match=msg):
  52. tm.assert_categorical_equal(c1, c2)
  53. @pytest.mark.parametrize("obj", ["index", "foo", "pandas"])
  54. def test_categorical_equal_object_override(obj):
  55. data = [1, 2, 3, 4]
  56. msg = f"""{obj} are different
  57. Attribute "ordered" are different
  58. \\[left\\]: False
  59. \\[right\\]: True"""
  60. c1 = Categorical(data, ordered=False)
  61. c2 = Categorical(data, ordered=True)
  62. with pytest.raises(AssertionError, match=msg):
  63. tm.assert_categorical_equal(c1, c2, obj=obj)