From bc53d16f08c02e733b9bbe37d052c42a13cd8466 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 14 Dec 2023 12:30:24 +0100 Subject: [PATCH 01/10] replace freq_to_period_freqstr() with PeriodDtype(freq)._freqstr in _shift_with_freq() --- pandas/core/generic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e46a0aa044b6d..3607ae387f2c2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -41,7 +41,6 @@ Timestamp, to_offset, ) -from pandas._libs.tslibs.dtypes import freq_to_period_freqstr from pandas._typing import ( AlignJoin, AnyArrayLike, @@ -135,6 +134,7 @@ from pandas.core.dtypes.dtypes import ( DatetimeTZDtype, ExtensionDtype, + PeriodDtype, ) from pandas.core.dtypes.generic import ( ABCDataFrame, @@ -11274,9 +11274,9 @@ def _shift_with_freq(self, periods: int, axis: int, freq) -> Self: if freq != orig_freq: assert orig_freq is not None # for mypy raise ValueError( - f"Given freq {freq_to_period_freqstr(freq.n, freq.name)} " + f"Given freq {PeriodDtype(freq)._freqstr} " f"does not match PeriodIndex freq " - f"{freq_to_period_freqstr(orig_freq.n, orig_freq.name)}" + f"{PeriodDtype(orig_freq)._freqstr}" ) new_ax = index.shift(periods) else: From afcffbbe9a4f852e0d24cbe44e9a44a3bc403421 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Tue, 19 Dec 2023 18:05:30 +0100 Subject: [PATCH 02/10] replace freq_to_period_freqstr with PeriodDtype()._freqstr except in frequencies.py, period.pyx --- pandas/core/arrays/datetimes.py | 10 ++++------ pandas/core/arrays/period.py | 16 ++++++++-------- pandas/core/indexes/datetimelike.py | 8 +++++--- pandas/core/resample.py | 8 +++++--- pandas/io/json/_table_schema.py | 4 +--- .../indexes/datetimes/methods/test_to_period.py | 17 ----------------- 6 files changed, 23 insertions(+), 40 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 0074645a482b2..64f9f80ef70f1 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -39,10 +39,8 @@ tz_convert_from_utc, tzconversion, ) -from pandas._libs.tslibs.dtypes import ( - abbrev_to_npy_unit, - freq_to_period_freqstr, -) +from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit + from pandas.errors import PerformanceWarning from pandas.util._exceptions import find_stack_level from pandas.util._validators import validate_inclusive @@ -1234,8 +1232,8 @@ def to_period(self, freq=None) -> PeriodArray: if freq is None: freq = self.freqstr or self.inferred_freq - if isinstance(self.freq, BaseOffset): - freq = freq_to_period_freqstr(self.freq.n, self.freq.name) + if isinstance(self.freq, BaseOffset) and hasattr(self.freq, "_period_dtype_code"): + freq = PeriodDtype(self.freq)._freqstr if freq is None: raise ValueError( diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 0e2d4409b9f39..ec333f3792709 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -37,7 +37,6 @@ from pandas._libs.tslibs.dtypes import ( FreqGroup, PeriodDtypeBase, - freq_to_period_freqstr, ) from pandas._libs.tslibs.fields import isleapyear_arr from pandas._libs.tslibs.offsets import ( @@ -75,6 +74,7 @@ from pandas.core.arrays import datetimelike as dtl import pandas.core.common as com +from pandas.tseries.offsets import BusinessDay if TYPE_CHECKING: from collections.abc import Sequence @@ -324,7 +324,7 @@ def _from_datetime64(cls, data, freq, tz=None) -> Self: PeriodArray[freq] """ if isinstance(freq, BaseOffset): - freq = freq_to_period_freqstr(freq.n, freq.name) + freq = PeriodDtype(freq)._freqstr data, freq = dt64arr_to_periodarr(data, freq, tz) dtype = PeriodDtype(freq) return cls(data, dtype=dtype) @@ -398,7 +398,7 @@ def freq(self) -> BaseOffset: @property def freqstr(self) -> str: - return freq_to_period_freqstr(self.freq.n, self.freq.name) + return PeriodDtype(self.freq)._freqstr def __array__(self, dtype: NpDtype | None = None) -> np.ndarray: if dtype == "i8": @@ -734,7 +734,7 @@ def asfreq(self, freq=None, how: str = "E") -> Self: """ how = libperiod.validate_end_alias(how) if isinstance(freq, BaseOffset): - freq = freq_to_period_freqstr(freq.n, freq.name) + freq = PeriodDtype(freq)._freqstr freq = Period._maybe_convert_freq(freq) base1 = self._dtype._dtype_code @@ -979,14 +979,14 @@ def raise_on_incompatible(left, right) -> IncompatibleFrequency: # GH#24283 error message format depends on whether right is scalar if isinstance(right, (np.ndarray, ABCTimedeltaArray)) or right is None: other_freq = None - elif isinstance(right, BaseOffset): - other_freq = freq_to_period_freqstr(right.n, right.name) - elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period)): + elif isinstance(right, BaseOffset) and not isinstance(right, BusinessDay): + other_freq = PeriodDtype(right)._freqstr + elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period, BusinessDay)): other_freq = right.freqstr else: other_freq = delta_to_tick(Timedelta(right)).freqstr - own_freq = freq_to_period_freqstr(left.freq.n, left.freq.name) + own_freq = PeriodDtype(left.freq)._freqstr msg = DIFFERENT_FREQ.format( cls=type(left).__name__, own_freq=own_freq, other_freq=other_freq ) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 2b03a64236128..5f3f57f14bb3d 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -32,7 +32,6 @@ parsing, to_offset, ) -from pandas._libs.tslibs.dtypes import freq_to_period_freqstr from pandas.compat.numpy import function as nv from pandas.errors import ( InvalidIndexError, @@ -50,7 +49,10 @@ is_list_like, ) from pandas.core.dtypes.concat import concat_compat -from pandas.core.dtypes.dtypes import CategoricalDtype +from pandas.core.dtypes.dtypes import ( + CategoricalDtype, + PeriodDtype, +) from pandas.core.arrays import ( DatetimeArray, @@ -117,7 +119,7 @@ def freqstr(self) -> str: if self._data.freqstr is not None and isinstance( self._data, (PeriodArray, PeriodIndex) ): - freq = freq_to_period_freqstr(self._data.freq.n, self._data.freq.name) + freq = PeriodDtype(self._data.freq)._freqstr return freq else: return self._data.freqstr # type: ignore[return-value] diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 39b2f4336730d..3ecd40e03ecbc 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -24,7 +24,6 @@ Timestamp, to_offset, ) -from pandas._libs.tslibs.dtypes import freq_to_period_freqstr from pandas._typing import NDFrameT from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError @@ -38,7 +37,10 @@ rewrite_warning, ) -from pandas.core.dtypes.dtypes import ArrowDtype +from pandas.core.dtypes.dtypes import ( + ArrowDtype, + PeriodDtype, +) from pandas.core.dtypes.generic import ( ABCDataFrame, ABCSeries, @@ -2785,7 +2787,7 @@ def asfreq( if isinstance(freq, BaseOffset): if hasattr(freq, "_period_dtype_code"): - freq = freq_to_period_freqstr(freq.n, freq.name) + freq = PeriodDtype(freq)._freqstr else: raise ValueError( f"Invalid offset: '{freq.base}' for converting time series " diff --git a/pandas/io/json/_table_schema.py b/pandas/io/json/_table_schema.py index 4d9fba72cf173..ca7273bc777f2 100644 --- a/pandas/io/json/_table_schema.py +++ b/pandas/io/json/_table_schema.py @@ -15,7 +15,6 @@ from pandas._libs import lib from pandas._libs.json import ujson_loads from pandas._libs.tslibs import timezones -from pandas._libs.tslibs.dtypes import freq_to_period_freqstr from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.base import _registry as registry @@ -212,8 +211,7 @@ def convert_json_field_to_pandas_type(field) -> str | CategoricalDtype: elif field.get("freq"): # GH#9586 rename frequency M to ME for offsets offset = to_offset(field["freq"]) - freq_n, freq_name = offset.n, offset.name - freq = freq_to_period_freqstr(freq_n, freq_name) + freq = PeriodDtype(offset)._freqstr # GH#47747 using datetime over period to minimize the change surface return f"period[{freq}]" else: diff --git a/pandas/tests/indexes/datetimes/methods/test_to_period.py b/pandas/tests/indexes/datetimes/methods/test_to_period.py index 42a3f3b0f7b42..00c0216a9b3b5 100644 --- a/pandas/tests/indexes/datetimes/methods/test_to_period.py +++ b/pandas/tests/indexes/datetimes/methods/test_to_period.py @@ -111,23 +111,6 @@ def test_to_period_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr): with tm.assert_produces_warning(FutureWarning, match=msg): assert prng.freq == freq_depr - @pytest.mark.parametrize( - "freq, freq_depr", - [ - ("2BQE-SEP", "2BQ-SEP"), - ("2BYE-MAR", "2BY-MAR"), - ], - ) - def test_to_period_frequency_BQ_BY_deprecated(self, freq, freq_depr): - # GH#9586 - msg = f"'{freq_depr[1:]}' is deprecated and will be removed " - f"in a future version, please use '{freq[1:]}' instead." - - rng = date_range("01-Jan-2012", periods=8, freq=freq) - prng = rng.to_period() - with tm.assert_produces_warning(FutureWarning, match=msg): - prng.freq == freq_depr - def test_to_period_infer(self): # https://github.com/pandas-dev/pandas/issues/33358 rng = date_range( From 6f9d9749a1dc19cc5879cfcb8b1697b101cc7e54 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Tue, 19 Dec 2023 21:30:51 +0100 Subject: [PATCH 03/10] fix pre-commit errors --- pandas/core/arrays/datetimes.py | 5 +++-- pandas/core/arrays/period.py | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 64f9f80ef70f1..55840d49e7e5d 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -40,7 +40,6 @@ tzconversion, ) from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit - from pandas.errors import PerformanceWarning from pandas.util._exceptions import find_stack_level from pandas.util._validators import validate_inclusive @@ -1232,7 +1231,9 @@ def to_period(self, freq=None) -> PeriodArray: if freq is None: freq = self.freqstr or self.inferred_freq - if isinstance(self.freq, BaseOffset) and hasattr(self.freq, "_period_dtype_code"): + if isinstance(self.freq, BaseOffset) and hasattr( + self.freq, "_period_dtype_code" + ): freq = PeriodDtype(self.freq)._freqstr if freq is None: diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index ec333f3792709..466a6a51db7a9 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -74,6 +74,7 @@ from pandas.core.arrays import datetimelike as dtl import pandas.core.common as com + from pandas.tseries.offsets import BusinessDay if TYPE_CHECKING: From b529965c6bd6dfbe543eedef5c25edc59606a47d Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sat, 10 Feb 2024 01:10:24 +0100 Subject: [PATCH 04/10] remove freq_to_period_freqstr from _maybe_coerce_freq and from tests --- pandas/_libs/tslibs/offsets.pyx | 2 +- pandas/tests/arrays/test_datetimelike.py | 8 +++++--- pandas/tests/plotting/test_datetimelike.py | 9 ++++++--- pandas/tseries/frequencies.py | 7 ++----- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index e96a905367f69..46821f2f02393 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -4709,7 +4709,7 @@ _lite_rule_alias = { "ns": "ns", } -_dont_uppercase = _dont_uppercase = {"h", "bh", "cbh", "MS", "ms", "s"} +_dont_uppercase = {"h", "bh", "cbh", "MS", "ms", "s"} INVALID_FREQ_ERR_MSG = "Invalid frequency: {0}" diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 82524ea115019..ed915a8878c9a 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -11,7 +11,9 @@ OutOfBoundsDatetime, Timestamp, ) -from pandas._libs.tslibs.dtypes import freq_to_period_freqstr +from pandas._libs.tslibs import to_offset + +from pandas.core.dtypes.dtypes import PeriodDtype import pandas as pd from pandas import ( @@ -51,7 +53,7 @@ def period_index(freqstr): warnings.filterwarnings( "ignore", message="Period with BDay freq", category=FutureWarning ) - freqstr = freq_to_period_freqstr(1, freqstr) + freqstr = PeriodDtype(to_offset(freqstr))._freqstr pi = pd.period_range(start=Timestamp("2000-01-01"), periods=100, freq=freqstr) return pi @@ -761,7 +763,7 @@ def test_to_period(self, datetime_index, freqstr): dti = datetime_index arr = dti._data - freqstr = freq_to_period_freqstr(1, freqstr) + freqstr = PeriodDtype(to_offset(freqstr))._freqstr expected = dti.to_period(freq=freqstr) result = arr.to_period(freq=freqstr) assert isinstance(result, PeriodArray) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 112172656b6ec..5a0d5fea40b08 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -6,6 +6,7 @@ timedelta, ) import pickle +import re import numpy as np import pytest @@ -14,7 +15,8 @@ BaseOffset, to_offset, ) -from pandas._libs.tslibs.dtypes import freq_to_period_freqstr + +from pandas.core.dtypes.dtypes import PeriodDtype from pandas import ( DataFrame, @@ -238,7 +240,8 @@ def test_line_plot_period_mlt_frame(self, frqncy): index=idx, columns=["A", "B", "C"], ) - freq = freq_to_period_freqstr(1, df.index.freq.rule_code) + freq = re.split(r"\d", PeriodDtype(df.index.freq)._freqstr)[-1] + freq = df.index.asfreq(freq).freq _check_plot_works(df.plot, freq) @@ -253,7 +256,7 @@ def test_line_plot_datetime_frame(self, freq): index=idx, columns=["A", "B", "C"], ) - freq = freq_to_period_freqstr(1, df.index.freq.rule_code) + freq = re.split(r"\d", PeriodDtype(df.index.freq)._freqstr)[-1] freq = df.index.to_period(freq).freq _check_plot_works(df.plot, freq) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 4a1a668426b36..5029d82899ede 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -19,10 +19,7 @@ MONTHS, int_to_weekday, ) -from pandas._libs.tslibs.dtypes import ( - OFFSET_TO_PERIOD_FREQSTR, - freq_to_period_freqstr, -) +from pandas._libs.tslibs.dtypes import OFFSET_TO_PERIOD_FREQSTR from pandas._libs.tslibs.fields import ( build_field_sarray, month_position_check, @@ -559,7 +556,7 @@ def _maybe_coerce_freq(code) -> str: """ assert code is not None if isinstance(code, DateOffset): - code = freq_to_period_freqstr(1, code.name) + code = PeriodDtype(to_offset(code.name))._freqstr if code in {"h", "min", "s", "ms", "us", "ns"}: return code else: From daf707b0badc029cac32101f45db1b29bdbeb191 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Tue, 13 Feb 2024 22:06:44 +0100 Subject: [PATCH 05/10] remove freq_to_period_freqstr from period.pyx --- pandas/_libs/tslibs/dtypes.pxd | 1 - pandas/_libs/tslibs/dtypes.pyi | 1 - pandas/_libs/tslibs/dtypes.pyx | 9 --------- pandas/_libs/tslibs/period.pyx | 23 ++++++++++++----------- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/pandas/_libs/tslibs/dtypes.pxd b/pandas/_libs/tslibs/dtypes.pxd index 88cfa6ca60d93..33f6789f3b402 100644 --- a/pandas/_libs/tslibs/dtypes.pxd +++ b/pandas/_libs/tslibs/dtypes.pxd @@ -11,7 +11,6 @@ cpdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except? -1 cdef NPY_DATETIMEUNIT get_supported_reso(NPY_DATETIMEUNIT reso) cdef bint is_supported_unit(NPY_DATETIMEUNIT reso) -cpdef freq_to_period_freqstr(freq_n, freq_name) cdef dict c_OFFSET_TO_PERIOD_FREQSTR cdef dict c_OFFSET_DEPR_FREQSTR cdef dict c_REVERSE_OFFSET_DEPR_FREQSTR diff --git a/pandas/_libs/tslibs/dtypes.pyi b/pandas/_libs/tslibs/dtypes.pyi index dc2855cbecd69..b435b9e2d8b31 100644 --- a/pandas/_libs/tslibs/dtypes.pyi +++ b/pandas/_libs/tslibs/dtypes.pyi @@ -7,7 +7,6 @@ OFFSET_TO_PERIOD_FREQSTR: dict[str, str] def periods_per_day(reso: int = ...) -> int: ... def periods_per_second(reso: int) -> int: ... def abbrev_to_npy_unit(abbrev: str | None) -> int: ... -def freq_to_period_freqstr(freq_n: int, freq_name: str) -> str: ... class PeriodDtypeBase: _dtype_code: int # PeriodDtypeCode diff --git a/pandas/_libs/tslibs/dtypes.pyx b/pandas/_libs/tslibs/dtypes.pyx index 52e1133e596c5..c0d1b2e79f587 100644 --- a/pandas/_libs/tslibs/dtypes.pyx +++ b/pandas/_libs/tslibs/dtypes.pyx @@ -334,15 +334,6 @@ cdef dict c_REVERSE_OFFSET_DEPR_FREQSTR = { v: k for k, v in c_OFFSET_DEPR_FREQSTR.items() } -cpdef freq_to_period_freqstr(freq_n, freq_name): - if freq_n == 1: - freqstr = f"""{c_OFFSET_TO_PERIOD_FREQSTR.get( - freq_name, freq_name)}""" - else: - freqstr = f"""{freq_n}{c_OFFSET_TO_PERIOD_FREQSTR.get( - freq_name, freq_name)}""" - return freqstr - # Map deprecated resolution abbreviations to correct resolution abbreviations cdef dict c_DEPR_ABBREVS = { "A": "Y", diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 74804336f09a3..3da0fa182faf3 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -38,10 +38,7 @@ from libc.time cimport ( tm, ) -from pandas._libs.tslibs.dtypes cimport ( - c_OFFSET_TO_PERIOD_FREQSTR, - freq_to_period_freqstr, -) +from pandas._libs.tslibs.dtypes cimport c_OFFSET_TO_PERIOD_FREQSTR from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime @@ -97,9 +94,6 @@ from pandas._libs.tslibs.dtypes cimport ( attrname_to_abbrevs, freq_group_code_to_npy_unit, ) - -from pandas._libs.tslibs.dtypes import freq_to_period_freqstr - from pandas._libs.tslibs.parsing cimport quarter_to_myear from pandas._libs.tslibs.parsing import parse_datetime_string_with_reso @@ -1554,7 +1548,7 @@ def extract_ordinals(ndarray values, freq) -> np.ndarray: # if we don't raise here, we'll segfault later! raise TypeError("extract_ordinals values must be object-dtype") - freqstr = freq_to_period_freqstr(freq.n, freq.name) + freqstr = PeriodDtypeBase(freq._period_dtype_code, freq.n)._freqstr for i in range(n): # Analogous to: p = values[i] @@ -1722,8 +1716,15 @@ cdef class PeriodMixin: condition = self.freq != other if condition: - freqstr = freq_to_period_freqstr(self.freq.n, self.freq.name) - other_freqstr = freq_to_period_freqstr(other.n, other.name) + freqstr = PeriodDtypeBase( + self.freq._period_dtype_code, self.freq.n + )._freqstr + if hasattr(other, "_period_dtype_code"): + other_freqstr = PeriodDtypeBase( + other._period_dtype_code, other.n + )._freqstr + else: + other_freqstr = other.freqstr msg = DIFFERENT_FREQ.format( cls=type(self).__name__, own_freq=freqstr, @@ -2479,7 +2480,7 @@ cdef class _Period(PeriodMixin): >>> pd.Period('2020-01', 'D').freqstr 'D' """ - freqstr = freq_to_period_freqstr(self.freq.n, self.freq.name) + freqstr = PeriodDtypeBase(self.freq._period_dtype_code, self.freq.n)._freqstr return freqstr def __repr__(self) -> str: From b74807d7793131638f2d6b8c6f8cec4333607219 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Tue, 13 Feb 2024 23:02:12 +0100 Subject: [PATCH 06/10] correct raise_on_incompatible --- pandas/core/arrays/period.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index d217783fb0442..37875b4a0b672 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -75,8 +75,6 @@ from pandas.core.arrays import datetimelike as dtl import pandas.core.common as com -from pandas.tseries.offsets import BusinessDay - if TYPE_CHECKING: from collections.abc import Sequence @@ -986,9 +984,9 @@ def raise_on_incompatible(left, right) -> IncompatibleFrequency: # GH#24283 error message format depends on whether right is scalar if isinstance(right, (np.ndarray, ABCTimedeltaArray)) or right is None: other_freq = None - elif isinstance(right, BaseOffset) and not isinstance(right, BusinessDay): + elif isinstance(right, BaseOffset): other_freq = PeriodDtype(right)._freqstr - elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period, BusinessDay)): + elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period)): other_freq = right.freqstr else: other_freq = delta_to_tick(Timedelta(right)).freqstr From 751ab4964cb9ef69883bda0b510bcf76f35e784f Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 14 Feb 2024 00:16:01 +0100 Subject: [PATCH 07/10] correct raise_on_incompatible again --- pandas/core/arrays/period.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 37875b4a0b672..15efbb9e63fdf 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -985,7 +985,9 @@ def raise_on_incompatible(left, right) -> IncompatibleFrequency: if isinstance(right, (np.ndarray, ABCTimedeltaArray)) or right is None: other_freq = None elif isinstance(right, BaseOffset): - other_freq = PeriodDtype(right)._freqstr + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + other_freq = PeriodDtype(right)._freqstr elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period)): other_freq = right.freqstr else: From 48292cd1f9682f00fe50dedc4c45ba8399fe2573 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 15 Feb 2024 12:50:37 +0100 Subject: [PATCH 08/10] replace warnings.simplefilter with warnings.filterwarnings in raise_on_incompatible --- pandas/core/arrays/period.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 15efbb9e63fdf..775db4b56fa50 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -986,7 +986,9 @@ def raise_on_incompatible(left, right) -> IncompatibleFrequency: other_freq = None elif isinstance(right, BaseOffset): with warnings.catch_warnings(): - warnings.simplefilter("ignore") + warnings.filterwarnings( + "ignore", r"PeriodDtype\[B\] is deprecated", category=FutureWarning + ) other_freq = PeriodDtype(right)._freqstr elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period)): other_freq = right.freqstr From ec255602c333e4ba8aa521a37df654ac2699a756 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 21 Feb 2024 22:19:46 +0100 Subject: [PATCH 09/10] remove regex splitting from test_line_plot_datetime_frame --- pandas/tests/plotting/test_datetimelike.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 5a0d5fea40b08..470721e785a0e 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -241,7 +241,6 @@ def test_line_plot_period_mlt_frame(self, frqncy): columns=["A", "B", "C"], ) freq = re.split(r"\d", PeriodDtype(df.index.freq)._freqstr)[-1] - freq = df.index.asfreq(freq).freq _check_plot_works(df.plot, freq) @@ -256,7 +255,7 @@ def test_line_plot_datetime_frame(self, freq): index=idx, columns=["A", "B", "C"], ) - freq = re.split(r"\d", PeriodDtype(df.index.freq)._freqstr)[-1] + freq = PeriodDtype(df.index.freq)._freqstr freq = df.index.to_period(freq).freq _check_plot_works(df.plot, freq) From 2131579e72c8eecc17c8d8a5b0460860a3b345e5 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 22 Feb 2024 12:27:22 +0100 Subject: [PATCH 10/10] remove regex splitting from test_line_plot_period_mlt_frame --- pandas/tests/plotting/test_datetimelike.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 470721e785a0e..e80cee88867c4 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -6,7 +6,6 @@ timedelta, ) import pickle -import re import numpy as np import pytest @@ -240,8 +239,7 @@ def test_line_plot_period_mlt_frame(self, frqncy): index=idx, columns=["A", "B", "C"], ) - freq = re.split(r"\d", PeriodDtype(df.index.freq)._freqstr)[-1] - freq = df.index.asfreq(freq).freq + freq = df.index.freq.rule_code _check_plot_works(df.plot, freq) @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")