BUG: WeekOfMonth.apply, LastWeekOfMonth.apply · Issue #18864 · pandas-dev/pandas (original) (raw)

Related: #18672

I am now reasonably confident that WeekOfMonth.apply and LastWeekOfMonth.apply both have a bug caused by using self.getOffsetOfMonth(dt) instead of `self._get_offset_day(dt)

>>> offset = pd.offsets.WeekOfMonth(n=-2, week=2, weekday=3)
>>> ts = pd.Timestamp('2017-12-21 19:32:49.533991002')
>>> offset.onOffset(ts)
True
>>> (ts + offset) - offset ==  ts   # should be the same as offset.onOffset(ts)
False
>>> ts + offset
Timestamp('2017-11-16 19:32:49.533991002')

But given that offset.onOffset(ts) is True, we would expect ts + offset to move back by 2 months, not 1.

Surfacing this bug requires timestamps with non-zero nanos and n<0. I expect there are other cases that would surface with tzaware timestamps.

Not that hard to fix; I'd like to get #18762 in first to avoid merge conflicts.