PageRenderTime 64ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/pandas/tools/tests/test_tile.py

https://github.com/thouis/pandas
Python | 222 lines | 152 code | 65 blank | 5 comment | 3 complexity | da4df9d20fc0f89d8dceed703feda827 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import os
  2. import nose
  3. import unittest
  4. import numpy as np
  5. from pandas import DataFrame, Series, unique
  6. import pandas.util.testing as tm
  7. import pandas.core.common as com
  8. from pandas.core.algorithms import quantile
  9. from pandas.tools.tile import cut, qcut
  10. import pandas.tools.tile as tmod
  11. from numpy.testing import assert_equal, assert_almost_equal
  12. class TestCut(unittest.TestCase):
  13. def test_simple(self):
  14. data = np.ones(5)
  15. result = cut(data, 4, labels=False)
  16. desired = [1, 1, 1, 1, 1]
  17. assert_equal(result, desired)
  18. def test_bins(self):
  19. data = np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1])
  20. result, bins = cut(data, 3, retbins=True)
  21. assert_equal(result.labels, [0, 0, 0, 1, 2, 0])
  22. assert_almost_equal(bins, [ 0.1905, 3.36666667, 6.53333333, 9.7])
  23. def test_right(self):
  24. data = np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1, 2.575])
  25. result, bins = cut(data, 4, right=True, retbins=True)
  26. assert_equal(result.labels, [0, 0, 0, 2, 3, 0, 0])
  27. assert_almost_equal(bins, [0.1905, 2.575, 4.95, 7.325, 9.7])
  28. def test_noright(self):
  29. data = np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1, 2.575])
  30. result, bins = cut(data, 4, right=False, retbins=True)
  31. assert_equal(result.labels, [0, 0, 0, 2, 3, 0, 1])
  32. assert_almost_equal(bins, [ 0.2, 2.575, 4.95, 7.325, 9.7095])
  33. def test_arraylike(self):
  34. data = [.2, 1.4, 2.5, 6.2, 9.7, 2.1]
  35. result, bins = cut(data, 3, retbins=True)
  36. assert_equal(result.labels, [0, 0, 0, 1, 2, 0])
  37. assert_almost_equal(bins, [ 0.1905, 3.36666667, 6.53333333, 9.7])
  38. def test_bins_not_monotonic(self):
  39. data = [.2, 1.4, 2.5, 6.2, 9.7, 2.1]
  40. self.assertRaises(ValueError, cut, data, [0.1, 1.5, 1, 10])
  41. def test_wrong_num_labels(self):
  42. data = [.2, 1.4, 2.5, 6.2, 9.7, 2.1]
  43. self.assertRaises(ValueError, cut, data, [0, 1, 10],
  44. labels=['foo', 'bar', 'baz'])
  45. def test_cut_corner(self):
  46. # h3h
  47. self.assertRaises(ValueError, cut, [], 2)
  48. self.assertRaises(ValueError, cut, [1, 2, 3], 0.5)
  49. def test_cut_out_of_range_more(self):
  50. # #1511
  51. s = Series([0, -1, 0, 1, -3])
  52. ind = cut(s, [0, 1], labels=False)
  53. exp = [np.nan, np.nan, np.nan, 0, np.nan]
  54. assert_almost_equal(ind, exp)
  55. def test_labels(self):
  56. arr = np.tile(np.arange(0, 1.01, 0.1), 4)
  57. result, bins = cut(arr, 4, retbins=True)
  58. ex_levels = ['(-0.001, 0.25]', '(0.25, 0.5]', '(0.5, 0.75]',
  59. '(0.75, 1]']
  60. self.assert_(np.array_equal(result.levels, ex_levels))
  61. result, bins = cut(arr, 4, retbins=True, right=False)
  62. ex_levels = ['[0, 0.25)', '[0.25, 0.5)', '[0.5, 0.75)',
  63. '[0.75, 1.001)']
  64. self.assert_(np.array_equal(result.levels, ex_levels))
  65. def test_cut_pass_series_name_to_factor(self):
  66. s = Series(np.random.randn(100), name='foo')
  67. factor = cut(s, 4)
  68. self.assertEquals(factor.name, 'foo')
  69. def test_label_precision(self):
  70. arr = np.arange(0, 0.73, 0.01)
  71. result = cut(arr, 4, precision=2)
  72. ex_levels = ['(-0.00072, 0.18]', '(0.18, 0.36]', '(0.36, 0.54]',
  73. '(0.54, 0.72]']
  74. self.assert_(np.array_equal(result.levels, ex_levels))
  75. def test_na_handling(self):
  76. arr = np.arange(0, 0.75, 0.01)
  77. arr[::3] = np.nan
  78. result = cut(arr, 4)
  79. result_arr = np.asarray(result)
  80. ex_arr = np.where(com.isnull(arr), np.nan, result_arr)
  81. tm.assert_almost_equal(result_arr, ex_arr)
  82. result = cut(arr, 4, labels=False)
  83. ex_result = np.where(com.isnull(arr), np.nan, result)
  84. tm.assert_almost_equal(result, ex_result)
  85. def test_qcut(self):
  86. arr = np.random.randn(1000)
  87. labels, bins = qcut(arr, 4, retbins=True)
  88. ex_bins = quantile(arr, [0, .25, .5, .75, 1.])
  89. assert_almost_equal(bins, ex_bins)
  90. ex_levels = cut(arr, ex_bins, include_lowest=True)
  91. self.assert_(np.array_equal(labels, ex_levels))
  92. def test_qcut_bounds(self):
  93. arr = np.random.randn(1000)
  94. factor = qcut(arr, 10, labels=False)
  95. self.assert_(len(np.unique(factor)) == 10)
  96. def test_qcut_specify_quantiles(self):
  97. arr = np.random.randn(100)
  98. factor = qcut(arr, [0, .25, .5, .75, 1.])
  99. expected = qcut(arr, 4)
  100. self.assert_(factor.equals(expected))
  101. def test_cut_out_of_bounds(self):
  102. arr = np.random.randn(100)
  103. result = cut(arr, [-1, 0, 1])
  104. mask = result.labels == -1
  105. ex_mask = (arr < -1) | (arr > 1)
  106. self.assert_(np.array_equal(mask, ex_mask))
  107. def test_cut_pass_labels(self):
  108. arr = [50, 5, 10, 15, 20, 30, 70]
  109. bins = [0, 25, 50, 100]
  110. labels = ['Small', 'Medium', 'Large']
  111. result = cut(arr, bins, labels=labels)
  112. exp = cut(arr, bins)
  113. exp.levels = labels
  114. self.assert_(result.equals(exp))
  115. def test_qcut_include_lowest(self):
  116. values = np.arange(10)
  117. cats = qcut(values, 4)
  118. ex_levels = ['[0, 2.25]', '(2.25, 4.5]', '(4.5, 6.75]', '(6.75, 9]']
  119. self.assert_((cats.levels == ex_levels).all())
  120. def test_qcut_nas(self):
  121. arr = np.random.randn(100)
  122. arr[:20] = np.nan
  123. result = qcut(arr, 4)
  124. self.assert_(com.isnull(result[:20]).all())
  125. def test_label_formatting(self):
  126. self.assertEquals(tmod._trim_zeros('1.000'), '1')
  127. # it works
  128. result = cut(np.arange(11.), 2)
  129. result = cut(np.arange(11.) / 1e10, 2)
  130. # #1979, negative numbers
  131. result = tmod._format_label(-117.9998, precision=3)
  132. self.assertEquals(result, '-118')
  133. result = tmod._format_label(117.9998, precision=3)
  134. self.assertEquals(result, '118')
  135. def test_qcut_binning_issues(self):
  136. # #1978, 1979
  137. path = os.path.join(curpath(), 'cut_data.csv')
  138. arr = np.loadtxt(path)
  139. result = qcut(arr, 20)
  140. starts = []
  141. ends = []
  142. for lev in result.levels:
  143. s, e = lev[1:-1].split(',')
  144. self.assertTrue(s != e)
  145. starts.append(float(s))
  146. ends.append(float(e))
  147. for (sp, sn), (ep, en) in zip(zip(starts[:-1], starts[1:]),
  148. zip(ends[:-1], ends[1:])):
  149. self.assertTrue(sp < sn)
  150. self.assertTrue(ep < en)
  151. self.assertTrue(ep <= sn)
  152. def curpath():
  153. pth, _ = os.path.split(os.path.abspath(__file__))
  154. return pth
  155. if __name__ == '__main__':
  156. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  157. exit=False)