test_to_timestamp.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from datetime import datetime
  2. import numpy as np
  3. import pytest
  4. from pandas import (
  5. DatetimeIndex,
  6. NaT,
  7. PeriodIndex,
  8. Timedelta,
  9. Timestamp,
  10. date_range,
  11. period_range,
  12. )
  13. import pandas._testing as tm
  14. class TestToTimestamp:
  15. def test_to_timestamp_non_contiguous(self):
  16. # GH#44100
  17. dti = date_range("2021-10-18", periods=9, freq="B")
  18. pi = dti.to_period()
  19. result = pi[::2].to_timestamp()
  20. expected = dti[::2]
  21. tm.assert_index_equal(result, expected)
  22. result = pi._data[::2].to_timestamp()
  23. expected = dti._data[::2]
  24. # TODO: can we get the freq to round-trip?
  25. tm.assert_datetime_array_equal(result, expected, check_freq=False)
  26. result = pi[::-1].to_timestamp()
  27. expected = dti[::-1]
  28. tm.assert_index_equal(result, expected)
  29. result = pi._data[::-1].to_timestamp()
  30. expected = dti._data[::-1]
  31. tm.assert_datetime_array_equal(result, expected, check_freq=False)
  32. result = pi[::2][::-1].to_timestamp()
  33. expected = dti[::2][::-1]
  34. tm.assert_index_equal(result, expected)
  35. result = pi._data[::2][::-1].to_timestamp()
  36. expected = dti._data[::2][::-1]
  37. tm.assert_datetime_array_equal(result, expected, check_freq=False)
  38. def test_to_timestamp_freq(self):
  39. idx = period_range("2017", periods=12, freq="A-DEC")
  40. result = idx.to_timestamp()
  41. expected = date_range("2017", periods=12, freq="AS-JAN")
  42. tm.assert_index_equal(result, expected)
  43. def test_to_timestamp_pi_nat(self):
  44. # GH#7228
  45. index = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
  46. result = index.to_timestamp("D")
  47. expected = DatetimeIndex(
  48. [NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], name="idx"
  49. )
  50. tm.assert_index_equal(result, expected)
  51. assert result.name == "idx"
  52. result2 = result.to_period(freq="M")
  53. tm.assert_index_equal(result2, index)
  54. assert result2.name == "idx"
  55. result3 = result.to_period(freq="3M")
  56. exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
  57. tm.assert_index_equal(result3, exp)
  58. assert result3.freqstr == "3M"
  59. msg = "Frequency must be positive, because it represents span: -2A"
  60. with pytest.raises(ValueError, match=msg):
  61. result.to_period(freq="-2A")
  62. def test_to_timestamp_preserve_name(self):
  63. index = period_range(freq="A", start="1/1/2001", end="12/1/2009", name="foo")
  64. assert index.name == "foo"
  65. conv = index.to_timestamp("D")
  66. assert conv.name == "foo"
  67. def test_to_timestamp_quarterly_bug(self):
  68. years = np.arange(1960, 2000).repeat(4)
  69. quarters = np.tile(list(range(1, 5)), 40)
  70. pindex = PeriodIndex(year=years, quarter=quarters)
  71. stamps = pindex.to_timestamp("D", "end")
  72. expected = DatetimeIndex([x.to_timestamp("D", "end") for x in pindex])
  73. tm.assert_index_equal(stamps, expected)
  74. assert stamps.freq == expected.freq
  75. def test_to_timestamp_pi_mult(self):
  76. idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M", name="idx")
  77. result = idx.to_timestamp()
  78. expected = DatetimeIndex(["2011-01-01", "NaT", "2011-02-01"], name="idx")
  79. tm.assert_index_equal(result, expected)
  80. result = idx.to_timestamp(how="E")
  81. expected = DatetimeIndex(["2011-02-28", "NaT", "2011-03-31"], name="idx")
  82. expected = expected + Timedelta(1, "D") - Timedelta(1, "ns")
  83. tm.assert_index_equal(result, expected)
  84. def test_to_timestamp_pi_combined(self):
  85. idx = period_range(start="2011", periods=2, freq="1D1H", name="idx")
  86. result = idx.to_timestamp()
  87. expected = DatetimeIndex(["2011-01-01 00:00", "2011-01-02 01:00"], name="idx")
  88. tm.assert_index_equal(result, expected)
  89. result = idx.to_timestamp(how="E")
  90. expected = DatetimeIndex(
  91. ["2011-01-02 00:59:59", "2011-01-03 01:59:59"], name="idx"
  92. )
  93. expected = expected + Timedelta(1, "s") - Timedelta(1, "ns")
  94. tm.assert_index_equal(result, expected)
  95. result = idx.to_timestamp(how="E", freq="H")
  96. expected = DatetimeIndex(["2011-01-02 00:00", "2011-01-03 01:00"], name="idx")
  97. expected = expected + Timedelta(1, "h") - Timedelta(1, "ns")
  98. tm.assert_index_equal(result, expected)
  99. def test_to_timestamp_1703(self):
  100. index = period_range("1/1/2012", periods=4, freq="D")
  101. result = index.to_timestamp()
  102. assert result[0] == Timestamp("1/1/2012")