PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/pandas/tools/tests/test_tile.py

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