test_rendering.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pprint
  2. import pytest
  3. import pytz # noqa # a test below uses pytz but only inside a `eval` call
  4. from pandas import Timestamp
  5. class TestTimestampRendering:
  6. timezones = ["UTC", "Asia/Tokyo", "US/Eastern", "dateutil/US/Pacific"]
  7. @pytest.mark.parametrize("tz", timezones)
  8. @pytest.mark.parametrize("freq", ["D", "M", "S", "N"])
  9. @pytest.mark.parametrize(
  10. "date", ["2014-03-07", "2014-01-01 09:00", "2014-01-01 00:00:00.000000001"]
  11. )
  12. def test_repr(self, date, freq, tz):
  13. # avoid to match with timezone name
  14. freq_repr = f"'{freq}'"
  15. if tz.startswith("dateutil"):
  16. tz_repr = tz.replace("dateutil", "")
  17. else:
  18. tz_repr = tz
  19. date_only = Timestamp(date)
  20. assert date in repr(date_only)
  21. assert tz_repr not in repr(date_only)
  22. assert freq_repr not in repr(date_only)
  23. assert date_only == eval(repr(date_only))
  24. date_tz = Timestamp(date, tz=tz)
  25. assert date in repr(date_tz)
  26. assert tz_repr in repr(date_tz)
  27. assert freq_repr not in repr(date_tz)
  28. assert date_tz == eval(repr(date_tz))
  29. def test_repr_utcoffset(self):
  30. # This can cause the tz field to be populated, but it's redundant to
  31. # include this information in the date-string.
  32. date_with_utc_offset = Timestamp("2014-03-13 00:00:00-0400", tz=None)
  33. assert "2014-03-13 00:00:00-0400" in repr(date_with_utc_offset)
  34. assert "tzoffset" not in repr(date_with_utc_offset)
  35. assert "UTC-04:00" in repr(date_with_utc_offset)
  36. expr = repr(date_with_utc_offset)
  37. assert date_with_utc_offset == eval(expr)
  38. def test_timestamp_repr_pre1900(self):
  39. # pre-1900
  40. stamp = Timestamp("1850-01-01", tz="US/Eastern")
  41. repr(stamp)
  42. iso8601 = "1850-01-01 01:23:45.012345"
  43. stamp = Timestamp(iso8601, tz="US/Eastern")
  44. result = repr(stamp)
  45. assert iso8601 in result
  46. def test_pprint(self):
  47. # GH#12622
  48. nested_obj = {"foo": 1, "bar": [{"w": {"a": Timestamp("2011-01-01")}}] * 10}
  49. result = pprint.pformat(nested_obj, width=50)
  50. expected = r"""{'bar': [{'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  51. {'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  52. {'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  53. {'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  54. {'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  55. {'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  56. {'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  57. {'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  58. {'w': {'a': Timestamp('2011-01-01 00:00:00')}},
  59. {'w': {'a': Timestamp('2011-01-01 00:00:00')}}],
  60. 'foo': 1}"""
  61. assert result == expected
  62. def test_to_timestamp_repr_is_code(self):
  63. zs = [
  64. Timestamp("99-04-17 00:00:00", tz="UTC"),
  65. Timestamp("2001-04-17 00:00:00", tz="UTC"),
  66. Timestamp("2001-04-17 00:00:00", tz="America/Los_Angeles"),
  67. Timestamp("2001-04-17 00:00:00", tz=None),
  68. ]
  69. for z in zs:
  70. assert eval(repr(z)) == z