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

/pandas/tests/test_tseries.py

https://github.com/benracine/pandas
Python | 181 lines | 123 code | 47 blank | 11 comment | 1 complexity | de6499dd0b2ead29c524c1ffda404066 MD5 | raw file
Possible License(s): BSD-3-Clause
  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_object(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_object(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_object(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_object(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_object(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_object(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_inner_join_indexer():
  55. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  56. b = np.array([0, 3, 5, 7, 9], dtype=np.int64)
  57. index, ares, bres = lib.inner_join_indexer_int64(a, b)
  58. index_exp = np.array([3, 5], dtype=np.int64)
  59. assert_almost_equal(index, index_exp)
  60. aexp = np.array([2, 4])
  61. bexp = np.array([1, 2])
  62. assert_almost_equal(ares, aexp)
  63. assert_almost_equal(bres, bexp)
  64. def test_outer_join_indexer():
  65. a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
  66. b = np.array([0, 3, 5, 7, 9], dtype=np.int64)
  67. index, ares, bres = lib.outer_join_indexer_int64(a, b)
  68. index_exp = np.array([0, 1, 2, 3, 4, 5, 7, 9], dtype=np.int64)
  69. assert_almost_equal(index, index_exp)
  70. aexp = np.array([-1, 0, 1, 2, 3, 4, -1, -1], dtype=np.int32)
  71. bexp = np.array([0, -1, -1, 1, -1, 2, 3, 4])
  72. assert_almost_equal(ares, aexp)
  73. assert_almost_equal(bres, bexp)
  74. def test_is_lexsorted():
  75. failure = [
  76. np.array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  77. 3, 3,
  78. 3, 3,
  79. 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  80. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  81. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  82. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0]),
  84. np.array([30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
  85. 15, 14,
  86. 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28,
  87. 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
  88. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28, 27, 26, 25,
  89. 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8,
  90. 7, 6, 5, 4, 3, 2, 1, 0, 30, 29, 28, 27, 26, 25, 24, 23, 22,
  91. 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
  92. 4, 3, 2, 1, 0])]
  93. assert(not lib.is_lexsorted(failure))
  94. # def test_get_group_index():
  95. # a = np.array([0, 1, 2, 0, 2, 1, 0, 0], dtype='i4')
  96. # b = np.array([1, 0, 3, 2, 0, 2, 3, 0], dtype='i4')
  97. # expected = np.array([1, 4, 11, 2, 8, 6, 3, 0], dtype='i4')
  98. # result = lib.get_group_index([a, b], (3, 4))
  99. # assert(np.array_equal(result, expected))
  100. def test_groupsort_indexer():
  101. a = np.random.randint(0, 1000, 100).astype('i4')
  102. b = np.random.randint(0, 1000, 100).astype('i4')
  103. result = lib.groupsort_indexer(a, 1000)
  104. # need to use a stable sort
  105. expected = np.argsort(a, kind='mergesort')
  106. assert(np.array_equal(result, expected))
  107. # compare with lexsort
  108. key = a * 1000 + b
  109. result = lib.groupsort_indexer(key, 1000000)
  110. expected = np.lexsort((b, a))
  111. assert(np.array_equal(result, expected))
  112. def test_duplicated_with_nas():
  113. keys = [0, 1, np.nan, 0, 2, np.nan]
  114. result = lib.duplicated(keys)
  115. expected = [False, False, False, True, False, True]
  116. assert(np.array_equal(result, expected))
  117. result = lib.duplicated(keys, take_last=True)
  118. expected = [True, False, True, False, False, False]
  119. assert(np.array_equal(result, expected))
  120. keys = [(0, 0), (0, np.nan), (np.nan, 0), (np.nan, np.nan)] * 2
  121. result = lib.duplicated(keys)
  122. falses = [False] * 4
  123. trues = [True] * 4
  124. expected = falses + trues
  125. assert(np.array_equal(result, expected))
  126. result = lib.duplicated(keys, take_last=True)
  127. expected = trues + falses
  128. assert(np.array_equal(result, expected))
  129. class TestMoments(unittest.TestCase):
  130. pass
  131. if __name__ == '__main__':
  132. import nose
  133. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  134. exit=False)