test_parse_iso8601.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. import pytest
  4. from pandas._libs import tslib
  5. @pytest.mark.parametrize("date_str, exp", [
  6. ("2011-01-02", datetime(2011, 1, 2)),
  7. ("2011-1-2", datetime(2011, 1, 2)),
  8. ("2011-01", datetime(2011, 1, 1)),
  9. ("2011-1", datetime(2011, 1, 1)),
  10. ("2011 01 02", datetime(2011, 1, 2)),
  11. ("2011.01.02", datetime(2011, 1, 2)),
  12. ("2011/01/02", datetime(2011, 1, 2)),
  13. ("2011\\01\\02", datetime(2011, 1, 2)),
  14. ("2013-01-01 05:30:00", datetime(2013, 1, 1, 5, 30)),
  15. ("2013-1-1 5:30:00", datetime(2013, 1, 1, 5, 30))])
  16. def test_parsers_iso8601(date_str, exp):
  17. # see gh-12060
  18. #
  19. # Test only the ISO parser - flexibility to
  20. # different separators and leading zero's.
  21. actual = tslib._test_parse_iso8601(date_str)
  22. assert actual == exp
  23. @pytest.mark.parametrize("date_str", [
  24. "2011-01/02",
  25. "2011=11=11",
  26. "201401",
  27. "201111",
  28. "200101",
  29. # Mixed separated and unseparated.
  30. "2005-0101",
  31. "200501-01",
  32. "20010101 12:3456",
  33. "20010101 1234:56",
  34. # HHMMSS must have two digits in
  35. # each component if unseparated.
  36. "20010101 1",
  37. "20010101 123",
  38. "20010101 12345",
  39. "20010101 12345Z",
  40. ])
  41. def test_parsers_iso8601_invalid(date_str):
  42. msg = "Error parsing datetime string \"{s}\"".format(s=date_str)
  43. with pytest.raises(ValueError, match=msg):
  44. tslib._test_parse_iso8601(date_str)
  45. def test_parsers_iso8601_invalid_offset_invalid():
  46. date_str = "2001-01-01 12-34-56"
  47. msg = ("Timezone hours offset out of range "
  48. "in datetime string \"{s}\"".format(s=date_str))
  49. with pytest.raises(ValueError, match=msg):
  50. tslib._test_parse_iso8601(date_str)