Skip to content

Commit feacb6f

Browse files
authored
TST: de-duplicate PeriodIndex constructor tests (pandas-dev#56435)
* rename test_period_asfreq->test_period * TST: de-duplicate PeriodIndex constructor tests
1 parent 8d259c0 commit feacb6f

File tree

5 files changed

+232
-264
lines changed

5 files changed

+232
-264
lines changed

pandas/tests/indexes/period/test_constructors.py

+94-65
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,54 @@
1919
from pandas.core.arrays import PeriodArray
2020

2121

22+
class TestPeriodIndexDisallowedFreqs:
23+
@pytest.mark.parametrize(
24+
"freq,freq_depr",
25+
[
26+
("2M", "2ME"),
27+
("2Q-MAR", "2QE-MAR"),
28+
("2Y-FEB", "2YE-FEB"),
29+
],
30+
)
31+
def test_period_index_frequency_ME_error_message(self, freq, freq_depr):
32+
# GH#52064
33+
msg = f"for Period, please use '{freq[1:]}' instead of '{freq_depr[1:]}'"
34+
35+
with pytest.raises(ValueError, match=msg):
36+
PeriodIndex(["2020-01-01", "2020-01-02"], freq=freq_depr)
37+
38+
with pytest.raises(ValueError, match=msg):
39+
period_range(start="2020-01-01", end="2020-01-02", freq=freq_depr)
40+
41+
@pytest.mark.parametrize("freq_depr", ["2SME", "2CBME", "2BYE"])
42+
def test_period_index_frequency_invalid_freq(self, freq_depr):
43+
# GH#9586
44+
msg = f"Invalid frequency: {freq_depr[1:]}"
45+
46+
with pytest.raises(ValueError, match=msg):
47+
period_range("2020-01", "2020-05", freq=freq_depr)
48+
with pytest.raises(ValueError, match=msg):
49+
PeriodIndex(["2020-01", "2020-05"], freq=freq_depr)
50+
51+
2252
class TestPeriodIndex:
53+
def test_from_ordinals(self):
54+
Period(ordinal=-1000, freq="Y")
55+
Period(ordinal=0, freq="Y")
56+
57+
msg = "The 'ordinal' keyword in PeriodIndex is deprecated"
58+
with tm.assert_produces_warning(FutureWarning, match=msg):
59+
idx1 = PeriodIndex(ordinal=[-1, 0, 1], freq="Y")
60+
with tm.assert_produces_warning(FutureWarning, match=msg):
61+
idx2 = PeriodIndex(ordinal=np.array([-1, 0, 1]), freq="Y")
62+
tm.assert_index_equal(idx1, idx2)
63+
64+
alt1 = PeriodIndex.from_ordinals([-1, 0, 1], freq="Y")
65+
tm.assert_index_equal(alt1, idx1)
66+
67+
alt2 = PeriodIndex.from_ordinals(np.array([-1, 0, 1]), freq="Y")
68+
tm.assert_index_equal(alt2, idx2)
69+
2370
def test_keyword_mismatch(self):
2471
# GH#55961 we should get exactly one of data/ordinals/**fields
2572
per = Period("2016-01-01", "D")
@@ -131,11 +178,6 @@ def test_constructor_field_arrays(self):
131178
exp = period_range("2007-01", periods=3, freq="M")
132179
tm.assert_index_equal(idx, exp)
133180

134-
def test_constructor_U(self):
135-
# U was used as undefined period
136-
with pytest.raises(ValueError, match="Invalid frequency: X"):
137-
period_range("2007-1-1", periods=500, freq="X")
138-
139181
def test_constructor_nano(self):
140182
idx = period_range(
141183
start=Period(ordinal=1, freq="ns"),
@@ -371,49 +413,12 @@ def test_constructor_mixed(self):
371413
exp = PeriodIndex(["2011-01-01", "NaT", "2012-01-01"], freq="D")
372414
tm.assert_index_equal(idx, exp)
373415

374-
def test_constructor_simple_new(self):
375-
idx = period_range("2007-01", name="p", periods=2, freq="M")
376-
377-
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"):
378-
idx._simple_new(idx, name="p")
379-
380-
result = idx._simple_new(idx._data, name="p")
381-
tm.assert_index_equal(result, idx)
382-
383-
msg = "Should be numpy array of type i8"
384-
with pytest.raises(AssertionError, match=msg):
385-
# Need ndarray, not int64 Index
386-
type(idx._data)._simple_new(Index(idx.asi8), dtype=idx.dtype)
387-
388-
arr = type(idx._data)._simple_new(idx.asi8, dtype=idx.dtype)
389-
result = idx._simple_new(arr, name="p")
390-
tm.assert_index_equal(result, idx)
391-
392-
def test_constructor_simple_new_empty(self):
393-
# GH13079
394-
idx = PeriodIndex([], freq="M", name="p")
395-
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"):
396-
idx._simple_new(idx, name="p")
397-
398-
result = idx._simple_new(idx._data, name="p")
399-
tm.assert_index_equal(result, idx)
400-
401416
@pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])])
402417
def test_constructor_floats(self, floats):
403-
with pytest.raises(AssertionError, match="<class "):
404-
PeriodIndex._simple_new(floats)
405-
406418
msg = "PeriodIndex does not allow floating point in construction"
407419
with pytest.raises(TypeError, match=msg):
408420
PeriodIndex(floats)
409421

410-
def test_constructor_nat(self):
411-
msg = "start and end must not be NaT"
412-
with pytest.raises(ValueError, match=msg):
413-
period_range(start="NaT", end="2011-01-01", freq="M")
414-
with pytest.raises(ValueError, match=msg):
415-
period_range(start="2011-01-01", end="NaT", freq="M")
416-
417422
def test_constructor_year_and_quarter(self):
418423
year = Series([2001, 2002, 2003])
419424
quarter = year - 2000
@@ -493,7 +498,7 @@ def test_constructor_freq_combined(self):
493498
expected = PeriodIndex(["2016-01-01 00:00", "2016-01-02 01:00"], freq="25h")
494499
tm.assert_index_equal(pidx, expected)
495500

496-
def test_constructor(self):
501+
def test_period_range_length(self):
497502
pi = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
498503
assert len(pi) == 9
499504

@@ -539,9 +544,12 @@ def test_constructor(self):
539544
assert (i1 == i2).all()
540545
assert i1.freq == i2.freq
541546

547+
def test_infer_freq_from_first_element(self):
548+
msg = "Period with BDay freq is deprecated"
542549
with tm.assert_produces_warning(FutureWarning, match=msg):
550+
start = Period("02-Apr-2005", "B")
543551
end_intv = Period("2005-05-01", "B")
544-
i1 = period_range(start=start, end=end_intv)
552+
period_range(start=start, end=end_intv)
545553

546554
# infer freq from first element
547555
i2 = PeriodIndex([end_intv, Period("2005-05-05", "B")])
@@ -553,7 +561,12 @@ def test_constructor(self):
553561
assert len(i2) == 2
554562
assert i2[0] == end_intv
555563

564+
def test_mixed_freq_raises(self):
556565
# Mixed freq should fail
566+
msg = "Period with BDay freq is deprecated"
567+
with tm.assert_produces_warning(FutureWarning, match=msg):
568+
end_intv = Period("2005-05-01", "B")
569+
557570
vals = [end_intv, Period("2006-12-31", "w")]
558571
msg = r"Input has different freq=W-SUN from PeriodIndex\(freq=B\)"
559572
with pytest.raises(IncompatibleFrequency, match=msg):
@@ -562,10 +575,6 @@ def test_constructor(self):
562575
with pytest.raises(IncompatibleFrequency, match=msg):
563576
PeriodIndex(vals)
564577

565-
# tuple freq disallowed GH#34703
566-
with pytest.raises(TypeError, match="pass as a string instead"):
567-
Period("2006-12-31", ("w", 1))
568-
569578
@pytest.mark.parametrize(
570579
"freq", ["M", "Q", "Y", "D", "B", "min", "s", "ms", "us", "ns", "h"]
571580
)
@@ -594,20 +603,39 @@ def test_map_with_string_constructor(self):
594603
# lastly, values should compare equal
595604
tm.assert_index_equal(res, expected)
596605

597-
@pytest.mark.parametrize(
598-
"freq, freq_msg",
599-
[
600-
(offsets.BYearBegin(), "BYearBegin"),
601-
(offsets.YearBegin(2), "YearBegin"),
602-
(offsets.QuarterBegin(startingMonth=12), "QuarterBegin"),
603-
(offsets.BusinessMonthEnd(2), "BusinessMonthEnd"),
604-
],
605-
)
606-
def test_offsets_not_supported(self, freq, freq_msg):
607-
# GH#55785
608-
msg = f"{freq_msg} is not supported as period frequency"
609-
with pytest.raises(TypeError, match=msg):
610-
Period(year=2014, freq=freq)
606+
607+
class TestSimpleNew:
608+
def test_constructor_simple_new(self):
609+
idx = period_range("2007-01", name="p", periods=2, freq="M")
610+
611+
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"):
612+
idx._simple_new(idx, name="p")
613+
614+
result = idx._simple_new(idx._data, name="p")
615+
tm.assert_index_equal(result, idx)
616+
617+
msg = "Should be numpy array of type i8"
618+
with pytest.raises(AssertionError, match=msg):
619+
# Need ndarray, not int64 Index
620+
type(idx._data)._simple_new(Index(idx.asi8), dtype=idx.dtype)
621+
622+
arr = type(idx._data)._simple_new(idx.asi8, dtype=idx.dtype)
623+
result = idx._simple_new(arr, name="p")
624+
tm.assert_index_equal(result, idx)
625+
626+
def test_constructor_simple_new_empty(self):
627+
# GH13079
628+
idx = PeriodIndex([], freq="M", name="p")
629+
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"):
630+
idx._simple_new(idx, name="p")
631+
632+
result = idx._simple_new(idx._data, name="p")
633+
tm.assert_index_equal(result, idx)
634+
635+
@pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])])
636+
def test_period_index_simple_new_disallows_floats(self, floats):
637+
with pytest.raises(AssertionError, match="<class "):
638+
PeriodIndex._simple_new(floats)
611639

612640

613641
class TestShallowCopy:
@@ -638,6 +666,7 @@ def test_constructor_cant_cast_period(self):
638666
Series(period_range("2000-01-01", periods=10, freq="D"), dtype=float)
639667

640668
def test_constructor_cast_object(self):
641-
s = Series(period_range("1/1/2000", periods=10), dtype=PeriodDtype("D"))
642-
exp = Series(period_range("1/1/2000", periods=10))
643-
tm.assert_series_equal(s, exp)
669+
pi = period_range("1/1/2000", periods=10)
670+
ser = Series(pi, dtype=PeriodDtype("D"))
671+
exp = Series(pi)
672+
tm.assert_series_equal(ser, exp)

pandas/tests/indexes/period/test_period.py

-136
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import numpy as np
22
import pytest
33

4-
from pandas._libs.tslibs.period import IncompatibleFrequency
5-
64
from pandas import (
75
Index,
86
NaT,
@@ -17,11 +15,6 @@
1715

1816

1917
class TestPeriodIndex:
20-
def test_make_time_series(self):
21-
index = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
22-
series = Series(1, index=index)
23-
assert isinstance(series, Series)
24-
2518
def test_view_asi8(self):
2619
idx = PeriodIndex([], freq="M")
2720

@@ -66,74 +59,6 @@ def test_values(self):
6659
exp = np.array([14975, -9223372036854775808], dtype=np.int64)
6760
tm.assert_numpy_array_equal(idx.asi8, exp)
6861

69-
def test_period_index_length(self):
70-
pi = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
71-
assert len(pi) == 9
72-
73-
pi = period_range(freq="Q", start="1/1/2001", end="12/1/2009")
74-
assert len(pi) == 4 * 9
75-
76-
pi = period_range(freq="M", start="1/1/2001", end="12/1/2009")
77-
assert len(pi) == 12 * 9
78-
79-
msg = "Period with BDay freq is deprecated"
80-
with tm.assert_produces_warning(FutureWarning, match=msg):
81-
start = Period("02-Apr-2005", "B")
82-
i1 = period_range(start=start, periods=20)
83-
assert len(i1) == 20
84-
assert i1.freq == start.freq
85-
assert i1[0] == start
86-
87-
end_intv = Period("2006-12-31", "W")
88-
i1 = period_range(end=end_intv, periods=10)
89-
assert len(i1) == 10
90-
assert i1.freq == end_intv.freq
91-
assert i1[-1] == end_intv
92-
93-
end_intv = Period("2006-12-31", "1w")
94-
i2 = period_range(end=end_intv, periods=10)
95-
assert len(i1) == len(i2)
96-
assert (i1 == i2).all()
97-
assert i1.freq == i2.freq
98-
99-
msg = "start and end must have same freq"
100-
msg2 = "Period with BDay freq is deprecated"
101-
with pytest.raises(ValueError, match=msg):
102-
with tm.assert_produces_warning(FutureWarning, match=msg2):
103-
period_range(start=start, end=end_intv)
104-
105-
with tm.assert_produces_warning(FutureWarning, match=msg2):
106-
end_intv = Period("2005-05-01", "B")
107-
with tm.assert_produces_warning(FutureWarning, match=msg2):
108-
i1 = period_range(start=start, end=end_intv)
109-
110-
msg = (
111-
"Of the three parameters: start, end, and periods, exactly two "
112-
"must be specified"
113-
)
114-
with pytest.raises(ValueError, match=msg):
115-
period_range(start=start)
116-
117-
# infer freq from first element
118-
with tm.assert_produces_warning(FutureWarning, match=msg2):
119-
i2 = PeriodIndex([end_intv, Period("2005-05-05", "B")])
120-
assert len(i2) == 2
121-
assert i2[0] == end_intv
122-
123-
with tm.assert_produces_warning(FutureWarning, match=msg2):
124-
i2 = PeriodIndex(np.array([end_intv, Period("2005-05-05", "B")]))
125-
assert len(i2) == 2
126-
assert i2[0] == end_intv
127-
128-
# Mixed freq should fail
129-
vals = [end_intv, Period("2006-12-31", "w")]
130-
msg = r"Input has different freq=W-SUN from PeriodIndex\(freq=B\)"
131-
with pytest.raises(IncompatibleFrequency, match=msg):
132-
PeriodIndex(vals)
133-
vals = np.array(vals)
134-
with pytest.raises(ValueError, match=msg):
135-
PeriodIndex(vals)
136-
13762
@pytest.mark.parametrize(
13863
"field",
13964
[
@@ -210,23 +135,6 @@ def test_index_unique(self):
210135
tm.assert_index_equal(idx.unique(), expected)
211136
assert idx.nunique() == 3
212137

213-
def test_negative_ordinals(self):
214-
Period(ordinal=-1000, freq="Y")
215-
Period(ordinal=0, freq="Y")
216-
217-
msg = "The 'ordinal' keyword in PeriodIndex is deprecated"
218-
with tm.assert_produces_warning(FutureWarning, match=msg):
219-
idx1 = PeriodIndex(ordinal=[-1, 0, 1], freq="Y")
220-
with tm.assert_produces_warning(FutureWarning, match=msg):
221-
idx2 = PeriodIndex(ordinal=np.array([-1, 0, 1]), freq="Y")
222-
tm.assert_index_equal(idx1, idx2)
223-
224-
alt1 = PeriodIndex.from_ordinals([-1, 0, 1], freq="Y")
225-
tm.assert_index_equal(alt1, idx1)
226-
227-
alt2 = PeriodIndex.from_ordinals(np.array([-1, 0, 1]), freq="Y")
228-
tm.assert_index_equal(alt2, idx2)
229-
230138
def test_pindex_fieldaccessor_nat(self):
231139
idx = PeriodIndex(
232140
["2011-01", "2011-02", "NaT", "2012-03", "2012-04"], freq="D", name="name"
@@ -281,50 +189,6 @@ def test_map(self):
281189
exp = Index([x.ordinal for x in index])
282190
tm.assert_index_equal(result, exp)
283191

284-
@pytest.mark.parametrize(
285-
"freq,freq_depr",
286-
[
287-
("2M", "2ME"),
288-
("2Q-MAR", "2QE-MAR"),
289-
("2Y-FEB", "2YE-FEB"),
290-
],
291-
)
292-
def test_period_index_frequency_ME_error_message(self, freq, freq_depr):
293-
# GH#52064
294-
msg = f"for Period, please use '{freq[1:]}' instead of '{freq_depr[1:]}'"
295-
296-
with pytest.raises(ValueError, match=msg):
297-
PeriodIndex(["2020-01-01", "2020-01-02"], freq=freq_depr)
298-
299-
def test_H_deprecated_from_time_series(self):
300-
# GH#52536
301-
msg = "'H' is deprecated and will be removed in a future version."
302-
303-
with tm.assert_produces_warning(FutureWarning, match=msg):
304-
index = period_range(freq="2H", start="1/1/2001", end="12/1/2009")
305-
series = Series(1, index=index)
306-
assert isinstance(series, Series)
307-
308-
@pytest.mark.parametrize("freq_depr", ["2A", "A-DEC", "200A-AUG"])
309-
def test_a_deprecated_from_time_series(self, freq_depr):
310-
# GH#52536
311-
freq_msg = freq_depr[freq_depr.index("A") :]
312-
msg = f"'{freq_msg}' is deprecated and will be removed in a future version, "
313-
f"please use 'Y{freq_msg[1:]}' instead."
314-
315-
with tm.assert_produces_warning(FutureWarning, match=msg):
316-
index = period_range(freq=freq_depr, start="1/1/2001", end="12/1/2009")
317-
series = Series(1, index=index)
318-
assert isinstance(series, Series)
319-
320-
@pytest.mark.parametrize("freq_depr", ["2SME", "2CBME", "2BYE"])
321-
def test_period_index_frequency_invalid_freq(self, freq_depr):
322-
# GH#9586
323-
msg = f"Invalid frequency: {freq_depr[1:]}"
324-
325-
with pytest.raises(ValueError, match=msg):
326-
period_range("2020-01", "2020-05", freq=freq_depr)
327-
328192

329193
def test_maybe_convert_timedelta():
330194
pi = PeriodIndex(["2000", "2001"], freq="D")

0 commit comments

Comments
 (0)