PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/index_GFF_id/pymodules/python2.7/lib/python/pandas-0.17.1-py2.7-linux-x86_64.egg/pandas/sparse/tests/test_array.py

https://gitlab.com/pooja043/Globus_Docker_4
Python | 193 lines | 149 code | 42 blank | 2 comment | 11 complexity | 2213a44bf98efe91b1d4ba43a2234ba9 MD5 | raw file
  1. from pandas.compat import range
  2. import re
  3. from numpy import nan, ndarray
  4. import numpy as np
  5. import operator
  6. import warnings
  7. from pandas.core.series import Series
  8. from pandas.core.common import notnull
  9. from pandas.sparse.api import SparseArray
  10. from pandas.util.testing import assert_almost_equal, assertRaisesRegexp
  11. import pandas.util.testing as tm
  12. def assert_sp_array_equal(left, right):
  13. assert_almost_equal(left.sp_values, right.sp_values)
  14. assert(left.sp_index.equals(right.sp_index))
  15. if np.isnan(left.fill_value):
  16. assert(np.isnan(right.fill_value))
  17. else:
  18. assert(left.fill_value == right.fill_value)
  19. class TestSparseArray(tm.TestCase):
  20. _multiprocess_can_split_ = True
  21. def setUp(self):
  22. self.arr_data = np.array([nan, nan, 1, 2, 3, nan, 4, 5, nan, 6])
  23. self.arr = SparseArray(self.arr_data)
  24. self.zarr = SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)
  25. def test_get_item(self):
  26. errmsg = re.compile("bounds")
  27. assertRaisesRegexp(IndexError, errmsg, lambda: self.arr[11])
  28. assertRaisesRegexp(IndexError, errmsg, lambda: self.arr[-11])
  29. self.assertEqual(self.arr[-1], self.arr[len(self.arr) - 1])
  30. def test_bad_take(self):
  31. assertRaisesRegexp(IndexError, "bounds", lambda: self.arr.take(11))
  32. self.assertRaises(IndexError, lambda: self.arr.take(-11))
  33. def test_set_item(self):
  34. def setitem():
  35. self.arr[5] = 3
  36. def setslice():
  37. self.arr[1:5] = 2
  38. assertRaisesRegexp(TypeError, "item assignment", setitem)
  39. assertRaisesRegexp(TypeError, "item assignment", setslice)
  40. def test_constructor_from_sparse(self):
  41. res = SparseArray(self.zarr)
  42. self.assertEqual(res.fill_value, 0)
  43. assert_almost_equal(res.sp_values, self.zarr.sp_values)
  44. def test_constructor_copy(self):
  45. cp = SparseArray(self.arr, copy=True)
  46. cp.sp_values[:3] = 0
  47. self.assertFalse((self.arr.sp_values[:3] == 0).any())
  48. not_copy = SparseArray(self.arr)
  49. not_copy.sp_values[:3] = 0
  50. self.assertTrue((self.arr.sp_values[:3] == 0).all())
  51. def test_astype(self):
  52. res = self.arr.astype('f8')
  53. res.sp_values[:3] = 27
  54. self.assertFalse((self.arr.sp_values[:3] == 27).any())
  55. assertRaisesRegexp(TypeError, "floating point", self.arr.astype, 'i8')
  56. def test_copy_shallow(self):
  57. arr2 = self.arr.copy(deep=False)
  58. def _get_base(values):
  59. base = values.base
  60. while base.base is not None:
  61. base = base.base
  62. return base
  63. assert(_get_base(arr2) is _get_base(self.arr))
  64. def test_values_asarray(self):
  65. assert_almost_equal(self.arr.values, self.arr_data)
  66. assert_almost_equal(self.arr.to_dense(), self.arr_data)
  67. assert_almost_equal(self.arr.sp_values, np.asarray(self.arr))
  68. def test_getitem(self):
  69. def _checkit(i):
  70. assert_almost_equal(self.arr[i], self.arr.values[i])
  71. for i in range(len(self.arr)):
  72. _checkit(i)
  73. _checkit(-i)
  74. def test_getslice(self):
  75. result = self.arr[:-3]
  76. exp = SparseArray(self.arr.values[:-3])
  77. assert_sp_array_equal(result, exp)
  78. result = self.arr[-4:]
  79. exp = SparseArray(self.arr.values[-4:])
  80. assert_sp_array_equal(result, exp)
  81. # two corner cases from Series
  82. result = self.arr[-12:]
  83. exp = SparseArray(self.arr)
  84. assert_sp_array_equal(result, exp)
  85. result = self.arr[:-12]
  86. exp = SparseArray(self.arr.values[:0])
  87. assert_sp_array_equal(result, exp)
  88. def test_binary_operators(self):
  89. data1 = np.random.randn(20)
  90. data2 = np.random.randn(20)
  91. data1[::2] = np.nan
  92. data2[::3] = np.nan
  93. arr1 = SparseArray(data1)
  94. arr2 = SparseArray(data2)
  95. data1[::2] = 3
  96. data2[::3] = 3
  97. farr1 = SparseArray(data1, fill_value=3)
  98. farr2 = SparseArray(data2, fill_value=3)
  99. def _check_op(op, first, second):
  100. res = op(first, second)
  101. exp = SparseArray(op(first.values, second.values),
  102. fill_value=first.fill_value)
  103. tm.assertIsInstance(res, SparseArray)
  104. assert_almost_equal(res.values, exp.values)
  105. res2 = op(first, second.values)
  106. tm.assertIsInstance(res2, SparseArray)
  107. assert_sp_array_equal(res, res2)
  108. res3 = op(first.values, second)
  109. tm.assertIsInstance(res3, SparseArray)
  110. assert_sp_array_equal(res, res3)
  111. res4 = op(first, 4)
  112. tm.assertIsInstance(res4, SparseArray)
  113. # ignore this if the actual op raises (e.g. pow)
  114. try:
  115. exp = op(first.values, 4)
  116. exp_fv = op(first.fill_value, 4)
  117. assert_almost_equal(res4.fill_value, exp_fv)
  118. assert_almost_equal(res4.values, exp)
  119. except (ValueError) :
  120. pass
  121. def _check_inplace_op(op):
  122. tmp = arr1.copy()
  123. self.assertRaises(NotImplementedError, op, tmp, arr2)
  124. bin_ops = [operator.add, operator.sub, operator.mul, operator.truediv,
  125. operator.floordiv, operator.pow]
  126. for op in bin_ops:
  127. _check_op(op, arr1, arr2)
  128. _check_op(op, farr1, farr2)
  129. inplace_ops = ['iadd', 'isub', 'imul', 'itruediv', 'ifloordiv', 'ipow']
  130. for op in inplace_ops:
  131. _check_inplace_op(getattr(operator, op))
  132. def test_pickle(self):
  133. def _check_roundtrip(obj):
  134. unpickled = self.round_trip_pickle(obj)
  135. assert_sp_array_equal(unpickled, obj)
  136. _check_roundtrip(self.arr)
  137. _check_roundtrip(self.zarr)
  138. def test_generator_warnings(self):
  139. sp_arr = SparseArray([1, 2, 3])
  140. with warnings.catch_warnings(record=True) as w:
  141. warnings.filterwarnings(action='always',
  142. category=DeprecationWarning)
  143. warnings.filterwarnings(action='always',
  144. category=PendingDeprecationWarning)
  145. for _ in sp_arr:
  146. pass
  147. assert len(w)==0
  148. if __name__ == '__main__':
  149. import nose
  150. nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
  151. exit=False)