test_take.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import numpy as np
  2. import pytest
  3. from pandas import Categorical
  4. import pandas._testing as tm
  5. class TestTake:
  6. # https://github.com/pandas-dev/pandas/issues/20664
  7. def test_take_default_allow_fill(self):
  8. cat = Categorical(["a", "b"])
  9. with tm.assert_produces_warning(None):
  10. result = cat.take([0, -1])
  11. assert result.equals(cat)
  12. def test_take_positive_no_warning(self):
  13. cat = Categorical(["a", "b"])
  14. with tm.assert_produces_warning(None):
  15. cat.take([0, 0])
  16. def test_take_bounds(self, allow_fill):
  17. # https://github.com/pandas-dev/pandas/issues/20664
  18. cat = Categorical(["a", "b", "a"])
  19. if allow_fill:
  20. msg = "indices are out-of-bounds"
  21. else:
  22. msg = "index 4 is out of bounds for( axis 0 with)? size 3"
  23. with pytest.raises(IndexError, match=msg):
  24. cat.take([4, 5], allow_fill=allow_fill)
  25. def test_take_empty(self, allow_fill):
  26. # https://github.com/pandas-dev/pandas/issues/20664
  27. cat = Categorical([], categories=["a", "b"])
  28. if allow_fill:
  29. msg = "indices are out-of-bounds"
  30. else:
  31. msg = "cannot do a non-empty take from an empty axes"
  32. with pytest.raises(IndexError, match=msg):
  33. cat.take([0], allow_fill=allow_fill)
  34. def test_positional_take(self, ordered):
  35. cat = Categorical(["a", "a", "b", "b"], categories=["b", "a"], ordered=ordered)
  36. result = cat.take([0, 1, 2], allow_fill=False)
  37. expected = Categorical(
  38. ["a", "a", "b"], categories=cat.categories, ordered=ordered
  39. )
  40. tm.assert_categorical_equal(result, expected)
  41. def test_positional_take_unobserved(self, ordered):
  42. cat = Categorical(["a", "b"], categories=["a", "b", "c"], ordered=ordered)
  43. result = cat.take([1, 0], allow_fill=False)
  44. expected = Categorical(["b", "a"], categories=cat.categories, ordered=ordered)
  45. tm.assert_categorical_equal(result, expected)
  46. def test_take_allow_fill(self):
  47. # https://github.com/pandas-dev/pandas/issues/23296
  48. cat = Categorical(["a", "a", "b"])
  49. result = cat.take([0, -1, -1], allow_fill=True)
  50. expected = Categorical(["a", np.nan, np.nan], categories=["a", "b"])
  51. tm.assert_categorical_equal(result, expected)
  52. def test_take_fill_with_negative_one(self):
  53. # -1 was a category
  54. cat = Categorical([-1, 0, 1])
  55. result = cat.take([0, -1, 1], allow_fill=True, fill_value=-1)
  56. expected = Categorical([-1, -1, 0], categories=[-1, 0, 1])
  57. tm.assert_categorical_equal(result, expected)
  58. def test_take_fill_value(self):
  59. # https://github.com/pandas-dev/pandas/issues/23296
  60. cat = Categorical(["a", "b", "c"])
  61. result = cat.take([0, 1, -1], fill_value="a", allow_fill=True)
  62. expected = Categorical(["a", "b", "a"], categories=["a", "b", "c"])
  63. tm.assert_categorical_equal(result, expected)
  64. def test_take_fill_value_new_raises(self):
  65. # https://github.com/pandas-dev/pandas/issues/23296
  66. cat = Categorical(["a", "b", "c"])
  67. xpr = r"Cannot setitem on a Categorical with a new category \(d\)"
  68. with pytest.raises(TypeError, match=xpr):
  69. cat.take([0, 1, -1], fill_value="d", allow_fill=True)