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