PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/pandas/tests/test_algos.py

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