/pandas/tests/test_common.py

https://github.com/datakungfu/pandas · Python · 296 lines · 215 code · 74 blank · 7 comment · 2 complexity · 4e5699c93d6164dc94c95dcf4cb3abaa MD5 · raw file

  1. from datetime import datetime
  2. import sys
  3. import unittest
  4. from pandas import Series, DataFrame, date_range, DatetimeIndex
  5. from pandas.core.common import notnull, isnull
  6. import pandas.core.common as com
  7. import pandas.util.testing as tm
  8. import numpy as np
  9. def test_notnull():
  10. assert notnull(1.)
  11. assert not notnull(None)
  12. assert not notnull(np.NaN)
  13. assert not notnull(np.inf)
  14. assert not notnull(-np.inf)
  15. float_series = Series(np.random.randn(5))
  16. obj_series = Series(np.random.randn(5), dtype=object)
  17. assert(isinstance(notnull(float_series), Series))
  18. assert(isinstance(notnull(obj_series), Series))
  19. def test_isnull():
  20. assert not isnull(1.)
  21. assert isnull(None)
  22. assert isnull(np.NaN)
  23. assert isnull(np.inf)
  24. assert isnull(-np.inf)
  25. float_series = Series(np.random.randn(5))
  26. obj_series = Series(np.random.randn(5), dtype=object)
  27. assert(isinstance(isnull(float_series), Series))
  28. assert(isinstance(isnull(obj_series), Series))
  29. # call on DataFrame
  30. df = DataFrame(np.random.randn(10, 5))
  31. df['foo'] = 'bar'
  32. result = isnull(df)
  33. expected = result.apply(isnull)
  34. tm.assert_frame_equal(result, expected)
  35. def test_isnull_datetime():
  36. assert (not isnull(datetime.now()))
  37. assert notnull(datetime.now())
  38. idx = date_range('1/1/1990', periods=20)
  39. assert(notnull(idx).all())
  40. import pandas.lib as lib
  41. idx = np.asarray(idx)
  42. idx[0] = lib.iNaT
  43. idx = DatetimeIndex(idx)
  44. mask = isnull(idx)
  45. assert(mask[0])
  46. assert(not mask[1:].any())
  47. def test_any_none():
  48. assert(com._any_none(1, 2, 3, None))
  49. assert(not com._any_none(1, 2, 3, 4))
  50. def test_all_not_none():
  51. assert(com._all_not_none(1, 2, 3, 4))
  52. assert(not com._all_not_none(1, 2, 3, None))
  53. assert(not com._all_not_none(None, None, None, None))
  54. def test_rands():
  55. r = com.rands(10)
  56. assert(len(r) == 10)
  57. def test_adjoin():
  58. data = [['a', 'b', 'c'],
  59. ['dd', 'ee', 'ff'],
  60. ['ggg', 'hhh', 'iii']]
  61. expected = 'a dd ggg\nb ee hhh\nc ff iii'
  62. adjoined = com.adjoin(2, *data)
  63. assert(adjoined == expected)
  64. def test_iterpairs():
  65. data = [1, 2, 3, 4]
  66. expected = [(1, 2),
  67. (2, 3),
  68. (3, 4)]
  69. result = list(com.iterpairs(data))
  70. assert(result == expected)
  71. def test_indent():
  72. s = 'a b c\nd e f'
  73. result = com.indent(s, spaces=6)
  74. assert(result == ' a b c\n d e f')
  75. def test_banner():
  76. ban = com.banner('hi')
  77. assert(ban == ('%s\nhi\n%s' % ('=' * 80, '=' * 80)))
  78. def test_map_indices_py():
  79. data = [4, 3, 2, 1]
  80. expected = {4 : 0, 3 : 1, 2 : 2, 1 : 3}
  81. result = com.map_indices_py(data)
  82. assert(result == expected)
  83. def test_union():
  84. a = [1, 2, 3]
  85. b = [4, 5, 6]
  86. union = sorted(com.union(a, b))
  87. assert((a + b) == union)
  88. def test_difference():
  89. a = [1, 2, 3]
  90. b = [1, 2, 3, 4, 5, 6]
  91. inter = sorted(com.difference(b, a))
  92. assert([4, 5, 6] == inter)
  93. def test_intersection():
  94. a = [1, 2, 3]
  95. b = [1, 2, 3, 4, 5, 6]
  96. inter = sorted(com.intersection(a, b))
  97. assert(a == inter)
  98. def test_groupby():
  99. values = ['foo', 'bar', 'baz', 'baz2', 'qux', 'foo3']
  100. expected = {'f' : ['foo', 'foo3'],
  101. 'b' : ['bar', 'baz', 'baz2'],
  102. 'q' : ['qux']}
  103. grouped = com.groupby(values, lambda x: x[0])
  104. for k, v in grouped:
  105. assert v == expected[k]
  106. def test_ensure_int32():
  107. values = np.arange(10, dtype=np.int32)
  108. result = com._ensure_int32(values)
  109. assert(result.dtype == np.int32)
  110. values = np.arange(10, dtype=np.int64)
  111. result = com._ensure_int32(values)
  112. assert(result.dtype == np.int32)
  113. class TestTake(unittest.TestCase):
  114. def test_1d_with_out(self):
  115. def _test_dtype(dtype):
  116. out = np.empty(5, dtype=dtype)
  117. arr = np.random.randn(10).astype(dtype)
  118. indexer = [0, 2, 4, 7, 1]
  119. arr.take(indexer, out=out)
  120. expected = arr.take(indexer)
  121. tm.assert_almost_equal(out, expected)
  122. _test_dtype(np.float64)
  123. _test_dtype(np.float32)
  124. _test_dtype(np.int32)
  125. _test_dtype(np.int64)
  126. _test_dtype(np.object_)
  127. _test_dtype(np.bool)
  128. def test_1d_upcast_with_out(self):
  129. def _test_dtype(dtype):
  130. out = np.empty(4, dtype=dtype)
  131. data = np.random.randint(0, 2, 5).astype(dtype)
  132. indexer = [2, 1, 0, -1]
  133. self.assertRaises(Exception, com.take_1d, data,
  134. indexer, out=out)
  135. _test_dtype(np.int64)
  136. _test_dtype(np.int32)
  137. _test_dtype(np.int16)
  138. _test_dtype(np.int8)
  139. _test_dtype(np.bool)
  140. def test_2d_upcast_with_out(self):
  141. def _test_dtype(dtype):
  142. out0 = np.empty((4, 3), dtype=dtype)
  143. out1 = np.empty((5, 4), dtype=dtype)
  144. data = np.random.randint(0, 2, (5, 3)).astype(dtype)
  145. indexer = [2, 1, 0, -1]
  146. self.assertRaises(Exception, com.take_2d, data,
  147. indexer, out=out0, axis=0)
  148. self.assertRaises(Exception, com.take_2d, data,
  149. indexer, out=out1, axis=1)
  150. # no exception o/w
  151. data.take(indexer, out=out0, axis=0)
  152. data.take(indexer, out=out1, axis=1)
  153. _test_dtype(np.int64)
  154. _test_dtype(np.int32)
  155. _test_dtype(np.int16)
  156. _test_dtype(np.int8)
  157. _test_dtype(np.bool)
  158. def test_1d_other_dtypes(self):
  159. arr = np.random.randn(10).astype(np.float32)
  160. indexer = [1, 2, 3, -1]
  161. result = com.take_1d(arr, indexer)
  162. expected = arr.take(indexer)
  163. expected[-1] = np.nan
  164. tm.assert_almost_equal(result, expected)
  165. def test_2d_other_dtypes(self):
  166. arr = np.random.randn(10, 5).astype(np.float32)
  167. indexer = [1, 2, 3, -1]
  168. # axis=0
  169. result = com.take_2d(arr, indexer, axis=0)
  170. expected = arr.take(indexer, axis=0)
  171. expected[-1] = np.nan
  172. tm.assert_almost_equal(result, expected)
  173. # axis=1
  174. result = com.take_2d(arr, indexer, axis=1)
  175. expected = arr.take(indexer, axis=1)
  176. expected[:, -1] = np.nan
  177. tm.assert_almost_equal(result, expected)
  178. def test_1d_bool(self):
  179. arr = np.array([0, 1, 0], dtype=bool)
  180. result = com.take_1d(arr, [0, 2, 2, 1])
  181. expected = arr.take([0, 2, 2, 1])
  182. self.assert_(np.array_equal(result, expected))
  183. result = com.take_1d(arr, [0, 2, -1])
  184. self.assert_(result.dtype == np.object_)
  185. def test_2d_bool(self):
  186. arr = np.array([[0, 1, 0],
  187. [1, 0, 1],
  188. [0, 1, 1]], dtype=bool)
  189. result = com.take_2d(arr, [0, 2, 2, 1])
  190. expected = arr.take([0, 2, 2, 1], axis=0)
  191. self.assert_(np.array_equal(result, expected))
  192. result = com.take_2d(arr, [0, 2, 2, 1], axis=1)
  193. expected = arr.take([0, 2, 2, 1], axis=1)
  194. self.assert_(np.array_equal(result, expected))
  195. result = com.take_2d(arr, [0, 2, -1])
  196. self.assert_(result.dtype == np.object_)
  197. def test_2d_float32(self):
  198. arr = np.random.randn(4, 3).astype(np.float32)
  199. indexer = [0, 2, -1, 1, -1]
  200. # axis=0
  201. result = com.take_2d(arr, indexer)
  202. result2 = np.empty_like(result)
  203. com.take_2d(arr, indexer, out=result2)
  204. tm.assert_almost_equal(result, result)
  205. expected = arr.take(indexer, axis=0)
  206. expected[[2, 4]] = np.nan
  207. tm.assert_almost_equal(result, expected)
  208. # test with float64 out buffer
  209. out = np.empty((len(indexer), arr.shape[1]), dtype='f8')
  210. com.take_2d(arr, indexer, out=out) # it works!
  211. # axis=1
  212. result = com.take_2d(arr, indexer, axis=1)
  213. result2 = np.empty_like(result)
  214. com.take_2d(arr, indexer, axis=1, out=result2)
  215. tm.assert_almost_equal(result, result)
  216. expected = arr.take(indexer, axis=1)
  217. expected[:, [2, 4]] = np.nan
  218. tm.assert_almost_equal(result, expected)
  219. if __name__ == '__main__':
  220. import nose
  221. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  222. exit=False)