PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/pandas/tools/tests/test_util.py

http://github.com/pydata/pandas
Python | 95 lines | 69 code | 23 blank | 3 comment | 13 complexity | a01c20e0e3570c9607bf1e2b8888abd4 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. import os
  2. import locale
  3. import codecs
  4. import nose
  5. import numpy as np
  6. from numpy.testing import assert_equal
  7. from pandas import date_range, Index
  8. import pandas.util.testing as tm
  9. from pandas.tools.util import cartesian_product
  10. CURRENT_LOCALE = locale.getlocale()
  11. LOCALE_OVERRIDE = os.environ.get('LOCALE_OVERRIDE', None)
  12. class TestCartesianProduct(tm.TestCase):
  13. def test_simple(self):
  14. x, y = list('ABC'), [1, 22]
  15. result = cartesian_product([x, y])
  16. expected = [np.array(['A', 'A', 'B', 'B', 'C', 'C']),
  17. np.array([ 1, 22, 1, 22, 1, 22])]
  18. assert_equal(result, expected)
  19. def test_datetimeindex(self):
  20. # regression test for GitHub issue #6439
  21. # make sure that the ordering on datetimeindex is consistent
  22. x = date_range('2000-01-01', periods=2)
  23. result = [Index(y).day for y in cartesian_product([x, x])]
  24. expected = [np.array([1, 1, 2, 2]), np.array([1, 2, 1, 2])]
  25. assert_equal(result, expected)
  26. class TestLocaleUtils(tm.TestCase):
  27. @classmethod
  28. def setUpClass(cls):
  29. super(TestLocaleUtils, cls).setUpClass()
  30. cls.locales = tm.get_locales()
  31. if not cls.locales:
  32. raise nose.SkipTest("No locales found")
  33. if os.name == 'nt': # we're on windows
  34. raise nose.SkipTest("Running on Windows")
  35. @classmethod
  36. def tearDownClass(cls):
  37. super(TestLocaleUtils, cls).tearDownClass()
  38. del cls.locales
  39. def test_get_locales(self):
  40. # all systems should have at least a single locale
  41. assert len(tm.get_locales()) > 0
  42. def test_get_locales_prefix(self):
  43. if len(self.locales) == 1:
  44. raise nose.SkipTest("Only a single locale found, no point in "
  45. "trying to test filtering locale prefixes")
  46. first_locale = self.locales[0]
  47. assert len(tm.get_locales(prefix=first_locale[:2])) > 0
  48. def test_set_locale(self):
  49. if len(self.locales) == 1:
  50. raise nose.SkipTest("Only a single locale found, no point in "
  51. "trying to test setting another locale")
  52. if LOCALE_OVERRIDE is not None:
  53. lang, enc = LOCALE_OVERRIDE.split('.')
  54. else:
  55. lang, enc = 'it_CH', 'UTF-8'
  56. enc = codecs.lookup(enc).name
  57. new_locale = lang, enc
  58. if not tm._can_set_locale(new_locale):
  59. with tm.assertRaises(locale.Error):
  60. with tm.set_locale(new_locale):
  61. pass
  62. else:
  63. with tm.set_locale(new_locale) as normalized_locale:
  64. new_lang, new_enc = normalized_locale.split('.')
  65. new_enc = codecs.lookup(enc).name
  66. normalized_locale = new_lang, new_enc
  67. self.assertEqual(normalized_locale, new_locale)
  68. current_locale = locale.getlocale()
  69. self.assertEqual(current_locale, CURRENT_LOCALE)
  70. if __name__ == '__main__':
  71. nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
  72. exit=False)