From 5b30943cbb02a01bae2901964ed330213dffd7b4 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 25 Sep 2023 11:11:24 +0100 Subject: [PATCH] dont map MS to period --- pandas/_libs/tslibs/dtypes.pyx | 11 ++--------- pandas/tests/resample/test_datetime_index.py | 13 +++++++++++++ pandas/tseries/frequencies.py | 16 +++++----------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pandas/_libs/tslibs/dtypes.pyx b/pandas/_libs/tslibs/dtypes.pyx index cca379c620aeb..48d70d8b2c0b0 100644 --- a/pandas/_libs/tslibs/dtypes.pyx +++ b/pandas/_libs/tslibs/dtypes.pyx @@ -189,13 +189,8 @@ OFFSET_TO_PERIOD_FREQSTR: dict = { "WEEKDAY": "D", "EOM": "M", "BM": "M", - "BQS": "Q", - "QS": "Q", "BQ": "Q", "BA": "A", - "AS": "A", - "BAS": "A", - "MS": "M", "D": "D", "B": "B", "min": "min", @@ -210,18 +205,16 @@ OFFSET_TO_PERIOD_FREQSTR: dict = { "ME": "M", "Y": "A", "BY": "A", - "YS": "A", - "BYS": "A", } cdef dict c_OFFSET_TO_PERIOD_FREQSTR = OFFSET_TO_PERIOD_FREQSTR 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)}""" + freq_name, None)}""" else: freqstr = f"""{freq_n}{c_OFFSET_TO_PERIOD_FREQSTR.get( - freq_name, freq_name)}""" + freq_name, None)}""" return freqstr # Map deprecated resolution abbreviations to correct resolution abbreviations diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index d929dfa6e1e59..9be27edf50cf3 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -2051,3 +2051,16 @@ def test_resample_M_deprecated(): with tm.assert_produces_warning(UserWarning, match=depr_msg): result = s.resample("2M").mean() tm.assert_series_equal(result, expected) + + +def test_resample_ms_closed_right(): + # https://github.com/pandas-dev/pandas/issues/55271 + dti = date_range(start="2020-01-31", freq="1min", periods=6000) + df = DataFrame({"ts": dti}, index=dti) + grouped = df.resample("MS", closed="right") + result = grouped.last() + expected = DataFrame( + {"ts": [datetime(2020, 2, 1), datetime(2020, 2, 4, 3, 59)]}, + index=DatetimeIndex([datetime(2020, 1, 1), datetime(2020, 2, 1)], freq="MS"), + ) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 7aa245341cbdd..471904cc5b60b 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -59,13 +59,6 @@ # --------------------------------------------------------------------- # Offset related functions -_need_suffix = ["QS", "BQ", "BQS", "YS", "AS", "BY", "BA", "BYS", "BAS"] - -for _prefix in _need_suffix: - for _m in MONTHS: - key = f"{_prefix}-{_m}" - OFFSET_TO_PERIOD_FREQSTR[key] = OFFSET_TO_PERIOD_FREQSTR[_prefix] - for _prefix in ["A", "Q"]: for _m in MONTHS: _alias = f"{_prefix}-{_m}" @@ -502,10 +495,10 @@ def is_superperiod(source, target) -> bool: ------- bool """ - if target is None or source is None: - return False source = _maybe_coerce_freq(source) target = _maybe_coerce_freq(target) + if target is None or source is None: + return False if _is_annual(source): if _is_annual(target): @@ -544,7 +537,7 @@ def is_superperiod(source, target) -> bool: return False -def _maybe_coerce_freq(code) -> str: +def _maybe_coerce_freq(code) -> str | None: """we might need to coerce a code to a rule_code and uppercase it @@ -557,9 +550,10 @@ def _maybe_coerce_freq(code) -> str: ------- str """ - assert code is not None if isinstance(code, DateOffset): code = freq_to_period_freqstr(1, code.name) + if code is None: + return None if code in {"min", "s", "ms", "us", "ns"}: return code else: