PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/pandas/tests/test_common.py

https://github.com/rkabir/pandas
Python | 129 lines | 93 code | 35 blank | 1 comment | 2 complexity | be1cf4e322cadf1beb31a1018ca41627 MD5 | raw file
  1. from pandas import Series, DataFrame
  2. from pandas.core.common import notnull, isnull
  3. import pandas.core.common as common
  4. import pandas.util.testing as tm
  5. import numpy as np
  6. def test_notnull():
  7. assert notnull(1.)
  8. assert not notnull(None)
  9. assert not notnull(np.NaN)
  10. assert not notnull(np.inf)
  11. assert not notnull(-np.inf)
  12. float_series = Series(np.random.randn(5))
  13. obj_series = Series(np.random.randn(5), dtype=object)
  14. assert(isinstance(notnull(float_series), Series))
  15. assert(isinstance(notnull(obj_series), Series))
  16. def test_isnull():
  17. assert not isnull(1.)
  18. assert isnull(None)
  19. assert isnull(np.NaN)
  20. assert isnull(np.inf)
  21. assert isnull(-np.inf)
  22. float_series = Series(np.random.randn(5))
  23. obj_series = Series(np.random.randn(5), dtype=object)
  24. assert(isinstance(isnull(float_series), Series))
  25. assert(isinstance(isnull(obj_series), Series))
  26. # call on DataFrame
  27. df = DataFrame(np.random.randn(10, 5))
  28. df['foo'] = 'bar'
  29. result = isnull(df)
  30. expected = result.apply(isnull)
  31. tm.assert_frame_equal(result, expected)
  32. def test_any_none():
  33. assert(common._any_none(1, 2, 3, None))
  34. assert(not common._any_none(1, 2, 3, 4))
  35. def test_all_not_none():
  36. assert(common._all_not_none(1, 2, 3, 4))
  37. assert(not common._all_not_none(1, 2, 3, None))
  38. assert(not common._all_not_none(None, None, None, None))
  39. def test_rands():
  40. r = common.rands(10)
  41. assert(len(r) == 10)
  42. def test_adjoin():
  43. data = [['a', 'b', 'c'],
  44. ['dd', 'ee', 'ff'],
  45. ['ggg', 'hhh', 'iii']]
  46. expected = 'a dd ggg\nb ee hhh\nc ff iii'
  47. adjoined = common.adjoin(2, *data)
  48. assert(adjoined == expected)
  49. def test_iterpairs():
  50. data = [1, 2, 3, 4]
  51. expected = [(1, 2),
  52. (2, 3),
  53. (3, 4)]
  54. result = list(common.iterpairs(data))
  55. assert(result == expected)
  56. def test_indent():
  57. s = 'a b c\nd e f'
  58. result = common.indent(s, spaces=6)
  59. assert(result == ' a b c\n d e f')
  60. def test_banner():
  61. ban = common.banner('hi')
  62. assert(ban == ('%s\nhi\n%s' % ('=' * 80, '=' * 80)))
  63. def test_map_indices_py():
  64. data = [4, 3, 2, 1]
  65. expected = {4 : 0, 3 : 1, 2 : 2, 1 : 3}
  66. result = common.map_indices_py(data)
  67. assert(result == expected)
  68. def test_union():
  69. a = [1, 2, 3]
  70. b = [4, 5, 6]
  71. union = sorted(common.union(a, b))
  72. assert((a + b) == union)
  73. def test_difference():
  74. a = [1, 2, 3]
  75. b = [1, 2, 3, 4, 5, 6]
  76. inter = sorted(common.difference(b, a))
  77. assert([4, 5, 6] == inter)
  78. def test_intersection():
  79. a = [1, 2, 3]
  80. b = [1, 2, 3, 4, 5, 6]
  81. inter = sorted(common.intersection(a, b))
  82. assert(a == inter)
  83. def test_groupby():
  84. values = ['foo', 'bar', 'baz', 'baz2', 'qux', 'foo3']
  85. expected = {'f' : ['foo', 'foo3'],
  86. 'b' : ['bar', 'baz', 'baz2'],
  87. 'q' : ['qux']}
  88. grouped = common.groupby(values, lambda x: x[0])
  89. for k, v in grouped:
  90. assert v == expected[k]
  91. if __name__ == '__main__':
  92. import nose
  93. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  94. exit=False)