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

/pandas/tests/test_algos.py

https://github.com/kljensen/pandas
Python | 73 lines | 49 code | 24 blank | 0 comment | 2 complexity | d93812ba6da808d2d4ac31845c2cde1a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import unittest
  2. import numpy as np
  3. from pandas.core.api import Series
  4. import pandas as pd
  5. import pandas.core.algorithms as algos
  6. import pandas.util.testing as tm
  7. class TestMatch(unittest.TestCase):
  8. def test_ints(self):
  9. values = np.array([0, 2, 1])
  10. to_match = np.array([0, 1, 2, 2, 0, 1, 3, 0])
  11. result = algos.match(to_match, values)
  12. expected = np.array([0, 2, 1, 1, 0, 2, -1, 0])
  13. self.assert_(np.array_equal(result, expected))
  14. def test_strings(self):
  15. values = ['foo', 'bar', 'baz']
  16. to_match = ['bar', 'foo', 'qux', 'foo', 'bar', 'baz', 'qux']
  17. result = algos.match(to_match, values)
  18. expected = np.array([1, 0, -1, 0, 1, 2, -1])
  19. self.assert_(np.array_equal(result, expected))
  20. class TestUnique(unittest.TestCase):
  21. def test_ints(self):
  22. arr = np.random.randint(0, 100, size=50)
  23. result = algos.unique(arr)
  24. self.assert_(isinstance(result, np.ndarray))
  25. def test_objects(self):
  26. arr = np.random.randint(0, 100, size=50).astype('O')
  27. result = algos.unique(arr)
  28. self.assert_(isinstance(result, np.ndarray))
  29. def test_object_refcount_bug(self):
  30. lst = ['A', 'B', 'C', 'D', 'E']
  31. for i in xrange(1000): len(algos.unique(lst))
  32. def test_on_index_object(self):
  33. mindex = pd.MultiIndex.from_arrays([np.arange(5).repeat(5),
  34. np.tile(np.arange(5), 5)])
  35. mindex = mindex.repeat(2)
  36. result = pd.unique(mindex)
  37. result.sort()
  38. expected = mindex.values
  39. expected.sort()
  40. tm.assert_almost_equal(result, expected)
  41. def test_quantile():
  42. s = Series(np.random.randn(100))
  43. result = algos.quantile(s, [0, .25, .5, .75, 1.])
  44. expected = algos.quantile(s.values, [0, .25, .5, .75, 1.])
  45. tm.assert_almost_equal(result, expected)
  46. if __name__ == '__main__':
  47. import nose
  48. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  49. exit=False)