test_frozen.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import warnings
  2. import numpy as np
  3. from pandas.compat import u
  4. from pandas.core.indexes.frozen import FrozenList, FrozenNDArray
  5. from pandas.tests.test_base import CheckImmutable, CheckStringMixin
  6. from pandas.util import testing as tm
  7. class TestFrozenList(CheckImmutable, CheckStringMixin):
  8. mutable_methods = ('extend', 'pop', 'remove', 'insert')
  9. unicode_container = FrozenList([u("\u05d0"), u("\u05d1"), "c"])
  10. def setup_method(self, _):
  11. self.lst = [1, 2, 3, 4, 5]
  12. self.container = FrozenList(self.lst)
  13. self.klass = FrozenList
  14. def test_add(self):
  15. result = self.container + (1, 2, 3)
  16. expected = FrozenList(self.lst + [1, 2, 3])
  17. self.check_result(result, expected)
  18. result = (1, 2, 3) + self.container
  19. expected = FrozenList([1, 2, 3] + self.lst)
  20. self.check_result(result, expected)
  21. def test_iadd(self):
  22. q = r = self.container
  23. q += [5]
  24. self.check_result(q, self.lst + [5])
  25. # Other shouldn't be mutated.
  26. self.check_result(r, self.lst)
  27. def test_union(self):
  28. result = self.container.union((1, 2, 3))
  29. expected = FrozenList(self.lst + [1, 2, 3])
  30. self.check_result(result, expected)
  31. def test_difference(self):
  32. result = self.container.difference([2])
  33. expected = FrozenList([1, 3, 4, 5])
  34. self.check_result(result, expected)
  35. def test_difference_dupe(self):
  36. result = FrozenList([1, 2, 3, 2]).difference([2])
  37. expected = FrozenList([1, 3])
  38. self.check_result(result, expected)
  39. class TestFrozenNDArray(CheckImmutable, CheckStringMixin):
  40. mutable_methods = ('put', 'itemset', 'fill')
  41. def setup_method(self, _):
  42. self.lst = [3, 5, 7, -2]
  43. self.klass = FrozenNDArray
  44. with warnings.catch_warnings(record=True):
  45. warnings.simplefilter("ignore", FutureWarning)
  46. self.container = FrozenNDArray(self.lst)
  47. self.unicode_container = FrozenNDArray(
  48. [u("\u05d0"), u("\u05d1"), "c"])
  49. def test_constructor_warns(self):
  50. # see gh-9031
  51. with tm.assert_produces_warning(FutureWarning):
  52. FrozenNDArray([1, 2, 3])
  53. def test_shallow_copying(self):
  54. original = self.container.copy()
  55. assert isinstance(self.container.view(), FrozenNDArray)
  56. assert not isinstance(self.container.view(np.ndarray), FrozenNDArray)
  57. assert self.container.view() is not self.container
  58. tm.assert_numpy_array_equal(self.container, original)
  59. # Shallow copy should be the same too
  60. assert isinstance(self.container._shallow_copy(), FrozenNDArray)
  61. # setting should not be allowed
  62. def testit(container):
  63. container[0] = 16
  64. self.check_mutable_error(testit, self.container)
  65. def test_values(self):
  66. original = self.container.view(np.ndarray).copy()
  67. n = original[0] + 15
  68. vals = self.container.values()
  69. tm.assert_numpy_array_equal(original, vals)
  70. assert original is not vals
  71. vals[0] = n
  72. assert isinstance(self.container, FrozenNDArray)
  73. tm.assert_numpy_array_equal(self.container.values(), original)
  74. assert vals[0] == n
  75. def test_searchsorted(self):
  76. expected = 2
  77. assert self.container.searchsorted(7) == expected
  78. with tm.assert_produces_warning(FutureWarning):
  79. assert self.container.searchsorted(v=7) == expected