-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
DEPR: the method is_anchored() for offsets #56594
Changes from all commits
287fdef
8c870f6
df78796
005198b
e703817
d031fad
b0d7ef4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -756,18 +756,27 @@ cdef class BaseOffset: | |||||
raise ValueError(f"{self} is a non-fixed frequency") | ||||||
|
||||||
def is_anchored(self) -> bool: | ||||||
# TODO: Does this make sense for the general case? It would help | ||||||
# if there were a canonical docstring for what is_anchored means. | ||||||
# GH#55388 | ||||||
""" | ||||||
Return boolean whether the frequency is a unit frequency (n=1). | ||||||
|
||||||
.. deprecated:: 2.2.0 | ||||||
is_anchored is deprecated and will be removed in a future version. | ||||||
Use ``obj.n == 1`` instead. | ||||||
|
||||||
Examples | ||||||
-------- | ||||||
>>> pd.DateOffset().is_anchored() | ||||||
True | ||||||
>>> pd.DateOffset(2).is_anchored() | ||||||
False | ||||||
""" | ||||||
warnings.warn( | ||||||
f"{type(self).__name__}.is_anchored is deprecated and will be removed " | ||||||
f"in a future version, please use \'obj.n == 1\' instead.", | ||||||
FutureWarning, | ||||||
stacklevel=find_stack_level(), | ||||||
) | ||||||
return self.n == 1 | ||||||
|
||||||
# ------------------------------------------------------------------ | ||||||
|
@@ -954,6 +963,27 @@ cdef class Tick(SingleConstructorOffset): | |||||
return True | ||||||
|
||||||
def is_anchored(self) -> bool: | ||||||
# GH#55388 | ||||||
""" | ||||||
Return False. | ||||||
|
||||||
.. deprecated:: 2.2.0 | ||||||
is_anchored is deprecated and will be removed in a future version. | ||||||
Use ``False`` instead. | ||||||
|
||||||
Examples | ||||||
-------- | ||||||
>>> pd.offsets.Hour().is_anchored() | ||||||
False | ||||||
>>> pd.offsets.Hour(2).is_anchored() | ||||||
False | ||||||
""" | ||||||
warnings.warn( | ||||||
f"{type(self).__name__}.is_anchored is deprecated and will be removed " | ||||||
f"in a future version, please use False instead.", | ||||||
FutureWarning, | ||||||
stacklevel=find_stack_level(), | ||||||
) | ||||||
return False | ||||||
|
||||||
# This is identical to BaseOffset.__hash__, but has to be redefined here | ||||||
|
@@ -2663,6 +2693,13 @@ cdef class QuarterOffset(SingleConstructorOffset): | |||||
return f"{self._prefix}-{month}" | ||||||
|
||||||
def is_anchored(self) -> bool: | ||||||
warnings.warn( | ||||||
f"{type(self).__name__}.is_anchored is deprecated and will be removed " | ||||||
f"in a future version, please use \'obj.n == 1 " | ||||||
f"and obj.startingMonth is not None\' instead.", | ||||||
FutureWarning, | ||||||
stacklevel=find_stack_level(), | ||||||
) | ||||||
return self.n == 1 and self.startingMonth is not None | ||||||
|
||||||
def is_on_offset(self, dt: datetime) -> bool: | ||||||
|
@@ -3308,6 +3345,13 @@ cdef class Week(SingleConstructorOffset): | |||||
self._cache = state.pop("_cache", {}) | ||||||
|
||||||
def is_anchored(self) -> bool: | ||||||
warnings.warn( | ||||||
f"{type(self).__name__}.is_anchored is deprecated and will be removed " | ||||||
f"in a future version, please use \'obj.n == 1 " | ||||||
f"and obj.weekday is not None\' instead.", | ||||||
FutureWarning, | ||||||
stacklevel=find_stack_level(), | ||||||
) | ||||||
return self.n == 1 and self.weekday is not None | ||||||
|
||||||
@apply_wraps | ||||||
|
@@ -3597,6 +3641,12 @@ cdef class FY5253Mixin(SingleConstructorOffset): | |||||
self.variation = state.pop("variation") | ||||||
|
||||||
def is_anchored(self) -> bool: | ||||||
warnings.warn( | ||||||
f"{type(self).__name__}.is_anchored is deprecated and will be removed " | ||||||
f"in a future version, please use \'obj.n == 1\' instead.", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we not want to say about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about this. I think, there is no need to check if parameters pandas/pandas/_libs/tslibs/offsets.pyx Lines 3579 to 3580 in 1381f70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, thanks, they can't actually be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we check:
On the other hand for the class Week we have
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but also, trying to pass In [12]: FY5253(n=1, weekday=1, startingMonth=1).is_anchored()
<ipython-input-12-37644e23496a>:1: FutureWarning: FY5253.is_anchored is deprecated and will be removed in a future version, please use 'obj.n == 1' instead.
FY5253(n=1, weekday=1, startingMonth=1).is_anchored()
Out[12]: True
In [13]: FY5253(n=1, weekday=None, startingMonth=None).is_anchored()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[13], line 1
----> 1 FY5253(n=1, weekday=None, startingMonth=None).is_anchored()
File offsets.pyx:3627, in pandas._libs.tslibs.offsets.FY5253Mixin.__init__()
TypeError: an integer is required
In [14]: FY5253(n=1, weekday=None, startingMonth=1).is_anchored()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[14], line 1
----> 1 FY5253(n=1, weekday=None, startingMonth=1).is_anchored()
File offsets.pyx:3628, in pandas._libs.tslibs.offsets.FY5253Mixin.__init__()
TypeError: an integer is required
In [15]: FY5253(n=1, weekday=1, startingMonth=None).is_anchored()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[15], line 1
----> 1 FY5253(n=1, weekday=1, startingMonth=None).is_anchored()
File offsets.pyx:3627, in pandas._libs.tslibs.offsets.FY5253Mixin.__init__()
TypeError: an integer is required |
||||||
FutureWarning, | ||||||
stacklevel=find_stack_level(), | ||||||
) | ||||||
return ( | ||||||
self.n == 1 and self.startingMonth is not None and self.weekday is not None | ||||||
) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this deprecation, how can I check for the equivalent attribute here? Use False is not really helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for this class it's always
False
for others you typically need to check that
obj.n==1
andobj.weekday is None
(if it has a weekday attribute) andobj.startingMonth is None
(if it has astartingMonth
attribute)what are you using it for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are generally looking for freqs that aren’t anchored
I thought a little bit about this and the deprecations isn’t great, it makes those checks significantly more complex than before for users
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that we can advise users use False, because
is_anchored
always returnsFalse
forTick
subclasses.Do you think it would be better to use some expression that is always False instead of False, like
pd.offsets.Hour().n == None
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mostly don’t want to have different check for every group, that’s what makes this ugly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That helps, thx
We have to make a decision about shifting divisions, this basically goes back how the offset behaves on different values, e.g. if it can shift different values to the same target (if it's anchored for example) then we have to make a different decision compared to if every value maps to a distinct target value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An example might help here. I think what you're referring to is what past-me thought is_anchored meant (xref #44025), e.g "W-SUN" would be anchored, but "W" would not (it would act like "7D").
pd.DateOffset(day=1)
would, butpd.DateOffset(days=1)
would not.NB: the "is equivalent to" i gave about isn't quite right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first one just shifts 7 days, so everything is fine
The second one shifts to a specific weekday though, which is an issue for us, because it maps 2 different days to the same target
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That matches what I think a more-useful is_anchored would mean. My guess is we can come up with examples (e.g. the day=1 vs days=1 above) where checking is_anchored would be wrong for your use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first one just shifts 14 days, and the second one shifts to a specific weekday
Yet neither "is_anchored":