diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index bc187361493c0..e9492b45c3be0 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -348,6 +348,7 @@ Deprecations - Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`) - Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`) - Deprecated logical operation between two non boolean :class:`Series` with different indexes always coercing the result to bool dtype. In a future version, this will maintain the return type of the inputs. (:issue:`52500`, :issue:`52538`) +- Deprecated :class:`Period` and :class:`PeriodDtype` with ``BDay`` freq, use a :class:`DatetimeIndex` with ``BDay`` freq instead (:issue:`53446`) - Deprecated :func:`value_counts`, use ``pd.Series(obj).value_counts()`` instead (:issue:`47862`) - Deprecated :meth:`Series.first` and :meth:`DataFrame.first` (please create a mask and filter using ``.loc`` instead) (:issue:`45908`) - Deprecated :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`53631`) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index d4997b5f00975..c37e9cd7ef1f3 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -107,7 +107,10 @@ from pandas._libs.tslibs.offsets cimport ( to_offset, ) -from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG +from pandas._libs.tslibs.offsets import ( + INVALID_FREQ_ERR_MSG, + BDay, +) cdef: enum: @@ -2812,6 +2815,18 @@ class Period(_Period): dt.hour, dt.minute, dt.second, dt.microsecond, 1000*nanosecond, base) + if isinstance(freq, BDay): + # GH#53446 + import warnings + + from pandas.util._exceptions import find_stack_level + warnings.warn( + "Period with BDay freq is deprecated and will be removed " + "in a future version. Use a DatetimeIndex with BDay freq instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + return cls._from_ordinal(ordinal, freq) diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index 886c0f389ebeb..564d727b4be5a 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -441,7 +441,8 @@ def makeTimedeltaIndex( def makePeriodIndex(k: int = 10, name=None, **kwargs) -> PeriodIndex: dt = datetime(2000, 1, 1) - return pd.period_range(start=dt, periods=k, freq="B", name=name, **kwargs) + pi = pd.period_range(start=dt, periods=k, freq="D", name=name, **kwargs) + return pi def makeMultiIndex(k: int = 10, names=None, **kwargs): diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 5d3d5a1a6b344..88d73589e0339 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -42,6 +42,7 @@ PeriodDtypeBase, abbrev_to_npy_unit, ) +from pandas._libs.tslibs.offsets import BDay from pandas.compat import pa_version_under7p0 from pandas.errors import PerformanceWarning from pandas.util._exceptions import find_stack_level @@ -966,6 +967,15 @@ def __new__(cls, freq): if not isinstance(freq, BaseOffset): freq = cls._parse_dtype_strict(freq) + if isinstance(freq, BDay): + # GH#53446 + warnings.warn( + "PeriodDtype[B] is deprecated and will be removed in a future " + "version. Use a DatetimeIndex with freq='B' instead", + FutureWarning, + stacklevel=find_stack_level(), + ) + try: dtype_code = cls._cache_dtypes[freq] except KeyError: diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index 15af2dc6aa7bd..90e6f29dd98ab 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -276,10 +276,22 @@ def maybe_convert_index(ax: Axes, data): freq_str = _get_period_alias(freq) - if isinstance(data.index, ABCDatetimeIndex): - data = data.tz_localize(None).to_period(freq=freq_str) - elif isinstance(data.index, ABCPeriodIndex): - data.index = data.index.asfreq(freq=freq_str) + import warnings + + with warnings.catch_warnings(): + # suppress Period[B] deprecation warning + # TODO: need to find an alternative to this before the deprecation + # is enforced! + warnings.filterwarnings( + "ignore", + r"PeriodDtype\[B\] is deprecated", + category=FutureWarning, + ) + + if isinstance(data.index, ABCDatetimeIndex): + data = data.tz_localize(None).to_period(freq=freq_str) + elif isinstance(data.index, ABCPeriodIndex): + data.index = data.index.asfreq(freq=freq_str) return data diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 7df17c42134e9..8f87749a4ed6e 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -2,6 +2,7 @@ import array import re +import warnings import numpy as np import pytest @@ -48,7 +49,10 @@ def period_index(freqstr): the PeriodIndex behavior. """ # TODO: non-monotone indexes; NaTs, different start dates - pi = pd.period_range(start=Timestamp("2000-01-01"), periods=100, freq=freqstr) + with warnings.catch_warnings(): + # suppress deprecation of Period[B] + warnings.simplefilter("ignore") + pi = pd.period_range(start=Timestamp("2000-01-01"), periods=100, freq=freqstr) return pi @@ -192,6 +196,9 @@ def test_take_fill(self, arr1d): result = arr.take([-1, 1], allow_fill=True, fill_value=NaT) assert result[0] is NaT + @pytest.mark.filterwarnings( + "ignore:Period with BDay freq is deprecated:FutureWarning" + ) def test_take_fill_str(self, arr1d): # Cast str fill_value matching other fill_value-taking methods result = arr1d.take([-1, 1], allow_fill=True, fill_value=str(arr1d[-1])) @@ -745,6 +752,7 @@ def test_astype_object(self, arr1d): assert asobj.dtype == "O" assert list(asobj) == list(dti) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_to_period(self, datetime_index, freqstr): dti = datetime_index arr = DatetimeArray(dti) @@ -999,6 +1007,8 @@ def test_take_fill_valid(self, timedelta_index, fixed_now_ts): arr.take([-1, 1], allow_fill=True, fill_value=value) +@pytest.mark.filterwarnings(r"ignore:Period with BDay freq is deprecated:FutureWarning") +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") class TestPeriodArray(SharedTests): index_cls = PeriodIndex array_cls = PeriodArray diff --git a/pandas/tests/base/test_unique.py b/pandas/tests/base/test_unique.py index ab49dd3052d31..4c845d8f24d01 100644 --- a/pandas/tests/base/test_unique.py +++ b/pandas/tests/base/test_unique.py @@ -6,6 +6,7 @@ from pandas.tests.base.common import allow_na_ops +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_unique(index_or_series_obj): obj = index_or_series_obj obj = np.repeat(obj, range(1, len(obj) + 1)) @@ -27,6 +28,7 @@ def test_unique(index_or_series_obj): tm.assert_numpy_array_equal(result, expected) +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") @pytest.mark.parametrize("null_obj", [np.nan, None]) def test_unique_null(null_obj, index_or_series_obj): obj = index_or_series_obj diff --git a/pandas/tests/base/test_value_counts.py b/pandas/tests/base/test_value_counts.py index 89f3c005c52f0..3cdfb7fe41e92 100644 --- a/pandas/tests/base/test_value_counts.py +++ b/pandas/tests/base/test_value_counts.py @@ -19,6 +19,7 @@ from pandas.tests.base.common import allow_na_ops +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_value_counts(index_or_series_obj): obj = index_or_series_obj obj = np.repeat(obj, range(1, len(obj) + 1)) @@ -54,6 +55,7 @@ def test_value_counts(index_or_series_obj): @pytest.mark.parametrize("null_obj", [np.nan, None]) +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_value_counts_null(null_obj, index_or_series_obj): orig = index_or_series_obj obj = orig.copy() diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index cdb33db540743..f57093c29b733 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -445,10 +445,13 @@ def test_construction(self): def test_cannot_use_custom_businessday(self): # GH#52534 msg = "CustomBusinessDay cannot be used with Period or PeriodDtype" + msg2 = r"PeriodDtype\[B\] is deprecated" with pytest.raises(TypeError, match=msg): - PeriodDtype("C") + with tm.assert_produces_warning(FutureWarning, match=msg2): + PeriodDtype("C") with pytest.raises(TypeError, match=msg): - PeriodDtype(pd.offsets.CustomBusinessDay()) + with tm.assert_produces_warning(FutureWarning, match=msg2): + PeriodDtype(pd.offsets.CustomBusinessDay()) def test_subclass(self): a = PeriodDtype("period[D]") diff --git a/pandas/tests/frame/methods/test_shift.py b/pandas/tests/frame/methods/test_shift.py index 2c36535d82ba5..c024cb0387ff2 100644 --- a/pandas/tests/frame/methods/test_shift.py +++ b/pandas/tests/frame/methods/test_shift.py @@ -249,20 +249,20 @@ def test_shift_with_periodindex(self, frame_or_series): else: tm.assert_numpy_array_equal(unshifted.dropna().values, ps.values[:-1]) - shifted2 = ps.shift(1, "B") - shifted3 = ps.shift(1, offsets.BDay()) + shifted2 = ps.shift(1, "D") + shifted3 = ps.shift(1, offsets.Day()) tm.assert_equal(shifted2, shifted3) - tm.assert_equal(ps, shifted2.shift(-1, "B")) + tm.assert_equal(ps, shifted2.shift(-1, "D")) msg = "does not match PeriodIndex freq" with pytest.raises(ValueError, match=msg): - ps.shift(freq="D") + ps.shift(freq="W") # legacy support - shifted4 = ps.shift(1, freq="B") + shifted4 = ps.shift(1, freq="D") tm.assert_equal(shifted2, shifted4) - shifted5 = ps.shift(1, freq=offsets.BDay()) + shifted5 = ps.shift(1, freq=offsets.Day()) tm.assert_equal(shifted5, shifted4) def test_shift_other_axis(self): @@ -492,10 +492,10 @@ def test_period_index_frame_shift_with_freq(self, frame_or_series): unshifted = shifted.shift(-1, freq="infer") tm.assert_equal(unshifted, ps) - shifted2 = ps.shift(freq="B") + shifted2 = ps.shift(freq="D") tm.assert_equal(shifted, shifted2) - shifted3 = ps.shift(freq=offsets.BDay()) + shifted3 = ps.shift(freq=offsets.Day()) tm.assert_equal(shifted, shifted3) def test_datetime_frame_shift_with_freq(self, datetime_frame, frame_or_series): @@ -524,7 +524,7 @@ def test_datetime_frame_shift_with_freq(self, datetime_frame, frame_or_series): def test_period_index_frame_shift_with_freq_error(self, frame_or_series): ps = tm.makePeriodFrame() ps = tm.get_obj(ps, frame_or_series) - msg = "Given freq M does not match PeriodIndex freq B" + msg = "Given freq M does not match PeriodIndex freq D" with pytest.raises(ValueError, match=msg): ps.shift(freq="M") diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index ee9c4f05991a0..f7d132a1c0bf0 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -341,6 +341,7 @@ def test_to_csv_nrows(self, nrows): "r_idx_type, c_idx_type", [("i", "i"), ("s", "s"), ("s", "dt"), ("p", "p")] ) @pytest.mark.parametrize("ncols", [1, 2, 3, 4]) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_to_csv_idx_types(self, nrows, r_idx_type, c_idx_type, ncols): df = tm.makeCustomDataframe( nrows, ncols, r_idx_type=r_idx_type, c_idx_type=c_idx_type diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index d1e48192edf0a..6a4d4ff2a5092 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -974,6 +974,7 @@ def test_idxmin(self, float_frame, int_frame, skipna, axis): tm.assert_series_equal(result, expected) @pytest.mark.parametrize("axis", [0, 1]) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_idxmin_empty(self, index, skipna, axis): # GH53265 if axis == 0: @@ -1014,6 +1015,7 @@ def test_idxmax(self, float_frame, int_frame, skipna, axis): tm.assert_series_equal(result, expected) @pytest.mark.parametrize("axis", [0, 1]) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_idxmax_empty(self, index, skipna, axis): # GH53265 if axis == 0: diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 1e9c4b446c4d0..63c0fec7b90b6 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -161,6 +161,7 @@ class TestGrouping: tm.makePeriodIndex, ], ) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_grouper_index_types(self, index): # related GH5375 # groupby misbehaving when using a Floatlike index diff --git a/pandas/tests/indexes/datetimes/methods/test_to_period.py b/pandas/tests/indexes/datetimes/methods/test_to_period.py index 6c41b76aa113c..fe0b9464c7f45 100644 --- a/pandas/tests/indexes/datetimes/methods/test_to_period.py +++ b/pandas/tests/indexes/datetimes/methods/test_to_period.py @@ -107,6 +107,7 @@ def test_to_period_infer(self): tm.assert_index_equal(pi1, pi2) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_period_dt64_round_trip(self): dti = date_range("1/1/2000", "1/7/2002", freq="B") pi = dti.to_period() diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index fd0928b82ecbf..29f9295513c31 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -622,6 +622,7 @@ def test_union_with_duplicates_keep_ea_dtype(dupe_val, any_numeric_ea_dtype): tm.assert_index_equal(result, expected) +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_union_duplicates(index, request): # GH#38977 if index.empty or isinstance(index, (IntervalIndex, CategoricalIndex)): diff --git a/pandas/tests/indexes/period/methods/test_to_timestamp.py b/pandas/tests/indexes/period/methods/test_to_timestamp.py index 164ed3ec43996..8bb0c3518c835 100644 --- a/pandas/tests/indexes/period/methods/test_to_timestamp.py +++ b/pandas/tests/indexes/period/methods/test_to_timestamp.py @@ -18,7 +18,7 @@ class TestToTimestamp: def test_to_timestamp_non_contiguous(self): # GH#44100 - dti = date_range("2021-10-18", periods=9, freq="B") + dti = date_range("2021-10-18", periods=9, freq="D") pi = dti.to_period() result = pi[::2].to_timestamp() diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index e3e0212607f91..7d4d681659ab6 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -61,10 +61,15 @@ def test_index_object_dtype(self, values_constructor): def test_constructor_use_start_freq(self): # GH #1118 - p = Period("4/2/2012", freq="B") - expected = period_range(start="4/2/2012", periods=10, freq="B") - - index = period_range(start=p, periods=10) + msg1 = "Period with BDay freq is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg1): + p = Period("4/2/2012", freq="B") + msg2 = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg2): + expected = period_range(start="4/2/2012", periods=10, freq="B") + + with tm.assert_produces_warning(FutureWarning, match=msg2): + index = period_range(start=p, periods=10) tm.assert_index_equal(index, expected) def test_constructor_field_arrays(self): @@ -440,7 +445,9 @@ def test_constructor(self): pi = period_range(freq="D", start="1/1/2001", end="12/31/2009") assert len(pi) == 365 * 9 + 2 - pi = period_range(freq="B", start="1/1/2001", end="12/31/2009") + msg = "Period with BDay freq is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + pi = period_range(freq="B", start="1/1/2001", end="12/31/2009") assert len(pi) == 261 * 9 pi = period_range(freq="H", start="1/1/2001", end="12/31/2001 23:00") @@ -452,8 +459,9 @@ def test_constructor(self): pi = period_range(freq="S", start="1/1/2001", end="1/1/2001 23:59:59") assert len(pi) == 24 * 60 * 60 - start = Period("02-Apr-2005", "B") - i1 = period_range(start=start, periods=20) + with tm.assert_produces_warning(FutureWarning, match=msg): + start = Period("02-Apr-2005", "B") + i1 = period_range(start=start, periods=20) assert len(i1) == 20 assert i1.freq == start.freq assert i1[0] == start @@ -470,15 +478,17 @@ def test_constructor(self): assert (i1 == i2).all() assert i1.freq == i2.freq - end_intv = Period("2005-05-01", "B") - i1 = period_range(start=start, end=end_intv) + with tm.assert_produces_warning(FutureWarning, match=msg): + end_intv = Period("2005-05-01", "B") + i1 = period_range(start=start, end=end_intv) - # infer freq from first element - i2 = PeriodIndex([end_intv, Period("2005-05-05", "B")]) + # infer freq from first element + i2 = PeriodIndex([end_intv, Period("2005-05-05", "B")]) assert len(i2) == 2 assert i2[0] == end_intv - i2 = PeriodIndex(np.array([end_intv, Period("2005-05-05", "B")])) + with tm.assert_produces_warning(FutureWarning, match=msg): + i2 = PeriodIndex(np.array([end_intv, Period("2005-05-05", "B")])) assert len(i2) == 2 assert i2[0] == end_intv @@ -498,6 +508,10 @@ def test_constructor(self): @pytest.mark.parametrize( "freq", ["M", "Q", "A", "D", "B", "T", "S", "L", "U", "N", "H"] ) + @pytest.mark.filterwarnings( + r"ignore:Period with BDay freq is deprecated:FutureWarning" + ) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_recreate_from_data(self, freq): org = period_range(start="2001/04/01", freq=freq, periods=1) idx = PeriodIndex(org.values, freq=freq) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 412d66de11ba3..6d8ae1793d5ec 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -76,8 +76,10 @@ def test_period_index_length(self): pi = period_range(freq="M", start="1/1/2001", end="12/1/2009") assert len(pi) == 12 * 9 - start = Period("02-Apr-2005", "B") - i1 = period_range(start=start, periods=20) + msg = "Period with BDay freq is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + start = Period("02-Apr-2005", "B") + i1 = period_range(start=start, periods=20) assert len(i1) == 20 assert i1.freq == start.freq assert i1[0] == start @@ -95,11 +97,15 @@ def test_period_index_length(self): assert i1.freq == i2.freq msg = "start and end must have same freq" + msg2 = "Period with BDay freq is deprecated" with pytest.raises(ValueError, match=msg): - period_range(start=start, end=end_intv) + with tm.assert_produces_warning(FutureWarning, match=msg2): + period_range(start=start, end=end_intv) - end_intv = Period("2005-05-01", "B") - i1 = period_range(start=start, end=end_intv) + with tm.assert_produces_warning(FutureWarning, match=msg2): + end_intv = Period("2005-05-01", "B") + with tm.assert_produces_warning(FutureWarning, match=msg2): + i1 = period_range(start=start, end=end_intv) msg = ( "Of the three parameters: start, end, and periods, exactly two " @@ -109,11 +115,13 @@ def test_period_index_length(self): period_range(start=start) # infer freq from first element - i2 = PeriodIndex([end_intv, Period("2005-05-05", "B")]) + with tm.assert_produces_warning(FutureWarning, match=msg2): + i2 = PeriodIndex([end_intv, Period("2005-05-05", "B")]) assert len(i2) == 2 assert i2[0] == end_intv - i2 = PeriodIndex(np.array([end_intv, Period("2005-05-05", "B")])) + with tm.assert_produces_warning(FutureWarning, match=msg2): + i2 = PeriodIndex(np.array([end_intv, Period("2005-05-05", "B")])) assert len(i2) == 2 assert i2[0] == end_intv @@ -153,7 +161,6 @@ def test_period_index_length(self): period_range(freq="Q", start="1/1/2001", end="12/1/2002"), period_range(freq="M", start="1/1/2001", end="1/1/2002"), period_range(freq="D", start="12/1/2001", end="6/1/2001"), - period_range(freq="B", start="12/1/2001", end="6/1/2001"), period_range(freq="H", start="12/31/2001", end="1/1/2002 23:00"), period_range(freq="Min", start="12/31/2001", end="1/1/2002 00:20"), period_range( @@ -237,6 +244,8 @@ def test_pindex_multiples(self): assert pi.freq == offsets.MonthEnd(2) assert pi.freqstr == "2M" + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") + @pytest.mark.filterwarnings("ignore:Period with BDay freq:FutureWarning") def test_iteration(self): index = period_range(start="1/1/10", periods=4, freq="B") diff --git a/pandas/tests/indexes/period/test_scalar_compat.py b/pandas/tests/indexes/period/test_scalar_compat.py index a42b8496b0bcf..c96444981ff14 100644 --- a/pandas/tests/indexes/period/test_scalar_compat.py +++ b/pandas/tests/indexes/period/test_scalar_compat.py @@ -1,5 +1,7 @@ """Tests for PeriodIndex behaving like a vectorized Period scalar""" +import pytest + from pandas import ( Timedelta, date_range, @@ -22,6 +24,10 @@ def test_end_time(self): expected_index += Timedelta(1, "D") - Timedelta(1, "ns") tm.assert_index_equal(index.end_time, expected_index) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") + @pytest.mark.filterwarnings( + "ignore:Period with BDay freq is deprecated:FutureWarning" + ) def test_end_time_business_friday(self): # GH#34449 pi = period_range("1990-01-05", freq="B", periods=1) diff --git a/pandas/tests/indexes/period/test_setops.py b/pandas/tests/indexes/period/test_setops.py index 22182416c79fd..68858b4466b56 100644 --- a/pandas/tests/indexes/period/test_setops.py +++ b/pandas/tests/indexes/period/test_setops.py @@ -1,4 +1,5 @@ import numpy as np +import pytest import pandas as pd from pandas import ( @@ -336,6 +337,7 @@ def test_intersection_equal_duplicates(self): result = idx_dup.intersection(idx_dup) tm.assert_index_equal(result, idx) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_union_duplicates(self): # GH#36289 idx = period_range("2011-01-01", periods=2) diff --git a/pandas/tests/indexes/period/test_tools.py b/pandas/tests/indexes/period/test_tools.py index 41b76d6aced23..13509bd58b4b8 100644 --- a/pandas/tests/indexes/period/test_tools.py +++ b/pandas/tests/indexes/period/test_tools.py @@ -30,6 +30,10 @@ class TestPeriodRepresentation: ("A", 1970), ], ) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") + @pytest.mark.filterwarnings( + "ignore:Period with BDay freq is deprecated:FutureWarning" + ) def test_freq(self, freq, base_date): rng = period_range(start=base_date, periods=10, freq=freq) exp = np.arange(10, dtype=np.int64) diff --git a/pandas/tests/indexes/test_any_index.py b/pandas/tests/indexes/test_any_index.py index 3b3d6dbaf697f..8a951c3c38be6 100644 --- a/pandas/tests/indexes/test_any_index.py +++ b/pandas/tests/indexes/test_any_index.py @@ -40,6 +40,7 @@ def test_mutability(index): index[0] = index[0] +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_map_identity_mapping(index, request): # GH#12766 diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 3d3c27bc8f6c7..00d82fe353497 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -297,9 +297,9 @@ def test_constructor_empty(self, value, klass): @pytest.mark.parametrize( "empty,klass", [ - (PeriodIndex([], freq="B"), PeriodIndex), - (PeriodIndex(iter([]), freq="B"), PeriodIndex), - (PeriodIndex((_ for _ in []), freq="B"), PeriodIndex), + (PeriodIndex([], freq="D"), PeriodIndex), + (PeriodIndex(iter([]), freq="D"), PeriodIndex), + (PeriodIndex((_ for _ in []), freq="D"), PeriodIndex), (RangeIndex(step=1), RangeIndex), (MultiIndex(levels=[[1, 2], ["blue", "red"]], codes=[[], []]), MultiIndex), ], @@ -557,6 +557,7 @@ def test_map_dictlike_simple(self, mapper): lambda values, index: Series(values, index), ], ) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_map_dictlike(self, index, mapper, request): # GH 12756 if isinstance(index, CategoricalIndex): @@ -780,6 +781,7 @@ def test_drop_tuple(self, values, to_drop): with pytest.raises(KeyError, match=msg): removed.drop(drop_me) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_drop_with_duplicates_in_index(self, index): # GH38051 if len(index) == 0 or isinstance(index, MultiIndex): diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index b73bd7c78f009..9bdc16ab2e0e2 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -243,6 +243,8 @@ def test_unique(self, index_flat): result = i.unique() tm.assert_index_equal(result, expected) + @pytest.mark.filterwarnings("ignore:Period with BDay freq:FutureWarning") + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_searchsorted_monotonic(self, index_flat, request): # GH17271 index = index_flat @@ -293,6 +295,7 @@ def test_searchsorted_monotonic(self, index_flat, request): with pytest.raises(ValueError, match=msg): index._searchsorted_monotonic(value, side="left") + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_drop_duplicates(self, index_flat, keep): # MultiIndex is tested separately index = index_flat @@ -328,6 +331,7 @@ def test_drop_duplicates(self, index_flat, keep): expected_dropped = holder(pd.Series(idx).drop_duplicates(keep=keep)) tm.assert_index_equal(idx.drop_duplicates(keep=keep), expected_dropped) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_drop_duplicates_no_duplicates(self, index_flat): # MultiIndex is tested separately index = index_flat @@ -355,6 +359,7 @@ def test_drop_duplicates_inplace(self, index): with pytest.raises(TypeError, match=msg): index.drop_duplicates(inplace=True) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_has_duplicates(self, index_flat): # MultiIndex tested separately in: # tests/indexes/multi/test_unique_and_duplicates. @@ -434,12 +439,14 @@ def test_hasnans_isnans(self, index_flat): assert idx.hasnans is True +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") @pytest.mark.parametrize("na_position", [None, "middle"]) def test_sort_values_invalid_na_position(index_with_missing, na_position): with pytest.raises(ValueError, match=f"invalid na_position: {na_position}"): index_with_missing.sort_values(na_position=na_position) +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") @pytest.mark.parametrize("na_position", ["first", "last"]) def test_sort_values_with_missing(index_with_missing, na_position, request): # GH 35584. Test that sort_values works with missing values, diff --git a/pandas/tests/indexes/test_datetimelike.py b/pandas/tests/indexes/test_datetimelike.py index b315b1ac141cd..71cc7f29c62bc 100644 --- a/pandas/tests/indexes/test_datetimelike.py +++ b/pandas/tests/indexes/test_datetimelike.py @@ -111,6 +111,7 @@ def test_map_callable(self, simple_index): lambda values, index: pd.Series(values, index, dtype=object), ], ) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_map_dictlike(self, mapper, simple_index): index = simple_index expected = index + index.freq diff --git a/pandas/tests/indexes/test_old_base.py b/pandas/tests/indexes/test_old_base.py index 588aa458c8b04..3b627f2fae845 100644 --- a/pandas/tests/indexes/test_old_base.py +++ b/pandas/tests/indexes/test_old_base.py @@ -237,6 +237,7 @@ def test_repr_max_seq_item_setting(self, simple_index): repr(idx) assert "..." not in str(idx) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_ensure_copied_data(self, index): # Check the "copy" argument of each Index.__new__ is honoured # GH12309 @@ -454,6 +455,7 @@ def test_delete_base(self, index): with pytest.raises(IndexError, match=msg): index.delete(length) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_equals(self, index): if isinstance(index, IntervalIndex): # IntervalIndex tested separately, the index.equals(index.astype(object)) @@ -651,6 +653,7 @@ def test_map(self, simple_index): lambda values, index: Series(values, index), ], ) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_map_dictlike(self, mapper, simple_index): idx = simple_index if isinstance(idx, CategoricalIndex): diff --git a/pandas/tests/indexes/test_setops.py b/pandas/tests/indexes/test_setops.py index 8b12d3ccb2c2f..31a867acdb5d1 100644 --- a/pandas/tests/indexes/test_setops.py +++ b/pandas/tests/indexes/test_setops.py @@ -13,10 +13,12 @@ from pandas.core.dtypes.cast import find_common_type from pandas import ( + CategoricalDtype, CategoricalIndex, DatetimeTZDtype, Index, MultiIndex, + PeriodDtype, RangeIndex, Series, Timestamp, @@ -67,6 +69,7 @@ def test_union_different_types(index_flat, index_flat2, request): common_dtype = find_common_type([idx1.dtype, idx2.dtype]) warn = None + msg = "'<' not supported between" if not len(idx1) or not len(idx2): pass elif (idx1.dtype.kind == "c" and (not lib.is_np_dtype(idx2.dtype, "iufc"))) or ( @@ -74,6 +77,19 @@ def test_union_different_types(index_flat, index_flat2, request): ): # complex objects non-sortable warn = RuntimeWarning + elif ( + isinstance(idx1.dtype, PeriodDtype) and isinstance(idx2.dtype, CategoricalDtype) + ) or ( + isinstance(idx2.dtype, PeriodDtype) and isinstance(idx1.dtype, CategoricalDtype) + ): + warn = FutureWarning + msg = r"PeriodDtype\[B\] is deprecated" + mark = pytest.mark.xfail( + reason="Warning not produced on all builds", + raises=AssertionError, + strict=False, + ) + request.node.add_marker(mark) any_uint64 = np.uint64 in (idx1.dtype, idx2.dtype) idx1_signed = is_signed_integer_dtype(idx1.dtype) @@ -84,7 +100,7 @@ def test_union_different_types(index_flat, index_flat2, request): idx1 = idx1.sort_values() idx2 = idx2.sort_values() - with tm.assert_produces_warning(warn, match="'<' not supported between"): + with tm.assert_produces_warning(warn, match=msg): res1 = idx1.union(idx2) res2 = idx2.union(idx1) @@ -175,6 +191,7 @@ def test_set_ops_error_cases(self, case, method, index): with pytest.raises(TypeError, match=msg): getattr(index, method)(case) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_intersection_base(self, index): if isinstance(index, CategoricalIndex): return @@ -203,6 +220,7 @@ def test_intersection_base(self, index): @pytest.mark.filterwarnings( "ignore:Falling back on a non-pyarrow:pandas.errors.PerformanceWarning" ) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_union_base(self, index): first = index[3:] second = index[:5] @@ -227,6 +245,7 @@ def test_union_base(self, index): with pytest.raises(TypeError, match=msg): first.union([1, 2, 3]) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") @pytest.mark.filterwarnings( "ignore:Falling back on a non-pyarrow:pandas.errors.PerformanceWarning" ) @@ -255,6 +274,7 @@ def test_difference_base(self, sort, index): with pytest.raises(TypeError, match=msg): first.difference([1, 2, 3], sort) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") @pytest.mark.filterwarnings( "ignore:Falling back on a non-pyarrow:pandas.errors.PerformanceWarning" ) @@ -420,6 +440,7 @@ def test_intersect_unequal(self, index_flat, fname, sname, expected_name): expected = index[1:].set_names(expected_name).sort_values() tm.assert_index_equal(intersect, expected) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_intersection_name_retention_with_nameless(self, index): if isinstance(index, MultiIndex): index = index.rename(list(range(index.nlevels))) @@ -473,6 +494,7 @@ def test_intersection_difference_match_empty(self, index, sort): tm.assert_index_equal(inter, diff, exact=True) +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") @pytest.mark.filterwarnings( "ignore:Falling back on a non-pyarrow:pandas.errors.PerformanceWarning" ) diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index 3bf8c2eaa7e94..f74da0d48a3be 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -708,7 +708,7 @@ def test_3levels_leading_period_index(): pi = pd.PeriodIndex( ["20181101 1100", "20181101 1200", "20181102 1300", "20181102 1400"], name="datetime", - freq="B", + freq="D", ) lev2 = ["A", "A", "Z", "W"] lev3 = ["B", "C", "Q", "F"] diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 4017a0e3a2f80..f6e7f7355bc73 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1656,6 +1656,7 @@ def test_loc_iloc_getitem_ellipsis(self, obj, indexer): result = indexer(obj)[...] tm.assert_equal(result, obj) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_loc_iloc_getitem_leading_ellipses(self, series_with_simple_index, indexer): obj = series_with_simple_index key = 0 if (indexer is tm.iloc or len(obj) == 0) else obj.index[0] @@ -2963,6 +2964,8 @@ def test_loc_set_int_dtype(): tm.assert_frame_equal(df, expected) +@pytest.mark.filterwarnings(r"ignore:Period with BDay freq is deprecated:FutureWarning") +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_loc_periodindex_3_levels(): # GH#24091 p_index = PeriodIndex( @@ -3246,6 +3249,8 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): def test_getitem_loc_str_periodindex(self): # GH#33964 - index = pd.period_range(start="2000", periods=20, freq="B") - series = Series(range(20), index=index) - assert series.loc["2000-01-14"] == 9 + msg = "Period with BDay freq is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + index = pd.period_range(start="2000", periods=20, freq="B") + series = Series(range(20), index=index) + assert series.loc["2000-01-14"] == 9 diff --git a/pandas/tests/io/pytables/test_put.py b/pandas/tests/io/pytables/test_put.py index 910f83e0b997c..1411c9d600897 100644 --- a/pandas/tests/io/pytables/test_put.py +++ b/pandas/tests/io/pytables/test_put.py @@ -217,6 +217,7 @@ def test_put_mixed_type(setup_path): ["fixed", tm.makePeriodIndex], ], ) +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_store_index_types(setup_path, format, index): # GH5386 # test storing various index types diff --git a/pandas/tests/io/pytables/test_read.py b/pandas/tests/io/pytables/test_read.py index 61dabf15653f0..5a270a7d88a1d 100644 --- a/pandas/tests/io/pytables/test_read.py +++ b/pandas/tests/io/pytables/test_read.py @@ -342,6 +342,8 @@ def test_read_hdf_series_mode_r(tmp_path, format, setup_path): tm.assert_series_equal(result, series) +@pytest.mark.filterwarnings(r"ignore:Period with BDay freq is deprecated:FutureWarning") +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_read_py2_hdf_file_in_py3(datapath): # GH 16781 diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 17b7eef011a0a..00a9587241db0 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -911,6 +911,7 @@ def test_columns_multiindex_modified(tmp_path, setup_path): assert cols2load_original == cols2load +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_to_hdf_with_object_column_names(tmp_path, setup_path): # GH9057 diff --git a/pandas/tests/io/pytables/test_time_series.py b/pandas/tests/io/pytables/test_time_series.py index 262f25e77b69c..8f6f8ee6c5682 100644 --- a/pandas/tests/io/pytables/test_time_series.py +++ b/pandas/tests/io/pytables/test_time_series.py @@ -21,6 +21,7 @@ def test_store_datetime_fractional_secs(setup_path): assert store["a"].index[0] == dt +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_tseries_indices_series(setup_path): with ensure_clean_store(setup_path) as store: idx = tm.makeDateIndex(10) @@ -42,6 +43,7 @@ def test_tseries_indices_series(setup_path): tm.assert_class_equal(result.index, ser.index, obj="series index") +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_tseries_indices_frame(setup_path): with ensure_clean_store(setup_path) as store: idx = tm.makeDateIndex(10) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index f3b3fb43edf36..5cbe7b6e30c84 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -339,7 +339,9 @@ def test_xcompat_plot_params_x_compat(self): ax = df.plot() lines = ax.get_lines() assert not isinstance(lines[0].get_xdata(), PeriodIndex) - assert isinstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex) + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + assert isinstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex) def test_xcompat_plot_params_context_manager(self): df = tm.makeTimeDataFrame() @@ -355,7 +357,9 @@ def test_xcompat_plot_period(self): ax = df.plot() lines = ax.get_lines() assert not isinstance(lines[0].get_xdata(), PeriodIndex) - assert isinstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex) + msg = r"PeriodDtype\[B\] is deprecated " + with tm.assert_produces_warning(FutureWarning, match=msg): + assert isinstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex) _check_ticks_props(ax, xrot=0) def test_period_compat(self): diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index af8bcd943765e..4b121841b4e4d 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -221,6 +221,7 @@ def test_line_plot_period_mlt_frame(self, frqncy): freq = df.index.asfreq(df.index.freq.rule_code).freq _check_plot_works(df.plot, freq) + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") @pytest.mark.parametrize( "freq", ["S", "T", "H", "D", "W", "M", "Q-DEC", "A", "1B30Min"] ) @@ -319,11 +320,16 @@ def test_irregular_datetime64_repr_bug(self): def test_business_freq(self): bts = tm.makePeriodSeries() + msg = r"PeriodDtype\[B\] is deprecated" + dt = bts.index[0].to_timestamp() + with tm.assert_produces_warning(FutureWarning, match=msg): + bts.index = period_range(start=dt, periods=len(bts), freq="B") _, ax = mpl.pyplot.subplots() bts.plot(ax=ax) assert ax.get_lines()[0].get_xydata()[0, 0] == bts.index[0].ordinal idx = ax.get_lines()[0].get_xdata() - assert PeriodIndex(data=idx).freqstr == "B" + with tm.assert_produces_warning(FutureWarning, match=msg): + assert PeriodIndex(data=idx).freqstr == "B" def test_business_freq_convert(self): bts = tm.makeTimeSeries(300).asfreq("BM") @@ -360,8 +366,13 @@ def test_dataframe(self): _, ax = mpl.pyplot.subplots() bts.plot(ax=ax) idx = ax.get_lines()[0].get_xdata() - tm.assert_index_equal(bts.index.to_period(), PeriodIndex(idx)) + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + tm.assert_index_equal(bts.index.to_period(), PeriodIndex(idx)) + @pytest.mark.filterwarnings( + "ignore:Period with BDay freq is deprecated:FutureWarning" + ) @pytest.mark.parametrize( "obj", [ @@ -407,7 +418,9 @@ def test_get_finder(self): def test_finder_daily(self): day_lst = [10, 40, 252, 400, 950, 2750, 10000] - xpl1 = xpl2 = [Period("1999-1-1", freq="B").ordinal] * len(day_lst) + msg = "Period with BDay freq is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + xpl1 = xpl2 = [Period("1999-1-1", freq="B").ordinal] * len(day_lst) rs1 = [] rs2 = [] for n in day_lst: @@ -698,14 +711,16 @@ def test_mixed_freq_regular_first(self): ax2 = s2.plot(style="g", ax=ax) lines = ax2.get_lines() - idx1 = PeriodIndex(lines[0].get_xdata()) - idx2 = PeriodIndex(lines[1].get_xdata()) + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + idx1 = PeriodIndex(lines[0].get_xdata()) + idx2 = PeriodIndex(lines[1].get_xdata()) - tm.assert_index_equal(idx1, s1.index.to_period("B")) - tm.assert_index_equal(idx2, s2.index.to_period("B")) + tm.assert_index_equal(idx1, s1.index.to_period("B")) + tm.assert_index_equal(idx2, s2.index.to_period("B")) - left, right = ax2.get_xlim() - pidx = s1.index.to_period() + left, right = ax2.get_xlim() + pidx = s1.index.to_period() assert left <= pidx[0].ordinal assert right >= pidx[-1].ordinal @@ -730,12 +745,14 @@ def test_mixed_freq_regular_first_df(self): s1.plot(ax=ax) ax2 = s2.plot(style="g", ax=ax) lines = ax2.get_lines() - idx1 = PeriodIndex(lines[0].get_xdata()) - idx2 = PeriodIndex(lines[1].get_xdata()) - assert idx1.equals(s1.index.to_period("B")) - assert idx2.equals(s2.index.to_period("B")) - left, right = ax2.get_xlim() - pidx = s1.index.to_period() + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + idx1 = PeriodIndex(lines[0].get_xdata()) + idx2 = PeriodIndex(lines[1].get_xdata()) + assert idx1.equals(s1.index.to_period("B")) + assert idx2.equals(s2.index.to_period("B")) + left, right = ax2.get_xlim() + pidx = s1.index.to_period() assert left <= pidx[0].ordinal assert right >= pidx[-1].ordinal @@ -802,10 +819,13 @@ def test_mixed_freq_lf_first_hourly(self): for line in ax.get_lines(): assert PeriodIndex(data=line.get_xdata()).freq == "T" + @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_mixed_freq_irreg_period(self): ts = tm.makeTimeSeries() irreg = ts.iloc[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 18, 29]] - rng = period_range("1/3/2000", periods=30, freq="B") + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + rng = period_range("1/3/2000", periods=30, freq="B") ps = Series(np.random.randn(len(rng)), rng) _, ax = mpl.pyplot.subplots() irreg.plot(ax=ax) diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 2f5deb7b2bbd5..57381352163e7 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -49,6 +49,9 @@ def get_objs(): class TestReductions: + @pytest.mark.filterwarnings( + "ignore:Period with BDay freq is deprecated:FutureWarning" + ) @pytest.mark.parametrize("opname", ["max", "min"]) @pytest.mark.parametrize("obj", get_objs()) def test_ops(self, opname, obj): diff --git a/pandas/tests/reductions/test_stat_reductions.py b/pandas/tests/reductions/test_stat_reductions.py index b1462d109561f..5f6fcba50142c 100644 --- a/pandas/tests/reductions/test_stat_reductions.py +++ b/pandas/tests/reductions/test_stat_reductions.py @@ -50,7 +50,10 @@ def test_period_mean(self, box, freq): # shuffle so that we are not just working with monotone-increasing dti = dti.take([4, 1, 3, 10, 9, 7, 8, 5, 0, 2, 6]) - parr = dti._data.to_period(freq) + warn = FutureWarning if freq == "B" else None + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(warn, match=msg): + parr = dti._data.to_period(freq) obj = box(parr) with pytest.raises(TypeError, match="ambiguous"): obj.mean() diff --git a/pandas/tests/resample/conftest.py b/pandas/tests/resample/conftest.py index 38f682c9c4f5a..2708ab3f8184f 100644 --- a/pandas/tests/resample/conftest.py +++ b/pandas/tests/resample/conftest.py @@ -1,4 +1,5 @@ from datetime import datetime +import warnings import numpy as np import pytest @@ -63,7 +64,15 @@ def simple_period_range_series(): """ def _simple_period_range_series(start, end, freq="D"): - rng = period_range(start, end, freq=freq) + with warnings.catch_warnings(): + # suppress Period[B] deprecation warning + msg = "|".join(["Period with BDay freq", r"PeriodDtype\[B\] is deprecated"]) + warnings.filterwarnings( + "ignore", + msg, + category=FutureWarning, + ) + rng = period_range(start, end, freq=freq) return Series(np.random.randn(len(rng)), index=rng) return _simple_period_range_series diff --git a/pandas/tests/resample/test_base.py b/pandas/tests/resample/test_base.py index 22bd8f9c8ced3..7a76a21a6c579 100644 --- a/pandas/tests/resample/test_base.py +++ b/pandas/tests/resample/test_base.py @@ -277,15 +277,20 @@ def test_resample_size_empty_dataframe(freq, empty_frame_dti): tm.assert_series_equal(result, expected) +@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") @pytest.mark.parametrize("index", tm.all_timeseries_index_generator(0)) @pytest.mark.parametrize("dtype", [float, int, object, "datetime64[ns]"]) def test_resample_empty_dtypes(index, dtype, resample_method): # Empty series were sometimes causing a segfault (for the functions # with Cython bounds-checking disabled) or an IndexError. We just run # them to ensure they no longer do. (GH #10228) + if isinstance(index, PeriodIndex): + # GH#53511 + index = PeriodIndex([], freq="B", name=index.name) empty_series_dti = Series([], index, dtype) + rs = empty_series_dti.resample("d", group_keys=False) try: - getattr(empty_series_dti.resample("d", group_keys=False), resample_method)() + getattr(rs, resample_method)() except DataError: # Ignore these since some combinations are invalid # (ex: doing mean with dtype of np.object_) diff --git a/pandas/tests/resample/test_period_index.py b/pandas/tests/resample/test_period_index.py index 20b997bdca873..9246ed4647e63 100644 --- a/pandas/tests/resample/test_period_index.py +++ b/pandas/tests/resample/test_period_index.py @@ -108,10 +108,12 @@ def test_annual_upsample_cases( self, targ, conv, meth, month, simple_period_range_series ): ts = simple_period_range_series("1/1/1990", "12/31/1991", freq=f"A-{month}") - - result = getattr(ts.resample(targ, convention=conv), meth)() - expected = result.to_timestamp(targ, how=conv) - expected = expected.asfreq(targ, meth).to_period() + warn = FutureWarning if targ == "B" else None + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(warn, match=msg): + result = getattr(ts.resample(targ, convention=conv), meth)() + expected = result.to_timestamp(targ, how=conv) + expected = expected.asfreq(targ, meth).to_period() tm.assert_series_equal(result, expected) def test_basic_downsample(self, simple_period_range_series): @@ -187,18 +189,25 @@ def test_quarterly_upsample( ): freq = f"Q-{month}" ts = simple_period_range_series("1/1/1990", "12/31/1995", freq=freq) - result = ts.resample(target, convention=convention).ffill() - expected = result.to_timestamp(target, how=convention) - expected = expected.asfreq(target, "ffill").to_period() + warn = FutureWarning if target == "B" else None + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(warn, match=msg): + result = ts.resample(target, convention=convention).ffill() + expected = result.to_timestamp(target, how=convention) + expected = expected.asfreq(target, "ffill").to_period() tm.assert_series_equal(result, expected) @pytest.mark.parametrize("target", ["D", "B"]) @pytest.mark.parametrize("convention", ["start", "end"]) def test_monthly_upsample(self, target, convention, simple_period_range_series): ts = simple_period_range_series("1/1/1990", "12/31/1995", freq="M") - result = ts.resample(target, convention=convention).ffill() - expected = result.to_timestamp(target, how=convention) - expected = expected.asfreq(target, "ffill").to_period() + + warn = None if target == "D" else FutureWarning + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(warn, match=msg): + result = ts.resample(target, convention=convention).ffill() + expected = result.to_timestamp(target, how=convention) + expected = expected.asfreq(target, "ffill").to_period() tm.assert_series_equal(result, expected) def test_resample_basic(self): @@ -363,9 +372,13 @@ def test_fill_method_and_how_upsample(self): def test_weekly_upsample(self, day, target, convention, simple_period_range_series): freq = f"W-{day}" ts = simple_period_range_series("1/1/1990", "12/31/1995", freq=freq) - result = ts.resample(target, convention=convention).ffill() - expected = result.to_timestamp(target, how=convention) - expected = expected.asfreq(target, "ffill").to_period() + + warn = None if target == "D" else FutureWarning + msg = r"PeriodDtype\[B\] is deprecated" + with tm.assert_produces_warning(warn, match=msg): + result = ts.resample(target, convention=convention).ffill() + expected = result.to_timestamp(target, how=convention) + expected = expected.asfreq(target, "ffill").to_period() tm.assert_series_equal(result, expected) def test_resample_to_timestamps(self, simple_period_range_series): diff --git a/pandas/tests/scalar/period/test_asfreq.py b/pandas/tests/scalar/period/test_asfreq.py index e652c63d46f18..f6c1675766210 100644 --- a/pandas/tests/scalar/period/test_asfreq.py +++ b/pandas/tests/scalar/period/test_asfreq.py @@ -9,11 +9,15 @@ Timestamp, offsets, ) +import pandas._testing as tm + +bday_msg = "Period with BDay freq is deprecated" class TestFreqConversion: """Test frequency conversion of date objects""" + @pytest.mark.filterwarnings("ignore:Period with BDay:FutureWarning") @pytest.mark.parametrize("freq", ["A", "Q", "M", "W", "B", "D"]) def test_asfreq_near_zero(self, freq): # GH#19643, GH#19650 @@ -37,10 +41,12 @@ def test_asfreq_near_zero_weekly(self): def test_to_timestamp_out_of_bounds(self): # GH#19643, used to incorrectly give Timestamp in 1754 - per = Period("0001-01-01", freq="B") + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + per = Period("0001-01-01", freq="B") msg = "Out of bounds nanosecond timestamp" with pytest.raises(OutOfBoundsDatetime, match=msg): - per.to_timestamp() + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + per.to_timestamp() def test_asfreq_corner(self): val = Period(freq="A", year=2007) @@ -67,8 +73,9 @@ def test_conv_annual(self): ival_A_to_M_end = Period(freq="M", year=2007, month=12) ival_A_to_W_start = Period(freq="W", year=2007, month=1, day=1) ival_A_to_W_end = Period(freq="W", year=2007, month=12, day=31) - ival_A_to_B_start = Period(freq="B", year=2007, month=1, day=1) - ival_A_to_B_end = Period(freq="B", year=2007, month=12, day=31) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_A_to_B_start = Period(freq="B", year=2007, month=1, day=1) + ival_A_to_B_end = Period(freq="B", year=2007, month=12, day=31) ival_A_to_D_start = Period(freq="D", year=2007, month=1, day=1) ival_A_to_D_end = Period(freq="D", year=2007, month=12, day=31) ival_A_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0) @@ -99,8 +106,9 @@ def test_conv_annual(self): assert ival_A.asfreq("M", "E") == ival_A_to_M_end assert ival_A.asfreq("W", "S") == ival_A_to_W_start assert ival_A.asfreq("W", "E") == ival_A_to_W_end - assert ival_A.asfreq("B", "S") == ival_A_to_B_start - assert ival_A.asfreq("B", "E") == ival_A_to_B_end + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_A.asfreq("B", "S") == ival_A_to_B_start + assert ival_A.asfreq("B", "E") == ival_A_to_B_end assert ival_A.asfreq("D", "S") == ival_A_to_D_start assert ival_A.asfreq("D", "E") == ival_A_to_D_end assert ival_A.asfreq("H", "S") == ival_A_to_H_start @@ -137,8 +145,9 @@ def test_conv_quarterly(self): ival_Q_to_M_end = Period(freq="M", year=2007, month=3) ival_Q_to_W_start = Period(freq="W", year=2007, month=1, day=1) ival_Q_to_W_end = Period(freq="W", year=2007, month=3, day=31) - ival_Q_to_B_start = Period(freq="B", year=2007, month=1, day=1) - ival_Q_to_B_end = Period(freq="B", year=2007, month=3, day=30) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_Q_to_B_start = Period(freq="B", year=2007, month=1, day=1) + ival_Q_to_B_end = Period(freq="B", year=2007, month=3, day=30) ival_Q_to_D_start = Period(freq="D", year=2007, month=1, day=1) ival_Q_to_D_end = Period(freq="D", year=2007, month=3, day=31) ival_Q_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0) @@ -169,8 +178,9 @@ def test_conv_quarterly(self): assert ival_Q.asfreq("M", "E") == ival_Q_to_M_end assert ival_Q.asfreq("W", "S") == ival_Q_to_W_start assert ival_Q.asfreq("W", "E") == ival_Q_to_W_end - assert ival_Q.asfreq("B", "S") == ival_Q_to_B_start - assert ival_Q.asfreq("B", "E") == ival_Q_to_B_end + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_Q.asfreq("B", "S") == ival_Q_to_B_start + assert ival_Q.asfreq("B", "E") == ival_Q_to_B_end assert ival_Q.asfreq("D", "S") == ival_Q_to_D_start assert ival_Q.asfreq("D", "E") == ival_Q_to_D_end assert ival_Q.asfreq("H", "S") == ival_Q_to_H_start @@ -197,8 +207,9 @@ def test_conv_monthly(self): ival_M_to_Q = Period(freq="Q", year=2007, quarter=1) ival_M_to_W_start = Period(freq="W", year=2007, month=1, day=1) ival_M_to_W_end = Period(freq="W", year=2007, month=1, day=31) - ival_M_to_B_start = Period(freq="B", year=2007, month=1, day=1) - ival_M_to_B_end = Period(freq="B", year=2007, month=1, day=31) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_M_to_B_start = Period(freq="B", year=2007, month=1, day=1) + ival_M_to_B_end = Period(freq="B", year=2007, month=1, day=31) ival_M_to_D_start = Period(freq="D", year=2007, month=1, day=1) ival_M_to_D_end = Period(freq="D", year=2007, month=1, day=31) ival_M_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0) @@ -223,8 +234,9 @@ def test_conv_monthly(self): assert ival_M.asfreq("W", "S") == ival_M_to_W_start assert ival_M.asfreq("W", "E") == ival_M_to_W_end - assert ival_M.asfreq("B", "S") == ival_M_to_B_start - assert ival_M.asfreq("B", "E") == ival_M_to_B_end + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_M.asfreq("B", "S") == ival_M_to_B_start + assert ival_M.asfreq("B", "E") == ival_M_to_B_end assert ival_M.asfreq("D", "S") == ival_M_to_D_start assert ival_M.asfreq("D", "E") == ival_M_to_D_end assert ival_M.asfreq("H", "S") == ival_M_to_H_start @@ -285,8 +297,9 @@ def test_conv_weekly(self): else: ival_W_to_M_end_of_month = Period(freq="M", year=2007, month=2) - ival_W_to_B_start = Period(freq="B", year=2007, month=1, day=1) - ival_W_to_B_end = Period(freq="B", year=2007, month=1, day=5) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_W_to_B_start = Period(freq="B", year=2007, month=1, day=1) + ival_W_to_B_end = Period(freq="B", year=2007, month=1, day=5) ival_W_to_D_start = Period(freq="D", year=2007, month=1, day=1) ival_W_to_D_end = Period(freq="D", year=2007, month=1, day=7) ival_W_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0) @@ -313,8 +326,9 @@ def test_conv_weekly(self): assert ival_W.asfreq("M") == ival_W_to_M assert ival_W_end_of_month.asfreq("M") == ival_W_to_M_end_of_month - assert ival_W.asfreq("B", "S") == ival_W_to_B_start - assert ival_W.asfreq("B", "E") == ival_W_to_B_end + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_W.asfreq("B", "S") == ival_W_to_B_start + assert ival_W.asfreq("B", "E") == ival_W_to_B_end assert ival_W.asfreq("D", "S") == ival_W_to_D_start assert ival_W.asfreq("D", "E") == ival_W_to_D_end @@ -369,11 +383,12 @@ def test_conv_weekly_legacy(self): def test_conv_business(self): # frequency conversion tests: from Business Frequency" - ival_B = Period(freq="B", year=2007, month=1, day=1) - ival_B_end_of_year = Period(freq="B", year=2007, month=12, day=31) - ival_B_end_of_quarter = Period(freq="B", year=2007, month=3, day=30) - ival_B_end_of_month = Period(freq="B", year=2007, month=1, day=31) - ival_B_end_of_week = Period(freq="B", year=2007, month=1, day=5) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_B = Period(freq="B", year=2007, month=1, day=1) + ival_B_end_of_year = Period(freq="B", year=2007, month=12, day=31) + ival_B_end_of_quarter = Period(freq="B", year=2007, month=3, day=30) + ival_B_end_of_month = Period(freq="B", year=2007, month=1, day=31) + ival_B_end_of_week = Period(freq="B", year=2007, month=1, day=5) ival_B_to_A = Period(freq="A", year=2007) ival_B_to_Q = Period(freq="Q", year=2007, quarter=1) @@ -413,7 +428,8 @@ def test_conv_business(self): assert ival_B.asfreq("S", "S") == ival_B_to_S_start assert ival_B.asfreq("S", "E") == ival_B_to_S_end - assert ival_B.asfreq("B") == ival_B + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_B.asfreq("B") == ival_B def test_conv_daily(self): # frequency conversion tests: from Business Frequency" @@ -428,8 +444,9 @@ def test_conv_daily(self): ival_D_saturday = Period(freq="D", year=2007, month=1, day=6) ival_D_sunday = Period(freq="D", year=2007, month=1, day=7) - ival_B_friday = Period(freq="B", year=2007, month=1, day=5) - ival_B_monday = Period(freq="B", year=2007, month=1, day=8) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_B_friday = Period(freq="B", year=2007, month=1, day=5) + ival_B_monday = Period(freq="B", year=2007, month=1, day=8) ival_D_to_A = Period(freq="A", year=2007) @@ -475,11 +492,12 @@ def test_conv_daily(self): assert ival_D.asfreq("W") == ival_D_to_W assert ival_D_end_of_week.asfreq("W") == ival_D_to_W - assert ival_D_friday.asfreq("B") == ival_B_friday - assert ival_D_saturday.asfreq("B", "S") == ival_B_friday - assert ival_D_saturday.asfreq("B", "E") == ival_B_monday - assert ival_D_sunday.asfreq("B", "S") == ival_B_friday - assert ival_D_sunday.asfreq("B", "E") == ival_B_monday + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_D_friday.asfreq("B") == ival_B_friday + assert ival_D_saturday.asfreq("B", "S") == ival_B_friday + assert ival_D_saturday.asfreq("B", "E") == ival_B_monday + assert ival_D_sunday.asfreq("B", "S") == ival_B_friday + assert ival_D_sunday.asfreq("B", "E") == ival_B_monday assert ival_D.asfreq("H", "S") == ival_D_to_H_start assert ival_D.asfreq("H", "E") == ival_D_to_H_end @@ -506,7 +524,8 @@ def test_conv_hourly(self): ival_H_to_M = Period(freq="M", year=2007, month=1) ival_H_to_W = Period(freq="W", year=2007, month=1, day=7) ival_H_to_D = Period(freq="D", year=2007, month=1, day=1) - ival_H_to_B = Period(freq="B", year=2007, month=1, day=1) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_H_to_B = Period(freq="B", year=2007, month=1, day=1) ival_H_to_T_start = Period( freq="Min", year=2007, month=1, day=1, hour=0, minute=0 @@ -531,8 +550,9 @@ def test_conv_hourly(self): assert ival_H_end_of_week.asfreq("W") == ival_H_to_W assert ival_H.asfreq("D") == ival_H_to_D assert ival_H_end_of_day.asfreq("D") == ival_H_to_D - assert ival_H.asfreq("B") == ival_H_to_B - assert ival_H_end_of_bus.asfreq("B") == ival_H_to_B + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_H.asfreq("B") == ival_H_to_B + assert ival_H_end_of_bus.asfreq("B") == ival_H_to_B assert ival_H.asfreq("Min", "S") == ival_H_to_T_start assert ival_H.asfreq("Min", "E") == ival_H_to_T_end @@ -572,7 +592,8 @@ def test_conv_minutely(self): ival_T_to_M = Period(freq="M", year=2007, month=1) ival_T_to_W = Period(freq="W", year=2007, month=1, day=7) ival_T_to_D = Period(freq="D", year=2007, month=1, day=1) - ival_T_to_B = Period(freq="B", year=2007, month=1, day=1) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_T_to_B = Period(freq="B", year=2007, month=1, day=1) ival_T_to_H = Period(freq="H", year=2007, month=1, day=1, hour=0) ival_T_to_S_start = Period( @@ -592,8 +613,9 @@ def test_conv_minutely(self): assert ival_T_end_of_week.asfreq("W") == ival_T_to_W assert ival_T.asfreq("D") == ival_T_to_D assert ival_T_end_of_day.asfreq("D") == ival_T_to_D - assert ival_T.asfreq("B") == ival_T_to_B - assert ival_T_end_of_bus.asfreq("B") == ival_T_to_B + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_T.asfreq("B") == ival_T_to_B + assert ival_T_end_of_bus.asfreq("B") == ival_T_to_B assert ival_T.asfreq("H") == ival_T_to_H assert ival_T_end_of_hour.asfreq("H") == ival_T_to_H @@ -636,7 +658,8 @@ def test_conv_secondly(self): ival_S_to_M = Period(freq="M", year=2007, month=1) ival_S_to_W = Period(freq="W", year=2007, month=1, day=7) ival_S_to_D = Period(freq="D", year=2007, month=1, day=1) - ival_S_to_B = Period(freq="B", year=2007, month=1, day=1) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + ival_S_to_B = Period(freq="B", year=2007, month=1, day=1) ival_S_to_H = Period(freq="H", year=2007, month=1, day=1, hour=0) ival_S_to_T = Period(freq="Min", year=2007, month=1, day=1, hour=0, minute=0) @@ -650,8 +673,9 @@ def test_conv_secondly(self): assert ival_S_end_of_week.asfreq("W") == ival_S_to_W assert ival_S.asfreq("D") == ival_S_to_D assert ival_S_end_of_day.asfreq("D") == ival_S_to_D - assert ival_S.asfreq("B") == ival_S_to_B - assert ival_S_end_of_bus.asfreq("B") == ival_S_to_B + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert ival_S.asfreq("B") == ival_S_to_B + assert ival_S_end_of_bus.asfreq("B") == ival_S_to_B assert ival_S.asfreq("H") == ival_S_to_H assert ival_S_end_of_hour.asfreq("H") == ival_S_to_H assert ival_S.asfreq("Min") == ival_S_to_T diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 1e8c2bce1d90d..b1fb657bb2051 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -32,6 +32,8 @@ ) import pandas._testing as tm +bday_msg = "Period with BDay freq is deprecated" + class TestPeriodConstruction: def test_custom_business_day_freq_raises(self): @@ -142,20 +144,21 @@ def test_construction_from_timestamp_nanos(self): def test_construction_bday(self): # Biz day construction, roll forward if non-weekday - i1 = Period("3/10/12", freq="B") - i2 = Period("3/10/12", freq="D") - assert i1 == i2.asfreq("B") - i2 = Period("3/11/12", freq="D") - assert i1 == i2.asfreq("B") - i2 = Period("3/12/12", freq="D") - assert i1 == i2.asfreq("B") - - i3 = Period("3/10/12", freq="b") - assert i1 == i3 - - i1 = Period(year=2012, month=3, day=10, freq="B") - i2 = Period("3/12/12", freq="B") - assert i1 == i2 + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + i1 = Period("3/10/12", freq="B") + i2 = Period("3/10/12", freq="D") + assert i1 == i2.asfreq("B") + i2 = Period("3/11/12", freq="D") + assert i1 == i2.asfreq("B") + i2 = Period("3/12/12", freq="D") + assert i1 == i2.asfreq("B") + + i3 = Period("3/10/12", freq="b") + assert i1 == i3 + + i1 = Period(year=2012, month=3, day=10, freq="B") + i2 = Period("3/12/12", freq="B") + assert i1 == i2 def test_construction_quarter(self): i1 = Period(year=2005, quarter=1, freq="Q") @@ -226,9 +229,10 @@ def test_period_constructor_offsets(self): ) assert Period("2005", freq=offsets.YearEnd()) == Period("2005", freq="A") assert Period("2005", freq=offsets.MonthEnd()) == Period("2005", freq="M") - assert Period("3/10/12", freq=offsets.BusinessDay()) == Period( - "3/10/12", freq="B" - ) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert Period("3/10/12", freq=offsets.BusinessDay()) == Period( + "3/10/12", freq="B" + ) assert Period("3/10/12", freq=offsets.Day()) == Period("3/10/12", freq="D") assert Period( @@ -241,17 +245,19 @@ def test_period_constructor_offsets(self): assert Period(year=2005, month=3, day=1, freq=offsets.Day()) == Period( year=2005, month=3, day=1, freq="D" ) - assert Period(year=2012, month=3, day=10, freq=offsets.BDay()) == Period( - year=2012, month=3, day=10, freq="B" - ) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert Period(year=2012, month=3, day=10, freq=offsets.BDay()) == Period( + year=2012, month=3, day=10, freq="B" + ) expected = Period("2005-03-01", freq="3D") assert Period(year=2005, month=3, day=1, freq=offsets.Day(3)) == expected assert Period(year=2005, month=3, day=1, freq="3D") == expected - assert Period(year=2012, month=3, day=10, freq=offsets.BDay(3)) == Period( - year=2012, month=3, day=10, freq="3B" - ) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert Period(year=2012, month=3, day=10, freq=offsets.BDay(3)) == Period( + year=2012, month=3, day=10, freq="3B" + ) assert Period(200701, freq=offsets.MonthEnd()) == Period(200701, freq="M") @@ -614,6 +620,9 @@ def test_to_timestamp_mult(self): expected = Timestamp("2011-04-01") - Timedelta(1, "ns") assert p.to_timestamp(how="E") == expected + @pytest.mark.filterwarnings( + "ignore:Period with BDay freq is deprecated:FutureWarning" + ) def test_to_timestamp(self): p = Period("1982", freq="A") start_ts = p.to_timestamp(how="S") @@ -678,8 +687,9 @@ def _ex(p): assert result == expected def test_to_timestamp_business_end(self): - per = Period("1990-01-05", "B") # Friday - result = per.to_timestamp("B", how="E") + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + per = Period("1990-01-05", "B") # Friday + result = per.to_timestamp("B", how="E") expected = Timestamp("1990-01-06") - Timedelta(nanoseconds=1) assert result == expected @@ -733,6 +743,9 @@ def test_to_timestamp_microsecond(self, ts, expected, freq): ("2000-12-15", "B", "2000-12-15", "B"), ), ) + @pytest.mark.filterwarnings( + "ignore:Period with BDay freq is deprecated:FutureWarning" + ) def test_repr(self, str_ts, freq, str_res, str_freq): p = Period(str_ts, freq=freq) assert str(p) == str_res @@ -790,6 +803,9 @@ def test_freq_str(self): assert i1.freq == offsets.Minute() assert i1.freqstr == "T" + @pytest.mark.filterwarnings( + "ignore:Period with BDay freq is deprecated:FutureWarning" + ) def test_period_deprecated_freq(self): cases = { "M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"], @@ -853,7 +869,8 @@ def test_start_time(self): for f in freq_lst: p = Period("2012", freq=f) assert p.start_time == xp - assert Period("2012", freq="B").start_time == datetime(2012, 1, 2) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert Period("2012", freq="B").start_time == datetime(2012, 1, 2) assert Period("2012", freq="W").start_time == datetime(2011, 12, 26) def test_end_time(self): @@ -881,9 +898,10 @@ def _ex(*args): xp = _ex(2012, 1, 1, 1) assert xp == p.end_time - p = Period("2012", freq="B") - xp = _ex(2012, 1, 3) - assert xp == p.end_time + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + p = Period("2012", freq="B") + xp = _ex(2012, 1, 3) + assert xp == p.end_time p = Period("2012", freq="W") xp = _ex(2012, 1, 2) @@ -904,8 +922,9 @@ def _ex(*args): def test_end_time_business_friday(self): # GH#34449 - per = Period("1990-01-05", "B") - result = per.end_time + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + per = Period("1990-01-05", "B") + result = per.end_time expected = Timestamp("1990-01-06") - Timedelta(nanoseconds=1) assert result == expected @@ -981,7 +1000,8 @@ def test_properties_weekly_legacy(self): def test_properties_daily(self): # Test properties on Periods with daily frequency. - b_date = Period(freq="B", year=2007, month=1, day=1) + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + b_date = Period(freq="B", year=2007, month=1, day=1) # assert b_date.year == 2007 assert b_date.quarter == 1 @@ -990,7 +1010,8 @@ def test_properties_daily(self): assert b_date.weekday == 0 assert b_date.dayofyear == 1 assert b_date.days_in_month == 31 - assert Period(freq="B", year=2012, month=2, day=1).days_in_month == 29 + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + assert Period(freq="B", year=2012, month=2, day=1).days_in_month == 29 d_date = Period(freq="D", year=2007, month=1, day=1) @@ -1582,7 +1603,8 @@ def test_negone_ordinals(): repr(period) assert period.year == 1969 - period = Period(ordinal=-1, freq="B") + with tm.assert_produces_warning(FutureWarning, match=bday_msg): + period = Period(ordinal=-1, freq="B") repr(period) period = Period(ordinal=-1, freq="W") repr(period)