PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/pandas/tests/test_tseries.py

http://github.com/wesm/pandas
Python | 358 lines | 260 code | 85 blank | 13 comment | 4 complexity | 8576b26e3ea00617b9facedd7f4f590b MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. import unittest
  2. import numpy as np
  3. from pandas import Index
  4. from pandas.util.testing import assert_almost_equal
  5. import pandas.util.testing as common
  6. import pandas._tseries as lib
  7. class TestTseriesUtil(unittest.TestCase):
  8. def test_combineFunc(self):
  9. pass
  10. def test_reindex(self):
  11. pass
  12. def test_isnull(self):
  13. pass
  14. def test_groupby(self):
  15. pass
  16. def test_groupby_withnull(self):
  17. pass
  18. def test_merge_indexer(self):
  19. old = Index([1, 5, 10])
  20. new = Index(range(12))
  21. filler = lib.merge_indexer_int64(new, old.indexMap)
  22. expect_filler = [-1, 0, -1, -1, -1, 1, -1, -1, -1, -1, 2, -1]
  23. self.assert_(np.array_equal(filler, expect_filler))
  24. # corner case
  25. old = Index([1, 4])
  26. new = Index(range(5, 10))
  27. filler = lib.merge_indexer_int64(new, old.indexMap)
  28. expect_filler = [-1, -1, -1, -1, -1]
  29. self.assert_(np.array_equal(filler, expect_filler))
  30. def test_backfill(self):
  31. old = Index([1, 5, 10])
  32. new = Index(range(12))
  33. filler = lib.backfill_int64(old, new, old.indexMap, new.indexMap)
  34. expect_filler = [0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1]
  35. self.assert_(np.array_equal(filler, expect_filler))
  36. # corner case
  37. old = Index([1, 4])
  38. new = Index(range(5, 10))
  39. filler = lib.backfill_int64(old, new, old.indexMap, new.indexMap)
  40. expect_filler = [-1, -1, -1, -1, -1]
  41. self.assert_(np.array_equal(filler, expect_filler))
  42. def test_pad(self):
  43. old = Index([1, 5, 10])
  44. new = Index(range(12))
  45. filler = lib.pad_int64(old, new, old.indexMap, new.indexMap)
  46. expect_filler = [-1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2]
  47. self.assert_(np.array_equal(filler, expect_filler))
  48. # corner case
  49. old = Index([5, 10])
  50. new = Index(range(5))
  51. filler = lib.pad_int64(old, new, old.indexMap, new.indexMap)
  52. expect_filler = [-1, -1, -1, -1, -1]
  53. self.assert_(np.array_equal(filler, expect_filler))
  54. def test_left_join_indexer():
  55. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  56. b = np.array([2, 2, 3, 4, 4], dtype=np.int64)
  57. result = lib.left_join_indexer_int64(b, a)
  58. expected = np.array([1, 1, 2, 3, 3], dtype='i4')
  59. assert(np.array_equal(result, expected))
  60. def test_left_outer_join_bug():
  61. left = np.array([0, 1, 0, 1, 1, 2, 3, 1, 0, 2, 1, 2, 0, 1, 1, 2, 3, 2, 3,
  62. 2, 1, 1, 3, 0, 3, 2, 3, 0, 0, 2, 3, 2, 0, 3, 1, 3, 0, 1,
  63. 3, 0, 0, 1, 0, 3, 1, 0, 1, 0, 1, 1, 0, 2, 2, 2, 2, 2, 0,
  64. 3, 1, 2, 0, 0, 3, 1, 3, 2, 2, 0, 1, 3, 0, 2, 3, 2, 3, 3,
  65. 2, 3, 3, 1, 3, 2, 0, 0, 3, 1, 1, 1, 0, 2, 3, 3, 1, 2, 0,
  66. 3, 1, 2, 0, 2], dtype=np.int32)
  67. right = np.array([3, 1], dtype=np.int32)
  68. max_groups = 4
  69. lidx, ridx = lib.left_outer_join(left, right, max_groups, sort=False)
  70. exp_lidx = np.arange(len(left))
  71. exp_ridx = -np.ones(len(left))
  72. exp_ridx[left == 1] = 1
  73. exp_ridx[left == 3] = 0
  74. assert(np.array_equal(lidx, exp_lidx))
  75. assert(np.array_equal(ridx, exp_ridx))
  76. def test_inner_join_indexer():
  77. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  78. b = np.array([0, 3, 5, 7, 9], dtype=np.int64)
  79. index, ares, bres = lib.inner_join_indexer_int64(a, b)
  80. index_exp = np.array([3, 5], dtype=np.int64)
  81. assert_almost_equal(index, index_exp)
  82. aexp = np.array([2, 4])
  83. bexp = np.array([1, 2])
  84. assert_almost_equal(ares, aexp)
  85. assert_almost_equal(bres, bexp)
  86. def test_outer_join_indexer():
  87. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  88. b = np.array([0, 3, 5, 7, 9], dtype=np.int64)
  89. index, ares, bres = lib.outer_join_indexer_int64(a, b)
  90. index_exp = np.array([0, 1, 2, 3, 4, 5, 7, 9], dtype=np.int64)
  91. assert_almost_equal(index, index_exp)
  92. aexp = np.array([-1, 0, 1, 2, 3, 4, -1, -1], dtype=np.int32)
  93. bexp = np.array([0, -1, -1, 1, -1, 2, 3, 4])
  94. assert_almost_equal(ares, aexp)
  95. assert_almost_equal(bres, bexp)
  96. def test_is_lexsorted():
  97. failure = [
  98. np.array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  99. 3, 3,
  100. 3, 3,
  101. 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  102. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  103. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  104. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  105. 0, 0, 0, 0, 0, 0, 0, 0, 0]),
  106. np.array([30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
  107. 15, 14,
  108. 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28,
  109. 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
  110. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28, 27, 26, 25,
  111. 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8,
  112. 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28, 27, 26, 25, 24, 23, 22,
  113. 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
  114. 4, 3, 2, 1, 0])]
  115. assert(not lib.is_lexsorted(failure))
  116. # def test_get_group_index():
  117. # a = np.array([0, 1, 2, 0, 2, 1, 0, 0], dtype='i4')
  118. # b = np.array([1, 0, 3, 2, 0, 2, 3, 0], dtype='i4')
  119. # expected = np.array([1, 4, 11, 2, 8, 6, 3, 0], dtype='i4')
  120. # result = lib.get_group_index([a, b], (3, 4))
  121. # assert(np.array_equal(result, expected))
  122. def test_groupsort_indexer():
  123. a = np.random.randint(0, 1000, 100).astype('i4')
  124. b = np.random.randint(0, 1000, 100).astype('i4')
  125. result = lib.groupsort_indexer(a, 1000)[0]
  126. # need to use a stable sort
  127. expected = np.argsort(a, kind='mergesort')
  128. assert(np.array_equal(result, expected))
  129. # compare with lexsort
  130. key = a * 1000 + b
  131. result = lib.groupsort_indexer(key, 1000000)[0]
  132. expected = np.lexsort((b, a))
  133. assert(np.array_equal(result, expected))
  134. def test_duplicated_with_nas():
  135. keys = [0, 1, np.nan, 0, 2, np.nan]
  136. result = lib.duplicated(keys)
  137. expected = [False, False, False, True, False, True]
  138. assert(np.array_equal(result, expected))
  139. result = lib.duplicated(keys, take_last=True)
  140. expected = [True, False, True, False, False, False]
  141. assert(np.array_equal(result, expected))
  142. keys = [(0, 0), (0, np.nan), (np.nan, 0), (np.nan, np.nan)] * 2
  143. result = lib.duplicated(keys)
  144. falses = [False] * 4
  145. trues = [True] * 4
  146. expected = falses + trues
  147. assert(np.array_equal(result, expected))
  148. result = lib.duplicated(keys, take_last=True)
  149. expected = trues + falses
  150. assert(np.array_equal(result, expected))
  151. def test_convert_objects():
  152. arr = np.array(['a', 'b', np.nan, np.nan, 'd', 'e', 'f'], dtype='O')
  153. result = lib.maybe_convert_objects(arr)
  154. assert(result.dtype == np.object_)
  155. def test_convert_objects_ints():
  156. # test that we can detect many kinds of integers
  157. dtypes = ['i1', 'i2', 'i4', 'i8', 'u1', 'u2', 'u4', 'u8']
  158. for dtype_str in dtypes:
  159. arr = np.array(list(np.arange(20, dtype=dtype_str)), dtype='O')
  160. assert(arr[0].dtype == np.dtype(dtype_str))
  161. result = lib.maybe_convert_objects(arr)
  162. assert(issubclass(result.dtype.type, np.integer))
  163. def test_rank():
  164. from scipy.stats import rankdata
  165. from numpy import nan
  166. def _check(arr):
  167. mask = -np.isfinite(arr)
  168. arr = arr.copy()
  169. result = lib.rank_1d_float64(arr)
  170. arr[mask] = np.inf
  171. exp = rankdata(arr)
  172. exp[mask] = np.nan
  173. assert_almost_equal(result, exp)
  174. _check(np.array([nan, nan, 5., 5., 5., nan, 1, 2, 3, nan]))
  175. _check(np.array([4., nan, 5., 5., 5., nan, 1, 2, 4., nan]))
  176. def test_get_reverse_indexer():
  177. indexer = np.array([-1, -1, 1, 2, 0, -1, 3, 4], dtype='i4')
  178. result = lib.get_reverse_indexer(indexer, 5)
  179. expected = np.array([4, 2, 3, 6, 7], dtype='i4')
  180. assert(np.array_equal(result, expected))
  181. def test_pad_backfill_object_segfault():
  182. from datetime import datetime
  183. old = np.array([], dtype='O')
  184. new = np.array([datetime(2010, 12, 31)], dtype='O')
  185. result = lib.pad_object(old, new, lib.map_indices_object(old),
  186. lib.map_indices_object(new))
  187. expected = np.array([-1], dtype='i4')
  188. assert(np.array_equal(result, expected))
  189. result = lib.pad_object(new, old, lib.map_indices_object(new),
  190. lib.map_indices_object(old))
  191. expected = np.array([], dtype='i4')
  192. assert(np.array_equal(result, expected))
  193. result = lib.backfill_object(old, new, lib.map_indices_object(old),
  194. lib.map_indices_object(new))
  195. expected = np.array([-1], dtype='i4')
  196. assert(np.array_equal(result, expected))
  197. result = lib.backfill_object(new, old, lib.map_indices_object(new),
  198. lib.map_indices_object(old))
  199. expected = np.array([], dtype='i4')
  200. assert(np.array_equal(result, expected))
  201. def test_arrmap():
  202. values = np.array(['foo', 'foo', 'bar', 'bar', 'baz', 'qux'], dtype='O')
  203. result = lib.arrmap_object(values, lambda x: x in ['foo', 'bar'])
  204. assert(result.dtype == np.bool_)
  205. class TestTypeInference(unittest.TestCase):
  206. def test_length_zero(self):
  207. result = lib.infer_dtype(np.array([], dtype='i4'))
  208. self.assertEqual(result, 'empty')
  209. result = lib.infer_dtype(np.array([], dtype='O'))
  210. self.assertEqual(result, 'empty')
  211. def test_integers(self):
  212. arr = np.array([1, 2, 3, np.int64(4), np.int32(5)], dtype='O')
  213. result = lib.infer_dtype(arr)
  214. self.assertEqual(result, 'integer')
  215. arr = np.array([1, 2, 3, np.int64(4), np.int32(5), 'foo'],
  216. dtype='O')
  217. result = lib.infer_dtype(arr)
  218. self.assertEqual(result, 'mixed')
  219. arr = np.array([1, 2, 3, 4, 5], dtype='i4')
  220. result = lib.infer_dtype(arr)
  221. self.assertEqual(result, 'integer')
  222. def test_bools(self):
  223. arr = np.array([True, False, True, True, True], dtype='O')
  224. result = lib.infer_dtype(arr)
  225. self.assertEqual(result, 'boolean')
  226. arr = np.array([np.bool_(True), np.bool_(False)], dtype='O')
  227. result = lib.infer_dtype(arr)
  228. self.assertEqual(result, 'boolean')
  229. arr = np.array([True, False, True, 'foo'], dtype='O')
  230. result = lib.infer_dtype(arr)
  231. self.assertEqual(result, 'mixed')
  232. arr = np.array([True, False, True], dtype=bool)
  233. result = lib.infer_dtype(arr)
  234. self.assertEqual(result, 'boolean')
  235. def test_floats(self):
  236. arr = np.array([1., 2., 3., np.float64(4), np.float32(5)], dtype='O')
  237. result = lib.infer_dtype(arr)
  238. self.assertEqual(result, 'floating')
  239. arr = np.array([1, 2, 3, np.float64(4), np.float32(5), 'foo'],
  240. dtype='O')
  241. result = lib.infer_dtype(arr)
  242. self.assertEqual(result, 'mixed')
  243. arr = np.array([1, 2, 3, 4, 5], dtype='f4')
  244. result = lib.infer_dtype(arr)
  245. self.assertEqual(result, 'floating')
  246. arr = np.array([1, 2, 3, 4, 5], dtype='f8')
  247. result = lib.infer_dtype(arr)
  248. self.assertEqual(result, 'floating')
  249. def test_string(self):
  250. pass
  251. def test_unicode(self):
  252. pass
  253. def test_datetime(self):
  254. pass
  255. def test_to_object_array_tuples(self):
  256. r = (5,6)
  257. values = [r]
  258. result = lib.to_object_array_tuples(values)
  259. try:
  260. # make sure record array works
  261. from collections import namedtuple
  262. record = namedtuple('record', 'x y')
  263. r = record(5, 6)
  264. values = [r]
  265. result = lib.to_object_array_tuples(values)
  266. except ImportError:
  267. pass
  268. class TestMoments(unittest.TestCase):
  269. pass
  270. if __name__ == '__main__':
  271. import nose
  272. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  273. exit=False)