test_period_range.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. NaT,
  5. Period,
  6. PeriodIndex,
  7. date_range,
  8. period_range,
  9. )
  10. import pandas._testing as tm
  11. class TestPeriodRange:
  12. def test_required_arguments(self):
  13. msg = (
  14. "Of the three parameters: start, end, and periods, exactly two "
  15. "must be specified"
  16. )
  17. with pytest.raises(ValueError, match=msg):
  18. period_range("2011-1-1", "2012-1-1", "B")
  19. @pytest.mark.parametrize("freq", ["D", "W", "M", "Q", "A"])
  20. def test_construction_from_string(self, freq):
  21. # non-empty
  22. expected = date_range(
  23. start="2017-01-01", periods=5, freq=freq, name="foo"
  24. ).to_period()
  25. start, end = str(expected[0]), str(expected[-1])
  26. result = period_range(start=start, end=end, freq=freq, name="foo")
  27. tm.assert_index_equal(result, expected)
  28. result = period_range(start=start, periods=5, freq=freq, name="foo")
  29. tm.assert_index_equal(result, expected)
  30. result = period_range(end=end, periods=5, freq=freq, name="foo")
  31. tm.assert_index_equal(result, expected)
  32. # empty
  33. expected = PeriodIndex([], freq=freq, name="foo")
  34. result = period_range(start=start, periods=0, freq=freq, name="foo")
  35. tm.assert_index_equal(result, expected)
  36. result = period_range(end=end, periods=0, freq=freq, name="foo")
  37. tm.assert_index_equal(result, expected)
  38. result = period_range(start=end, end=start, freq=freq, name="foo")
  39. tm.assert_index_equal(result, expected)
  40. def test_construction_from_period(self):
  41. # upsampling
  42. start, end = Period("2017Q1", freq="Q"), Period("2018Q1", freq="Q")
  43. expected = date_range(
  44. start="2017-03-31", end="2018-03-31", freq="M", name="foo"
  45. ).to_period()
  46. result = period_range(start=start, end=end, freq="M", name="foo")
  47. tm.assert_index_equal(result, expected)
  48. # downsampling
  49. start, end = Period("2017-1", freq="M"), Period("2019-12", freq="M")
  50. expected = date_range(
  51. start="2017-01-31", end="2019-12-31", freq="Q", name="foo"
  52. ).to_period()
  53. result = period_range(start=start, end=end, freq="Q", name="foo")
  54. tm.assert_index_equal(result, expected)
  55. # test for issue # 21793
  56. start, end = Period("2017Q1", freq="Q"), Period("2018Q1", freq="Q")
  57. idx = period_range(start=start, end=end, freq="Q", name="foo")
  58. result = idx == idx.values
  59. expected = np.array([True, True, True, True, True])
  60. tm.assert_numpy_array_equal(result, expected)
  61. # empty
  62. expected = PeriodIndex([], freq="W", name="foo")
  63. result = period_range(start=start, periods=0, freq="W", name="foo")
  64. tm.assert_index_equal(result, expected)
  65. result = period_range(end=end, periods=0, freq="W", name="foo")
  66. tm.assert_index_equal(result, expected)
  67. result = period_range(start=end, end=start, freq="W", name="foo")
  68. tm.assert_index_equal(result, expected)
  69. def test_errors(self):
  70. # not enough params
  71. msg = (
  72. "Of the three parameters: start, end, and periods, "
  73. "exactly two must be specified"
  74. )
  75. with pytest.raises(ValueError, match=msg):
  76. period_range(start="2017Q1")
  77. with pytest.raises(ValueError, match=msg):
  78. period_range(end="2017Q1")
  79. with pytest.raises(ValueError, match=msg):
  80. period_range(periods=5)
  81. with pytest.raises(ValueError, match=msg):
  82. period_range()
  83. # too many params
  84. with pytest.raises(ValueError, match=msg):
  85. period_range(start="2017Q1", end="2018Q1", periods=8, freq="Q")
  86. # start/end NaT
  87. msg = "start and end must not be NaT"
  88. with pytest.raises(ValueError, match=msg):
  89. period_range(start=NaT, end="2018Q1")
  90. with pytest.raises(ValueError, match=msg):
  91. period_range(start="2017Q1", end=NaT)
  92. # invalid periods param
  93. msg = "periods must be a number, got foo"
  94. with pytest.raises(TypeError, match=msg):
  95. period_range(start="2017Q1", periods="foo")