From afe985c54ce39004504098790797df3803e5071d Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 10 Dec 2023 10:07:56 -0800 Subject: [PATCH 1/2] rename test_period_asfreq->test_period --- pandas/tests/tslibs/{test_period_asfreq.py => test_period.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pandas/tests/tslibs/{test_period_asfreq.py => test_period.py} (100%) diff --git a/pandas/tests/tslibs/test_period_asfreq.py b/pandas/tests/tslibs/test_period.py similarity index 100% rename from pandas/tests/tslibs/test_period_asfreq.py rename to pandas/tests/tslibs/test_period.py From b88e2d1f70ae2a7069f53c51fbc6d967f04439fe Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 10 Dec 2023 10:27:51 -0800 Subject: [PATCH 2/2] TST: de-duplicate PeriodIndex constructor tests --- .../tests/indexes/period/test_constructors.py | 159 +++++++++++------- pandas/tests/indexes/period/test_period.py | 136 --------------- .../tests/indexes/period/test_period_range.py | 137 ++++++++++----- pandas/tests/scalar/period/test_period.py | 57 ++++--- pandas/tests/tslibs/test_period.py | 7 + 5 files changed, 232 insertions(+), 264 deletions(-) diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index aecd3b3bace9a..d53581fab40c7 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -19,7 +19,54 @@ from pandas.core.arrays import PeriodArray +class TestPeriodIndexDisallowedFreqs: + @pytest.mark.parametrize( + "freq,freq_depr", + [ + ("2M", "2ME"), + ("2Q-MAR", "2QE-MAR"), + ("2Y-FEB", "2YE-FEB"), + ], + ) + def test_period_index_frequency_ME_error_message(self, freq, freq_depr): + # GH#52064 + msg = f"for Period, please use '{freq[1:]}' instead of '{freq_depr[1:]}'" + + with pytest.raises(ValueError, match=msg): + PeriodIndex(["2020-01-01", "2020-01-02"], freq=freq_depr) + + with pytest.raises(ValueError, match=msg): + period_range(start="2020-01-01", end="2020-01-02", freq=freq_depr) + + @pytest.mark.parametrize("freq_depr", ["2SME", "2CBME", "2BYE"]) + def test_period_index_frequency_invalid_freq(self, freq_depr): + # GH#9586 + msg = f"Invalid frequency: {freq_depr[1:]}" + + with pytest.raises(ValueError, match=msg): + period_range("2020-01", "2020-05", freq=freq_depr) + with pytest.raises(ValueError, match=msg): + PeriodIndex(["2020-01", "2020-05"], freq=freq_depr) + + class TestPeriodIndex: + def test_from_ordinals(self): + Period(ordinal=-1000, freq="Y") + Period(ordinal=0, freq="Y") + + msg = "The 'ordinal' keyword in PeriodIndex is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + idx1 = PeriodIndex(ordinal=[-1, 0, 1], freq="Y") + with tm.assert_produces_warning(FutureWarning, match=msg): + idx2 = PeriodIndex(ordinal=np.array([-1, 0, 1]), freq="Y") + tm.assert_index_equal(idx1, idx2) + + alt1 = PeriodIndex.from_ordinals([-1, 0, 1], freq="Y") + tm.assert_index_equal(alt1, idx1) + + alt2 = PeriodIndex.from_ordinals(np.array([-1, 0, 1]), freq="Y") + tm.assert_index_equal(alt2, idx2) + def test_keyword_mismatch(self): # GH#55961 we should get exactly one of data/ordinals/**fields per = Period("2016-01-01", "D") @@ -131,11 +178,6 @@ def test_constructor_field_arrays(self): exp = period_range("2007-01", periods=3, freq="M") tm.assert_index_equal(idx, exp) - def test_constructor_U(self): - # U was used as undefined period - with pytest.raises(ValueError, match="Invalid frequency: X"): - period_range("2007-1-1", periods=500, freq="X") - def test_constructor_nano(self): idx = period_range( start=Period(ordinal=1, freq="ns"), @@ -371,49 +413,12 @@ def test_constructor_mixed(self): exp = PeriodIndex(["2011-01-01", "NaT", "2012-01-01"], freq="D") tm.assert_index_equal(idx, exp) - def test_constructor_simple_new(self): - idx = period_range("2007-01", name="p", periods=2, freq="M") - - with pytest.raises(AssertionError, match=""): - idx._simple_new(idx, name="p") - - result = idx._simple_new(idx._data, name="p") - tm.assert_index_equal(result, idx) - - msg = "Should be numpy array of type i8" - with pytest.raises(AssertionError, match=msg): - # Need ndarray, not int64 Index - type(idx._data)._simple_new(Index(idx.asi8), dtype=idx.dtype) - - arr = type(idx._data)._simple_new(idx.asi8, dtype=idx.dtype) - result = idx._simple_new(arr, name="p") - tm.assert_index_equal(result, idx) - - def test_constructor_simple_new_empty(self): - # GH13079 - idx = PeriodIndex([], freq="M", name="p") - with pytest.raises(AssertionError, match=""): - idx._simple_new(idx, name="p") - - result = idx._simple_new(idx._data, name="p") - tm.assert_index_equal(result, idx) - @pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])]) def test_constructor_floats(self, floats): - with pytest.raises(AssertionError, match="