diff --git a/asv_bench/benchmarks/tslibs/offsets.py b/asv_bench/benchmarks/tslibs/offsets.py index d6379b922641c..fc1efe63307b2 100644 --- a/asv_bench/benchmarks/tslibs/offsets.py +++ b/asv_bench/benchmarks/tslibs/offsets.py @@ -59,7 +59,7 @@ def setup(self, offset): def time_on_offset(self, offset): for date in self.dates: - offset.onOffset(date) + offset.is_on_offset(date) class OffestDatetimeArithmetic: diff --git a/doc/source/reference/offset_frequency.rst b/doc/source/reference/offset_frequency.rst index 4a58055f1c955..fc1c6d6bd6d47 100644 --- a/doc/source/reference/offset_frequency.rst +++ b/doc/source/reference/offset_frequency.rst @@ -35,6 +35,8 @@ Methods DateOffset.copy DateOffset.isAnchored DateOffset.onOffset + DateOffset.is_anchored + DateOffset.is_on_offset BusinessDay ----------- @@ -65,6 +67,8 @@ Methods BusinessDay.copy BusinessDay.isAnchored BusinessDay.onOffset + BusinessDay.is_anchored + BusinessDay.is_on_offset BusinessHour ------------ @@ -94,6 +98,8 @@ Methods BusinessHour.copy BusinessHour.isAnchored BusinessHour.onOffset + BusinessHour.is_anchored + BusinessHour.is_on_offset CustomBusinessDay ----------------- @@ -123,6 +129,8 @@ Methods CustomBusinessDay.copy CustomBusinessDay.isAnchored CustomBusinessDay.onOffset + CustomBusinessDay.is_anchored + CustomBusinessDay.is_on_offset CustomBusinessHour ------------------ @@ -152,6 +160,8 @@ Methods CustomBusinessHour.copy CustomBusinessHour.isAnchored CustomBusinessHour.onOffset + CustomBusinessHour.is_anchored + CustomBusinessHour.is_on_offset MonthOffset ----------- @@ -182,6 +192,8 @@ Methods MonthOffset.copy MonthOffset.isAnchored MonthOffset.onOffset + MonthOffset.is_anchored + MonthOffset.is_on_offset MonthEnd -------- @@ -212,6 +224,8 @@ Methods MonthEnd.copy MonthEnd.isAnchored MonthEnd.onOffset + MonthEnd.is_anchored + MonthEnd.is_on_offset MonthBegin ---------- @@ -242,6 +256,8 @@ Methods MonthBegin.copy MonthBegin.isAnchored MonthBegin.onOffset + MonthBegin.is_anchored + MonthBegin.is_on_offset BusinessMonthEnd ---------------- @@ -272,6 +288,8 @@ Methods BusinessMonthEnd.copy BusinessMonthEnd.isAnchored BusinessMonthEnd.onOffset + BusinessMonthEnd.is_anchored + BusinessMonthEnd.is_on_offset BusinessMonthBegin ------------------ @@ -302,6 +320,8 @@ Methods BusinessMonthBegin.copy BusinessMonthBegin.isAnchored BusinessMonthBegin.onOffset + BusinessMonthBegin.is_anchored + BusinessMonthBegin.is_on_offset CustomBusinessMonthEnd ---------------------- @@ -332,6 +352,8 @@ Methods CustomBusinessMonthEnd.copy CustomBusinessMonthEnd.isAnchored CustomBusinessMonthEnd.onOffset + CustomBusinessMonthEnd.is_anchored + CustomBusinessMonthEnd.is_on_offset CustomBusinessMonthBegin ------------------------ @@ -362,6 +384,8 @@ Methods CustomBusinessMonthBegin.copy CustomBusinessMonthBegin.isAnchored CustomBusinessMonthBegin.onOffset + CustomBusinessMonthBegin.is_anchored + CustomBusinessMonthBegin.is_on_offset SemiMonthOffset --------------- @@ -392,6 +416,8 @@ Methods SemiMonthOffset.copy SemiMonthOffset.isAnchored SemiMonthOffset.onOffset + SemiMonthOffset.is_anchored + SemiMonthOffset.is_on_offset SemiMonthEnd ------------ @@ -422,6 +448,8 @@ Methods SemiMonthEnd.copy SemiMonthEnd.isAnchored SemiMonthEnd.onOffset + SemiMonthEnd.is_anchored + SemiMonthEnd.is_on_offset SemiMonthBegin -------------- @@ -452,6 +480,8 @@ Methods SemiMonthBegin.copy SemiMonthBegin.isAnchored SemiMonthBegin.onOffset + SemiMonthBegin.is_anchored + SemiMonthBegin.is_on_offset Week ---- @@ -482,6 +512,8 @@ Methods Week.copy Week.isAnchored Week.onOffset + Week.is_anchored + Week.is_on_offset WeekOfMonth ----------- @@ -511,6 +543,8 @@ Methods WeekOfMonth.copy WeekOfMonth.isAnchored WeekOfMonth.onOffset + WeekOfMonth.is_anchored + WeekOfMonth.is_on_offset LastWeekOfMonth --------------- @@ -540,6 +574,8 @@ Methods LastWeekOfMonth.copy LastWeekOfMonth.isAnchored LastWeekOfMonth.onOffset + LastWeekOfMonth.is_anchored + LastWeekOfMonth.is_on_offset QuarterOffset ------------- @@ -570,6 +606,8 @@ Methods QuarterOffset.copy QuarterOffset.isAnchored QuarterOffset.onOffset + QuarterOffset.is_anchored + QuarterOffset.is_on_offset BQuarterEnd ----------- @@ -600,6 +638,8 @@ Methods BQuarterEnd.copy BQuarterEnd.isAnchored BQuarterEnd.onOffset + BQuarterEnd.is_anchored + BQuarterEnd.is_on_offset BQuarterBegin ------------- @@ -630,6 +670,8 @@ Methods BQuarterBegin.copy BQuarterBegin.isAnchored BQuarterBegin.onOffset + BQuarterBegin.is_anchored + BQuarterBegin.is_on_offset QuarterEnd ---------- @@ -660,6 +702,8 @@ Methods QuarterEnd.copy QuarterEnd.isAnchored QuarterEnd.onOffset + QuarterEnd.is_anchored + QuarterEnd.is_on_offset QuarterBegin ------------ @@ -690,6 +734,8 @@ Methods QuarterBegin.copy QuarterBegin.isAnchored QuarterBegin.onOffset + QuarterBegin.is_anchored + QuarterBegin.is_on_offset YearOffset ---------- @@ -720,6 +766,8 @@ Methods YearOffset.copy YearOffset.isAnchored YearOffset.onOffset + YearOffset.is_anchored + YearOffset.is_on_offset BYearEnd -------- @@ -750,6 +798,8 @@ Methods BYearEnd.copy BYearEnd.isAnchored BYearEnd.onOffset + BYearEnd.is_anchored + BYearEnd.is_on_offset BYearBegin ---------- @@ -780,6 +830,8 @@ Methods BYearBegin.copy BYearBegin.isAnchored BYearBegin.onOffset + BYearBegin.is_anchored + BYearBegin.is_on_offset YearEnd ------- @@ -810,6 +862,8 @@ Methods YearEnd.copy YearEnd.isAnchored YearEnd.onOffset + YearEnd.is_anchored + YearEnd.is_on_offset YearBegin --------- @@ -840,6 +894,8 @@ Methods YearBegin.copy YearBegin.isAnchored YearBegin.onOffset + YearBegin.is_anchored + YearBegin.is_on_offset FY5253 ------ @@ -871,6 +927,8 @@ Methods FY5253.get_year_end FY5253.isAnchored FY5253.onOffset + FY5253.is_anchored + FY5253.is_on_offset FY5253Quarter ------------- @@ -901,6 +959,8 @@ Methods FY5253Quarter.get_weeks FY5253Quarter.isAnchored FY5253Quarter.onOffset + FY5253Quarter.is_anchored + FY5253Quarter.is_on_offset FY5253Quarter.year_has_extra_week Easter @@ -931,6 +991,8 @@ Methods Easter.copy Easter.isAnchored Easter.onOffset + Easter.is_anchored + Easter.is_on_offset Tick ---- @@ -960,6 +1022,8 @@ Methods Tick.copy Tick.isAnchored Tick.onOffset + Tick.is_anchored + Tick.is_on_offset Day --- @@ -989,6 +1053,8 @@ Methods Day.copy Day.isAnchored Day.onOffset + Day.is_anchored + Day.is_on_offset Hour ---- @@ -1018,6 +1084,8 @@ Methods Hour.copy Hour.isAnchored Hour.onOffset + Hour.is_anchored + Hour.is_on_offset Minute ------ @@ -1047,6 +1115,8 @@ Methods Minute.copy Minute.isAnchored Minute.onOffset + Minute.is_anchored + Minute.is_on_offset Second ------ @@ -1076,6 +1146,8 @@ Methods Second.copy Second.isAnchored Second.onOffset + Second.is_anchored + Second.is_on_offset Milli ----- @@ -1105,6 +1177,8 @@ Methods Milli.copy Milli.isAnchored Milli.onOffset + Milli.is_anchored + Milli.is_on_offset Micro ----- @@ -1134,6 +1208,8 @@ Methods Micro.copy Micro.isAnchored Micro.onOffset + Micro.is_anchored + Micro.is_on_offset Nano ---- @@ -1163,6 +1239,8 @@ Methods Nano.copy Nano.isAnchored Nano.onOffset + Nano.is_anchored + Nano.is_on_offset BDay ---- @@ -1195,6 +1273,8 @@ Methods BDay.copy BDay.isAnchored BDay.onOffset + BDay.is_anchored + BDay.is_on_offset BDay.rollback BDay.rollforward @@ -1228,6 +1308,8 @@ Methods BMonthEnd.copy BMonthEnd.isAnchored BMonthEnd.onOffset + BMonthEnd.is_anchored + BMonthEnd.is_on_offset BMonthEnd.rollback BMonthEnd.rollforward @@ -1261,6 +1343,8 @@ Methods BMonthBegin.copy BMonthBegin.isAnchored BMonthBegin.onOffset + BMonthBegin.is_anchored + BMonthBegin.is_on_offset BMonthBegin.rollback BMonthBegin.rollforward @@ -1298,6 +1382,8 @@ Methods CBMonthEnd.copy CBMonthEnd.isAnchored CBMonthEnd.onOffset + CBMonthEnd.is_anchored + CBMonthEnd.is_on_offset CBMonthEnd.rollback CBMonthEnd.rollforward @@ -1335,6 +1421,8 @@ Methods CBMonthBegin.copy CBMonthBegin.isAnchored CBMonthBegin.onOffset + CBMonthBegin.is_anchored + CBMonthBegin.is_on_offset CBMonthBegin.rollback CBMonthBegin.rollforward @@ -1369,6 +1457,8 @@ Methods CDay.copy CDay.isAnchored CDay.onOffset + CDay.is_anchored + CDay.is_on_offset CDay.rollback CDay.rollforward diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index a15d5b319fc82..48c80fb23753e 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -495,6 +495,8 @@ Deprecations is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`). - :func:`is_extension_type` is deprecated, :func:`is_extension_array_dtype` should be used instead (:issue:`29457`) - :func:`eval` keyword argument "truediv" is deprecated and will be removed in a future version (:issue:`29812`) +- :meth:`DateOffset.isAnchored` and :meth:`DatetOffset.onOffset` are deprecated and will be removed in a future version, use :meth:`DateOffset.is_anchored` and :meth:`DateOffset.is_on_offset` instead (:issue:`30340`) +- ``pandas.tseries.frequencies.get_offset`` is deprecated and will be removed in a future version, use ``pandas.tseries.frequencies.to_offset`` instead (:issue:`4205`) - :meth:`Categorical.take_nd` is deprecated, use :meth:`Categorical.take` instead (:issue:`27745`) - The parameter ``numeric_only`` of :meth:`Categorical.min` and :meth:`Categorical.max` is deprecated and replaced with ``skipna`` (:issue:`25303`) - The parameter ``label`` in :func:`lreshape` has been deprecated and will be removed in a future version (:issue:`29742`) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 41420dbceef9d..f24dce28cd5f7 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -933,7 +933,7 @@ def shift_month(stamp: datetime, months: int, cpdef int get_day_of_month(datetime other, day_opt) except? -1: """ - Find the day in `other`'s month that satisfies a DateOffset's onOffset + Find the day in `other`'s month that satisfies a DateOffset's is_on_offset policy, as described by the `day_opt` argument. Parameters diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 03bd1b331ec30..a3b206c468af6 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8013,7 +8013,7 @@ def first(self, offset): end_date = end = self.index[0] + offset # Tick-like, e.g. 3 weeks - if not offset.isAnchored() and hasattr(offset, "_inc"): + if not offset.is_anchored() and hasattr(offset, "_inc"): if end_date in self.index: end = self.index.searchsorted(end_date, side="left") return self.iloc[:end] diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 3bf6dce00a031..a2c8e7d4a0ed1 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -600,7 +600,7 @@ def intersection(self, other, sort=False): other.freq is None or self.freq is None or other.freq != self.freq - or not other.freq.isAnchored() + or not other.freq.is_anchored() or (not self.is_monotonic or not other.is_monotonic) ): result = Index.intersection(self, other, sort=sort) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 1fd962dd24656..4ca047c169ca7 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -698,7 +698,7 @@ def snap(self, freq="S"): for i, v in enumerate(self): s = v - if not freq.onOffset(s): + if not freq.is_on_offset(s): t0 = freq.rollback(s) t1 = freq.rollforward(s) if abs(s - t0) < abs(t1 - s): diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 2294c846e81c7..b5550c596ed5b 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1700,8 +1700,8 @@ def _get_period_range_edges(first, last, offset, closed="left", base=0): # GH 23882 first = first.to_timestamp() last = last.to_timestamp() - adjust_first = not offset.onOffset(first) - adjust_last = offset.onOffset(last) + adjust_first = not offset.is_on_offset(first) + adjust_last = offset.is_on_offset(last) first, last = _get_timestamp_range_edges( first, last, offset, closed=closed, base=base diff --git a/pandas/tests/indexes/interval/test_interval_range.py b/pandas/tests/indexes/interval/test_interval_range.py index b102444b4ec9c..7891666e6cdba 100644 --- a/pandas/tests/indexes/interval/test_interval_range.py +++ b/pandas/tests/indexes/interval/test_interval_range.py @@ -84,7 +84,7 @@ def test_constructor_timestamp(self, closed, name, freq, periods, tz): tm.assert_index_equal(result, expected) # GH 20976: linspace behavior defined from start/end/periods - if not breaks.freq.isAnchored() and tz is None: + if not breaks.freq.is_anchored() and tz is None: # matches expected only for non-anchored offsets and tz naive # (anchored/DST transitions cause unequal spacing in expected) result = interval_range( diff --git a/pandas/tests/tseries/frequencies/test_inference.py b/pandas/tests/tseries/frequencies/test_inference.py index 50844aabb2c88..250c37cdadbe4 100644 --- a/pandas/tests/tseries/frequencies/test_inference.py +++ b/pandas/tests/tseries/frequencies/test_inference.py @@ -468,7 +468,7 @@ def test_series_datetime_index(freq): @pytest.mark.parametrize( "offset_func", [ - frequencies.get_offset, + frequencies._get_offset, lambda freq: date_range("2011-01-01", periods=5, freq=freq), ], ) @@ -528,8 +528,8 @@ def test_legacy_offset_warnings(offset_func, freq): def test_ms_vs_capital_ms(): - left = frequencies.get_offset("ms") - right = frequencies.get_offset("MS") + left = frequencies._get_offset("ms") + right = frequencies._get_offset("MS") assert left == offsets.Milli() assert right == offsets.MonthBegin() diff --git a/pandas/tests/tseries/offsets/common.py b/pandas/tests/tseries/offsets/common.py index a097636bbf0b4..71953fd095882 100644 --- a/pandas/tests/tseries/offsets/common.py +++ b/pandas/tests/tseries/offsets/common.py @@ -18,8 +18,8 @@ def assert_offset_equal(offset, base, expected): ) -def assert_onOffset(offset, date, expected): - actual = offset.onOffset(date) +def assert_is_on_offset(offset, date, expected): + actual = offset.is_on_offset(date) assert actual == expected, ( f"\nExpected: {expected}\nActual: {actual}\nFor Offset: {offset})" f"\nAt Date: {date}" diff --git a/pandas/tests/tseries/offsets/test_fiscal.py b/pandas/tests/tseries/offsets/test_fiscal.py index c97e0b8493f9c..9ab722b866c76 100644 --- a/pandas/tests/tseries/offsets/test_fiscal.py +++ b/pandas/tests/tseries/offsets/test_fiscal.py @@ -9,11 +9,12 @@ from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG from pandas import Timestamp +import pandas.util.testing as tm from pandas.tseries.frequencies import get_offset from pandas.tseries.offsets import FY5253, FY5253Quarter -from .common import assert_offset_equal, assert_onOffset +from .common import assert_is_on_offset, assert_offset_equal from .test_offsets import Base, WeekDay @@ -50,9 +51,11 @@ def test_get_offset_name(): def test_get_offset(): with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): - get_offset("gibberish") + with tm.assert_produces_warning(FutureWarning): + get_offset("gibberish") with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): - get_offset("QS-JAN-B") + with tm.assert_produces_warning(FutureWarning): + get_offset("QS-JAN-B") pairs = [ ("RE-N-DEC-MON", makeFY5253NearestEndMonth(weekday=0, startingMonth=12)), @@ -78,7 +81,8 @@ def test_get_offset(): ] for name, expected in pairs: - offset = get_offset(name) + with tm.assert_produces_warning(FutureWarning): + offset = get_offset(name) assert offset == expected, ( f"Expected {repr(name)} to yield {repr(expected)} " f"(actual: {repr(offset)})" @@ -128,9 +132,9 @@ class TestFY5253LastOfMonth(Base): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) def test_apply(self): offset_lom_aug_sat = makeFY5253LastOfMonth(startingMonth=8, weekday=WeekDay.SAT) @@ -253,9 +257,9 @@ def test_get_year_end(self): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) def test_apply(self): date_seq_nem_8_sat = [ @@ -329,16 +333,16 @@ def test_apply(self): class TestFY5253LastOfMonthQuarter(Base): - def test_isAnchored(self): + def test_is_anchored(self): assert makeFY5253LastOfMonthQuarter( startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4 - ).isAnchored() + ).is_anchored() assert makeFY5253LastOfMonthQuarter( weekday=WeekDay.SAT, startingMonth=3, qtr_with_extra_week=4 - ).isAnchored() + ).is_anchored() assert not makeFY5253LastOfMonthQuarter( 2, startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4 - ).isAnchored() + ).is_anchored() def test_equality(self): assert makeFY5253LastOfMonthQuarter( @@ -491,9 +495,9 @@ def test_offset(self): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) def test_year_has_extra_week(self): # End of long Q1 @@ -596,9 +600,9 @@ class TestFY5253NearestEndMonthQuarter(Base): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) def test_offset(self): offset = makeFY5253NearestEndMonthQuarter( @@ -652,7 +656,7 @@ def test_fy5253_last_onoffset(): # GH#18877 dates on the year-end but not normalized to midnight offset = FY5253(n=-5, startingMonth=5, variation="last", weekday=0) ts = Timestamp("1984-05-28 06:29:43.955911354+0200", tz="Europe/San_Marino") - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) slow = (ts + offset) - offset == ts assert fast == slow @@ -661,7 +665,7 @@ def test_fy5253_nearest_onoffset(): # GH#18877 dates on the year-end but not normalized to midnight offset = FY5253(n=3, startingMonth=7, variation="nearest", weekday=2) ts = Timestamp("2032-07-28 00:12:59.035729419+0000", tz="Africa/Dakar") - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) slow = (ts + offset) - offset == ts assert fast == slow @@ -672,7 +676,7 @@ def test_fy5253qtr_onoffset_nearest(): offset = FY5253Quarter( n=3, qtr_with_extra_week=1, startingMonth=2, variation="nearest", weekday=0 ) - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) slow = (ts + offset) - offset == ts assert fast == slow @@ -684,5 +688,5 @@ def test_fy5253qtr_onoffset_last(): ) ts = Timestamp("2011-01-26 19:03:40.331096129+0200", tz="Africa/Windhoek") slow = (ts + offset) - offset == ts - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) assert fast == slow diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py index 6f628bf86829a..fcabc0bee85b6 100644 --- a/pandas/tests/tseries/offsets/test_offsets.py +++ b/pandas/tests/tseries/offsets/test_offsets.py @@ -27,7 +27,7 @@ import pandas.util.testing as tm from pandas.io.pickle import read_pickle -from pandas.tseries.frequencies import _offset_map, get_offset +from pandas.tseries.frequencies import _get_offset, _offset_map from pandas.tseries.holiday import USFederalHolidayCalendar import pandas.tseries.offsets as offsets from pandas.tseries.offsets import ( @@ -67,7 +67,7 @@ YearEnd, ) -from .common import assert_offset_equal, assert_onOffset +from .common import assert_is_on_offset, assert_offset_equal class WeekDay: @@ -337,7 +337,7 @@ def test_offset_freqstr(self, offset_types): freqstr = offset.freqstr if freqstr not in ("", "", "LWOM-SAT"): - code = get_offset(freqstr) + code = _get_offset(freqstr) assert offset.rule_code == code def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=False): @@ -557,24 +557,24 @@ def test_rollback(self, offset_types): offset_types, "rollback", dt, expected, normalize=True ) - def test_onOffset(self, offset_types): + def test_is_on_offset(self, offset_types): dt = self.expecteds[offset_types.__name__] offset_s = self._get_offset(offset_types) - assert offset_s.onOffset(dt) + assert offset_s.is_on_offset(dt) - # when normalize=True, onOffset checks time is 00:00:00 + # when normalize=True, is_on_offset checks time is 00:00:00 if issubclass(offset_types, Tick): # normalize=True disallowed for Tick subclasses GH#21427 return offset_n = self._get_offset(offset_types, normalize=True) - assert not offset_n.onOffset(dt) + assert not offset_n.is_on_offset(dt) if offset_types in (BusinessHour, CustomBusinessHour): # In default BusinessHour (9:00-17:00), normalized time # cannot be in business hour range return date = datetime(dt.year, dt.month, dt.day) - assert offset_n.onOffset(date) + assert offset_n.is_on_offset(date) def test_add(self, offset_types, tz_naive_fixture): tz = tz_naive_fixture @@ -666,6 +666,27 @@ def test_pickle_v0_15_2(self, datapath): # tm.assert_dict_equal(offsets, read_pickle(pickle_path)) + def test_onOffset_deprecated(self, offset_types): + # GH#30340 use idiomatic naming + off = self._get_offset(offset_types) + + ts = Timestamp.now() + with tm.assert_produces_warning(FutureWarning): + result = off.onOffset(ts) + + expected = off.is_on_offset(ts) + assert result == expected + + def test_isAnchored_deprecated(self, offset_types): + # GH#30340 use idiomatic naming + off = self._get_offset(offset_types) + + with tm.assert_produces_warning(FutureWarning): + result = off.isAnchored() + + expected = off.is_anchored() + assert result == expected + class TestDateOffset(Base): def setup_method(self, method): @@ -689,8 +710,8 @@ def test_constructor(self): assert (self.d + DateOffset(2)) == datetime(2008, 1, 4) - assert not DateOffset(2).isAnchored() - assert DateOffset(1).isAnchored() + assert not DateOffset(2).is_anchored() + assert DateOffset(1).is_anchored() d = datetime(2008, 1, 31) assert (d + DateOffset(months=1)) == datetime(2008, 2, 29) @@ -778,14 +799,14 @@ def test_roll_date_object(self): result = offset.rollforward(dt) assert result == datetime(2012, 9, 15) - def test_onOffset(self): + def test_is_on_offset(self): tests = [ (BDay(), datetime(2008, 1, 1), True), (BDay(), datetime(2008, 1, 5), False), ] for offset, d, expected in tests: - assert_onOffset(offset, d, expected) + assert_is_on_offset(offset, d, expected) apply_cases: _ApplyCases = [] apply_cases.append( @@ -1273,10 +1294,10 @@ def test_normalize(self, case): ) @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, cases = case for dt, expected in cases.items(): - assert offset.onOffset(dt) == expected + assert offset.is_on_offset(dt) == expected opening_time_cases = [] # opening time should be affected by sign of n, not by n's value and @@ -2472,7 +2493,7 @@ def test_normalize(self, norm_cases): for dt, expected in cases.items(): assert offset.apply(dt) == expected - def test_onOffset(self): + def test_is_on_offset(self): tests = [] tests.append( @@ -2491,7 +2512,7 @@ def test_onOffset(self): for offset, cases in tests: for dt, expected in cases.items(): - assert offset.onOffset(dt) == expected + assert offset.is_on_offset(dt) == expected apply_cases = [] apply_cases.append( @@ -2671,9 +2692,9 @@ def test_roll_date_object(self): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, d, expected = case - assert_onOffset(offset, d, expected) + assert_is_on_offset(offset, d, expected) apply_cases: _ApplyCases = [] apply_cases.append( @@ -2918,9 +2939,9 @@ def test_roll_date_object(self): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, d, expected = case - assert_onOffset(offset, d, expected) + assert_is_on_offset(offset, d, expected) apply_cases: _ApplyCases = [] apply_cases.append( @@ -3067,9 +3088,9 @@ def test_roll_date_object(self): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) apply_cases: _ApplyCases = [] apply_cases.append( @@ -3184,11 +3205,11 @@ def test_corner(self): with pytest.raises(ValueError, match="Day must be"): Week(weekday=-1) - def test_isAnchored(self): - assert Week(weekday=0).isAnchored() - assert not Week().isAnchored() - assert not Week(2, weekday=2).isAnchored() - assert not Week(2).isAnchored() + def test_is_anchored(self): + assert Week(weekday=0).is_anchored() + assert not Week().is_anchored() + assert not Week(2, weekday=2).is_anchored() + assert not Week(2).is_anchored() offset_cases = [] # not business week @@ -3252,7 +3273,7 @@ def test_offset(self, case): assert_offset_equal(offset, base, expected) @pytest.mark.parametrize("weekday", range(7)) - def test_onOffset(self, weekday): + def test_is_on_offset(self, weekday): offset = Week(weekday=weekday) for day in range(1, 8): @@ -3262,7 +3283,7 @@ def test_onOffset(self, weekday): expected = True else: expected = False - assert_onOffset(offset, date, expected) + assert_is_on_offset(offset, date, expected) class TestWeekOfMonth(Base): @@ -3359,10 +3380,10 @@ def test_offset(self): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): week, weekday, dt, expected = case offset = WeekOfMonth(week=week, weekday=weekday) - assert offset.onOffset(dt) == expected + assert offset.is_on_offset(dt) == expected class TestLastWeekOfMonth(Base): @@ -3436,10 +3457,10 @@ def test_offset(self): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): weekday, dt, expected = case offset = LastWeekOfMonth(weekday=weekday) - assert offset.onOffset(dt) == expected + assert offset.is_on_offset(dt) == expected class TestSemiMonthEnd(Base): @@ -3646,9 +3667,9 @@ def test_apply_index(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): dt, expected = case - assert_onOffset(SemiMonthEnd(), dt, expected) + assert_is_on_offset(SemiMonthEnd(), dt, expected) @pytest.mark.parametrize("klass", [Series, DatetimeIndex]) def test_vectorized_offset_addition(self, klass): @@ -3910,9 +3931,9 @@ def test_apply_index(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): dt, expected = case - assert_onOffset(SemiMonthBegin(), dt, expected) + assert_is_on_offset(SemiMonthBegin(), dt, expected) @pytest.mark.parametrize("klass", [Series, DatetimeIndex]) def test_vectorized_offset_addition(self, klass): @@ -3995,9 +4016,9 @@ def test_get_offset_name(self): def test_get_offset(): with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): - get_offset("gibberish") + _get_offset("gibberish") with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): - get_offset("QS-JAN-B") + _get_offset("QS-JAN-B") pairs = [ ("B", BDay()), @@ -4012,7 +4033,7 @@ def test_get_offset(): ] for name, expected in pairs: - offset = get_offset(name) + offset = _get_offset(name) assert offset == expected, ( f"Expected {repr(name)} to yield {repr(expected)} " f"(actual: {repr(offset)})" @@ -4023,7 +4044,7 @@ def test_get_offset_legacy(): pairs = [("w@Sat", Week(weekday=5))] for name, expected in pairs: with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): - get_offset(name) + _get_offset(name) class TestOffsetAliases: @@ -4039,17 +4060,17 @@ def test_alias_equality(self): def test_rule_code(self): lst = ["M", "MS", "BM", "BMS", "D", "B", "H", "T", "S", "L", "U"] for k in lst: - assert k == get_offset(k).rule_code + assert k == _get_offset(k).rule_code # should be cached - this is kind of an internals test... assert k in _offset_map - assert k == (get_offset(k) * 3).rule_code + assert k == (_get_offset(k) * 3).rule_code suffix_lst = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"] base = "W" for v in suffix_lst: alias = "-".join([base, v]) - assert alias == get_offset(alias).rule_code - assert alias == (get_offset(alias) * 5).rule_code + assert alias == _get_offset(alias).rule_code + assert alias == (_get_offset(alias) * 5).rule_code suffix_lst = [ "JAN", @@ -4069,8 +4090,8 @@ def test_rule_code(self): for base in base_lst: for v in suffix_lst: alias = "-".join([base, v]) - assert alias == get_offset(alias).rule_code - assert alias == (get_offset(alias) * 5).rule_code + assert alias == _get_offset(alias).rule_code + assert alias == (_get_offset(alias) * 5).rule_code lst = ["M", "D", "B", "H", "T", "S", "L", "U"] for k in lst: @@ -4123,7 +4144,7 @@ def test_str_for_named_is_name(self): names += ["WOM-" + week + day for week in ("1", "2", "3", "4") for day in days] _offset_map.clear() for name in names: - offset = get_offset(name) + offset = _get_offset(name) assert offset.freqstr == name @@ -4348,34 +4369,34 @@ def test_tick_normalize_raises(tick_classes): def test_weeks_onoffset(): # GH#18510 Week with weekday = None, normalize = False should always - # be onOffset + # be is_on_offset offset = Week(n=2, weekday=None) ts = Timestamp("1862-01-13 09:03:34.873477378+0210", tz="Africa/Lusaka") - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) slow = (ts + offset) - offset == ts assert fast == slow # negative n offset = Week(n=2, weekday=None) ts = Timestamp("1856-10-24 16:18:36.556360110-0717", tz="Pacific/Easter") - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) slow = (ts + offset) - offset == ts assert fast == slow def test_weekofmonth_onoffset(): # GH#18864 - # Make sure that nanoseconds don't trip up onOffset (and with it apply) + # Make sure that nanoseconds don't trip up is_on_offset (and with it apply) offset = WeekOfMonth(n=2, week=2, weekday=0) ts = Timestamp("1916-05-15 01:14:49.583410462+0422", tz="Asia/Qyzylorda") - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) slow = (ts + offset) - offset == ts assert fast == slow # negative n offset = WeekOfMonth(n=-3, week=1, weekday=0) ts = Timestamp("1980-12-08 03:38:52.878321185+0500", tz="Asia/Oral") - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) slow = (ts + offset) - offset == ts assert fast == slow @@ -4385,14 +4406,14 @@ def test_last_week_of_month_on_offset(): offset = LastWeekOfMonth(n=4, weekday=6) ts = Timestamp("1917-05-27 20:55:27.084284178+0200", tz="Europe/Warsaw") slow = (ts + offset) - offset == ts - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) assert fast == slow # negative n offset = LastWeekOfMonth(n=-4, weekday=5) ts = Timestamp("2005-08-27 05:01:42.799392561-0500", tz="America/Rainy_River") slow = (ts + offset) - offset == ts - fast = offset.onOffset(ts) + fast = offset.is_on_offset(ts) assert fast == slow diff --git a/pandas/tests/tseries/offsets/test_offsets_properties.py b/pandas/tests/tseries/offsets/test_offsets_properties.py index 351f0f9ad3b5b..716d3ff3faf1c 100644 --- a/pandas/tests/tseries/offsets/test_offsets_properties.py +++ b/pandas/tests/tseries/offsets/test_offsets_properties.py @@ -90,11 +90,11 @@ @given(gen_random_datetime, gen_yqm_offset) def test_on_offset_implementations(dt, offset): assume(not offset.normalize) - # check that the class-specific implementations of onOffset match + # check that the class-specific implementations of is_on_offset match # the general case definition: # (dt + offset) - offset == dt compare = (dt + offset) - offset - assert offset.onOffset(dt) == (compare == dt) + assert offset.is_on_offset(dt) == (compare == dt) @pytest.mark.xfail( diff --git a/pandas/tests/tseries/offsets/test_ticks.py b/pandas/tests/tseries/offsets/test_ticks.py index 98a3631c8e63a..2914d4ddf0da0 100644 --- a/pandas/tests/tseries/offsets/test_ticks.py +++ b/pandas/tests/tseries/offsets/test_ticks.py @@ -284,7 +284,7 @@ def test_tick_equalities(cls): @pytest.mark.parametrize("cls", tick_classes) def test_tick_offset(cls): - assert not cls().isAnchored() + assert not cls().is_anchored() @pytest.mark.parametrize("cls", tick_classes) diff --git a/pandas/tests/tseries/offsets/test_yqm_offsets.py b/pandas/tests/tseries/offsets/test_yqm_offsets.py index 12a524d82fcf5..79a0e0f2c25eb 100644 --- a/pandas/tests/tseries/offsets/test_yqm_offsets.py +++ b/pandas/tests/tseries/offsets/test_yqm_offsets.py @@ -23,7 +23,7 @@ YearEnd, ) -from .common import assert_offset_equal, assert_onOffset +from .common import assert_is_on_offset, assert_offset_equal from .test_offsets import Base # -------------------------------------------------------------------- @@ -85,7 +85,7 @@ def test_on_offset(offset): if not (m == 11 and d == 31) ] for date in dates: - res = offset.onOffset(date) + res = offset.is_on_offset(date) slow_version = date == (date + offset) - offset assert res == slow_version @@ -247,9 +247,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) class TestBMonthBegin(Base): @@ -335,9 +335,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) class TestBMonthEnd(Base): @@ -424,9 +424,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) # -------------------------------------------------------------------- @@ -442,10 +442,10 @@ def test_repr(self): expected = "" assert repr(QuarterBegin(startingMonth=1)) == expected - def test_isAnchored(self): - assert QuarterBegin(startingMonth=1).isAnchored() - assert QuarterBegin().isAnchored() - assert not QuarterBegin(2, startingMonth=1).isAnchored() + def test_is_anchored(self): + assert QuarterBegin(startingMonth=1).is_anchored() + assert QuarterBegin().is_anchored() + assert not QuarterBegin(2, startingMonth=1).is_anchored() def test_offset_corner_case(self): # corner @@ -552,10 +552,10 @@ def test_repr(self): expected = "" assert repr(QuarterEnd(startingMonth=1)) == expected - def test_isAnchored(self): - assert QuarterEnd(startingMonth=1).isAnchored() - assert QuarterEnd().isAnchored() - assert not QuarterEnd(2, startingMonth=1).isAnchored() + def test_is_anchored(self): + assert QuarterEnd(startingMonth=1).is_anchored() + assert QuarterEnd().is_anchored() + assert not QuarterEnd(2, startingMonth=1).is_anchored() def test_offset_corner_case(self): # corner @@ -683,9 +683,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) class TestBQuarterBegin(Base): @@ -699,10 +699,10 @@ def test_repr(self): expected = "" assert repr(BQuarterBegin(startingMonth=1)) == expected - def test_isAnchored(self): - assert BQuarterBegin(startingMonth=1).isAnchored() - assert BQuarterBegin().isAnchored() - assert not BQuarterBegin(2, startingMonth=1).isAnchored() + def test_is_anchored(self): + assert BQuarterBegin(startingMonth=1).is_anchored() + assert BQuarterBegin().is_anchored() + assert not BQuarterBegin(2, startingMonth=1).is_anchored() def test_offset_corner_case(self): # corner @@ -824,10 +824,10 @@ def test_repr(self): expected = "" assert repr(BQuarterEnd(startingMonth=1)) == expected - def test_isAnchored(self): - assert BQuarterEnd(startingMonth=1).isAnchored() - assert BQuarterEnd().isAnchored() - assert not BQuarterEnd(2, startingMonth=1).isAnchored() + def test_is_anchored(self): + assert BQuarterEnd(startingMonth=1).is_anchored() + assert BQuarterEnd().is_anchored() + assert not BQuarterEnd(2, startingMonth=1).is_anchored() def test_offset_corner_case(self): # corner @@ -951,9 +951,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) # -------------------------------------------------------------------- @@ -1109,9 +1109,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) class TestYearEnd(Base): @@ -1186,9 +1186,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) class TestYearEndDiffMonth(Base): @@ -1258,9 +1258,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) class TestBYearBegin(Base): @@ -1404,9 +1404,9 @@ def test_offset(self, case): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) class TestBYearEndLagged(Base): @@ -1459,6 +1459,6 @@ def test_roll(self): ] @pytest.mark.parametrize("case", on_offset_cases) - def test_onOffset(self, case): + def test_is_on_offset(self, case): offset, dt, expected = case - assert_onOffset(offset, dt, expected) + assert_is_on_offset(offset, dt, expected) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 26b13b42b1af6..ac64a875ca0aa 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -1,6 +1,7 @@ from datetime import timedelta import re from typing import Dict, Optional +import warnings import numpy as np from pytz import AmbiguousTimeError @@ -125,7 +126,7 @@ def to_offset(freq) -> Optional[DateOffset]: if isinstance(stride, str): name, stride = stride, name name, _ = libfreqs._base_and_stride(name) - delta = get_offset(name) * stride + delta = _get_offset(name) * stride elif isinstance(freq, timedelta): delta = None @@ -166,7 +167,7 @@ def to_offset(freq) -> Optional[DateOffset]: float(stride), prefix ) stride = int(stride) - offset = get_offset(name) + offset = _get_offset(name) offset = offset * int(np.fabs(stride) * stride_sign) if delta is None: delta = offset @@ -185,10 +186,29 @@ def get_offset(name: str) -> DateOffset: """ Return DateOffset object associated with rule name. + .. deprecated:: 1.0.0 + Examples -------- get_offset('EOM') --> BMonthEnd(1) """ + warnings.warn( + "get_offset is deprecated and will be removed in a future version, " + "use to_offset instead", + FutureWarning, + stacklevel=2, + ) + return _get_offset(name) + + +def _get_offset(name: str) -> DateOffset: + """ + Return DateOffset object associated with rule name. + + Examples + -------- + _get_offset('EOM') --> BMonthEnd(1) + """ if name not in libfreqs._dont_uppercase: name = name.upper() name = libfreqs._lite_rule_alias.get(name, name) diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index 75698f7354bf9..f20d385ffbbce 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -2,6 +2,7 @@ import functools import operator from typing import Any, Optional +import warnings from dateutil.easter import easter import numpy as np @@ -165,7 +166,7 @@ class DateOffset(BaseOffset): that conform to the DateOffset. For example, Bday defines this set to be the set of dates that are weekdays (M-F). To test if a date is in the set of a DateOffset dateOffset we can use the - onOffset method: dateOffset.onOffset(date). + is_on_offset method: dateOffset.is_on_offset(date). If a date is not on a valid date, the rollback and rollforward methods can be used to roll the date to the nearest valid date @@ -251,6 +252,7 @@ def __add__(date): _use_relativedelta = False _adjust_dst = False _attributes = frozenset(["n", "normalize"] + list(liboffsets.relativedelta_kwds)) + _deprecations = frozenset(["isAnchored", "onOffset"]) # default for prior pickles normalize = False @@ -363,11 +365,27 @@ def apply_index(self, i): "applied vectorized" ) - def isAnchored(self): + def is_anchored(self): # TODO: Does this make sense for the general case? It would help - # if there were a canonical docstring for what isAnchored means. + # if there were a canonical docstring for what is_anchored means. return self.n == 1 + def onOffset(self, dt): + warnings.warn( + "onOffset is a deprecated, use is_on_offset instead", + FutureWarning, + stacklevel=2, + ) + return self.is_on_offset(dt) + + def isAnchored(self): + warnings.warn( + "isAnchored is a deprecated, use is_anchored instead", + FutureWarning, + stacklevel=2, + ) + return self.is_anchored() + # TODO: Combine this with BusinessMixin version by defining a whitelisted # set of attributes on each object rather than the existing behavior of # iterating over internal ``__dict__`` @@ -400,7 +418,7 @@ def rollback(self, dt): Rolled timestamp if not on offset, otherwise unchanged timestamp. """ dt = as_timestamp(dt) - if not self.onOffset(dt): + if not self.is_on_offset(dt): dt = dt - type(self)(1, normalize=self.normalize, **self.kwds) return dt @@ -414,11 +432,11 @@ def rollforward(self, dt): Rolled timestamp if not on offset, otherwise unchanged timestamp. """ dt = as_timestamp(dt) - if not self.onOffset(dt): + if not self.is_on_offset(dt): dt = dt + type(self)(1, normalize=self.normalize, **self.kwds) return dt - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False # XXX, see #1395 @@ -631,7 +649,7 @@ def apply_index(self, i): result = shifted.to_timestamp() + time return result - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False return dt.weekday() < 5 @@ -728,7 +746,7 @@ def _next_opening_time(self, other, sign=1): earliest_start = self.start[0] latest_start = self.start[-1] - if not self.next_bday.onOffset(other): + if not self.next_bday.is_on_offset(other): # today is not business day other = other + sign * self.next_bday if self.n * sign >= 0: @@ -795,7 +813,7 @@ def rollback(self, dt): """ Roll provided date backward to next offset only if not on offset. """ - if not self.onOffset(dt): + if not self.is_on_offset(dt): if self.n >= 0: dt = self._prev_opening_time(dt) else: @@ -808,7 +826,7 @@ def rollforward(self, dt): """ Roll provided date forward to next offset only if not on offset. """ - if not self.onOffset(dt): + if not self.is_on_offset(dt): if self.n >= 0: return self._next_opening_time(dt) else: @@ -856,13 +874,13 @@ def apply(self, other): # adjust other to reduce number of cases to handle if n >= 0: - if other.time() in self.end or not self._onOffset(other): + if other.time() in self.end or not self._is_on_offset(other): other = self._next_opening_time(other) else: if other.time() in self.start: # adjustment to move to previous business day other = other - timedelta(seconds=1) - if not self._onOffset(other): + if not self._is_on_offset(other): other = self._next_opening_time(other) other = self._get_closing_time(other) @@ -880,7 +898,7 @@ def apply(self, other): if bd != 0: skip_bd = BusinessDay(n=bd) # midnight business hour may not on BusinessDay - if not self.next_bday.onOffset(other): + if not self.next_bday.is_on_offset(other): prev_open = self._prev_opening_time(other) remain = other - prev_open other = prev_open + skip_bd + remain @@ -929,7 +947,7 @@ def apply(self, other): else: raise ApplyTypeError("Only know how to combine business hour with datetime") - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False @@ -939,9 +957,9 @@ def onOffset(self, dt): ) # Valid BH can be on the different BusinessDay during midnight # Distinguish by the time spent from previous opening time - return self._onOffset(dt) + return self._is_on_offset(dt) - def _onOffset(self, dt): + def _is_on_offset(self, dt): """ Slight speedups using calculated values. """ @@ -1061,7 +1079,7 @@ def apply(self, other): def apply_index(self, i): raise NotImplementedError - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False day64 = _to_dt64(dt, "datetime64[D]") @@ -1109,13 +1127,13 @@ class MonthOffset(SingleConstructorOffset): @property def name(self): - if self.isAnchored: + if self.is_anchored: return self.rule_code else: month = ccalendar.MONTH_ALIASES[self.n] return f"{self.code_rule}-{month}" - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False return dt.day == self._get_offset_day(dt) @@ -1197,7 +1215,7 @@ class _CustomBusinessMonth(_CustomMixin, BusinessMixin, MonthOffset): ["n", "normalize", "weekmask", "holidays", "calendar", "offset"] ) - onOffset = DateOffset.onOffset # override MonthOffset method + is_on_offset = DateOffset.is_on_offset # override MonthOffset method apply_index = DateOffset.apply_index # override MonthOffset method def __init__( @@ -1403,7 +1421,7 @@ class SemiMonthEnd(SemiMonthOffset): _prefix = "SM" _min_day_of_month = 1 - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False days_in_month = ccalendar.get_days_in_month(dt.year, dt.month) @@ -1461,7 +1479,7 @@ class SemiMonthBegin(SemiMonthOffset): _prefix = "SMS" - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False return dt.day in (1, self.day_of_month) @@ -1530,7 +1548,7 @@ def __init__(self, n=1, normalize=False, weekday=None): if self.weekday < 0 or self.weekday > 6: raise ValueError(f"Day must be 0<=day<=6, got {self.weekday}") - def isAnchored(self): + def is_anchored(self): return self.n == 1 and self.weekday is not None @apply_wraps @@ -1606,7 +1624,7 @@ def _end_apply_index(self, dtindex): return base + off + Timedelta(1, "ns") - Timedelta(1, "D") - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False elif self.weekday is None: @@ -1649,7 +1667,7 @@ def apply(self, other): to_day = self._get_offset_day(shifted) return liboffsets.shift_day(shifted, to_day - shifted.day) - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False return dt.day == self._get_offset_day(dt) @@ -1806,7 +1824,7 @@ class QuarterOffset(DateOffset): _adjust_dst = True _attributes = frozenset(["n", "normalize", "startingMonth"]) # TODO: Consider combining QuarterOffset and YearOffset __init__ at some - # point. Also apply_index, onOffset, rule_code if + # point. Also apply_index, is_on_offset, rule_code if # startingMonth vs month attr names are resolved def __init__(self, n=1, normalize=False, startingMonth=None): @@ -1816,7 +1834,7 @@ def __init__(self, n=1, normalize=False, startingMonth=None): startingMonth = self._default_startingMonth object.__setattr__(self, "startingMonth", startingMonth) - def isAnchored(self): + def is_anchored(self): return self.n == 1 and self.startingMonth is not None @classmethod @@ -1838,7 +1856,7 @@ def rule_code(self): def apply(self, other): # months_since: find the calendar quarter containing other.month, # e.g. if other.month == 8, the calendar quarter is [Jul, Aug, Sep]. - # Then find the month in that quarter containing an onOffset date for + # Then find the month in that quarter containing an is_on_offset date for # self. `months_since` is the number of months to shift other.month # to get to this on-offset month. months_since = other.month % 3 - self.startingMonth % 3 @@ -1848,7 +1866,7 @@ def apply(self, other): months = qtrs * 3 - months_since return shift_month(other, months, self._day_opt) - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False mod_month = (dt.month - self.startingMonth) % 3 @@ -1951,7 +1969,7 @@ def apply_index(self, dtindex): shifted, freq=dtindex.freq, dtype=dtindex.dtype ) - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False return dt.month == self.month and dt.day == self._get_offset_day(dt) @@ -2091,12 +2109,12 @@ def __init__( if self.variation not in ["nearest", "last"]: raise ValueError(f"{self.variation} is not a valid variation") - def isAnchored(self): + def is_anchored(self): return ( self.n == 1 and self.startingMonth is not None and self.weekday is not None ) - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False dt = datetime(dt.year, dt.month, dt.day) @@ -2320,8 +2338,8 @@ def _offset(self): variation=self.variation, ) - def isAnchored(self): - return self.n == 1 and self._offset.isAnchored() + def is_anchored(self): + return self.n == 1 and self._offset.is_anchored() def _rollback_to_year(self, other): """ @@ -2347,7 +2365,7 @@ def _rollback_to_year(self, other): norm = Timestamp(other).tz_localize(None) start = self._offset.rollback(norm) - # Note: start <= norm and self._offset.onOffset(start) + # Note: start <= norm and self._offset.is_on_offset(start) if start < norm: # roll adjustment @@ -2355,7 +2373,7 @@ def _rollback_to_year(self, other): # check thet qtr_lens is consistent with self._offset addition end = liboffsets.shift_day(start, days=7 * sum(qtr_lens)) - assert self._offset.onOffset(end), (start, end, qtr_lens) + assert self._offset.is_on_offset(end), (start, end, qtr_lens) tdelta = norm - start for qlen in qtr_lens: @@ -2419,10 +2437,10 @@ def year_has_extra_week(self, dt): assert weeks_in_year in [52, 53], weeks_in_year return weeks_in_year == 53 - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False - if self._offset.onOffset(dt): + if self._offset.is_on_offset(dt): return True next_year_end = dt - self._offset @@ -2490,7 +2508,7 @@ def apply(self, other): ) return new - def onOffset(self, dt): + def is_on_offset(self, dt): if self.normalize and not _is_normalized(dt): return False return date(dt.year, dt.month, dt.day) == easter(dt.year) @@ -2622,7 +2640,7 @@ def apply(self, other): raise ApplyTypeError(f"Unhandled type: {type(other).__name__}") - def isAnchored(self): + def is_anchored(self): return False @@ -2727,10 +2745,10 @@ def generate_range(start=None, end=None, periods=None, offset=BDay()): end = Timestamp(end) end = end if end is not NaT else None - if start and not offset.onOffset(start): + if start and not offset.is_on_offset(start): start = offset.rollforward(start) - elif end and not offset.onOffset(end): + elif end and not offset.is_on_offset(end): end = offset.rollback(end) if periods is None and end < start and offset.n >= 0: