Skip to content

Commit 287fdef

Browse files
committed
deprecate is_anchored, fix tests
1 parent e0e47e8 commit 287fdef

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

pandas/_libs/tslibs/offsets.pyx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,12 @@ cdef class BaseOffset:
768768
>>> pd.DateOffset(2).is_anchored()
769769
False
770770
"""
771+
warnings.warn(
772+
f"{type(self).__name__}.is_anchored() is deprecated and will be removed "
773+
f"in a future version, please use {type(self).__name__}.n == 1 instead.",
774+
FutureWarning,
775+
stacklevel=find_stack_level(),
776+
)
771777
return self.n == 1
772778

773779
# ------------------------------------------------------------------
@@ -954,6 +960,12 @@ cdef class Tick(SingleConstructorOffset):
954960
return True
955961

956962
def is_anchored(self) -> bool:
963+
warnings.warn(
964+
f"{type(self).__name__}.is_anchored() is deprecated and will be removed "
965+
f"in a future version, please use False instead.",
966+
FutureWarning,
967+
stacklevel=find_stack_level(),
968+
)
957969
return False
958970

959971
# This is identical to BaseOffset.__hash__, but has to be redefined here
@@ -2663,6 +2675,13 @@ cdef class QuarterOffset(SingleConstructorOffset):
26632675
return f"{self._prefix}-{month}"
26642676

26652677
def is_anchored(self) -> bool:
2678+
warnings.warn(
2679+
f"{type(self).__name__}.is_anchored() is deprecated and will be removed "
2680+
f"in a future version, please use {type(self).__name__}.n == 1 "
2681+
f" and {type(self).__name__}.startingMonth is not None instead.",
2682+
FutureWarning,
2683+
stacklevel=find_stack_level(),
2684+
)
26662685
return self.n == 1 and self.startingMonth is not None
26672686

26682687
def is_on_offset(self, dt: datetime) -> bool:
@@ -3308,6 +3327,13 @@ cdef class Week(SingleConstructorOffset):
33083327
self._cache = state.pop("_cache", {})
33093328

33103329
def is_anchored(self) -> bool:
3330+
warnings.warn(
3331+
f"{type(self).__name__}.is_anchored() is deprecated and will be removed "
3332+
f"in a future version, please use {type(self).__name__}.n == 1 "
3333+
f" and {type(self).__name__}.weekday is not None instead.",
3334+
FutureWarning,
3335+
stacklevel=find_stack_level(),
3336+
)
33113337
return self.n == 1 and self.weekday is not None
33123338

33133339
@apply_wraps
@@ -3597,6 +3623,12 @@ cdef class FY5253Mixin(SingleConstructorOffset):
35973623
self.variation = state.pop("variation")
35983624

35993625
def is_anchored(self) -> bool:
3626+
warnings.warn(
3627+
f"{type(self).__name__}.is_anchored() is deprecated and will be removed "
3628+
f"in a future version, please use {type(self).__name__}.n == 1 instead.",
3629+
FutureWarning,
3630+
stacklevel=find_stack_level(),
3631+
)
36003632
return (
36013633
self.n == 1 and self.startingMonth is not None and self.weekday is not None
36023634
)

pandas/tests/indexes/interval/test_interval_range.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_constructor_timestamp(self, closed, name, freq, periods, tz):
8484
tm.assert_index_equal(result, expected)
8585

8686
# GH 20976: linspace behavior defined from start/end/periods
87-
if not breaks.freq.is_anchored() and tz is None:
87+
if not breaks.freq.n == 1 and tz is None:
8888
# matches expected only for non-anchored offsets and tz naive
8989
# (anchored/DST transitions cause unequal spacing in expected)
9090
result = interval_range(

pandas/tests/tseries/offsets/test_ticks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ def test_tick_equalities(cls):
339339

340340
@pytest.mark.parametrize("cls", tick_classes)
341341
def test_tick_offset(cls):
342-
assert not cls().is_anchored()
342+
msg = f"{cls.__name__}.is_anchored() is deprecated and will be removed in a "
343+
"future version, please use False instead."
344+
345+
with tm.assert_produces_warning(FutureWarning, match=msg):
346+
assert not cls().is_anchored()
343347

344348

345349
@pytest.mark.parametrize("cls", tick_classes)

0 commit comments

Comments
 (0)