Skip to content

Commit 9434a5a

Browse files
jbrockmendeljreback
authored andcommitted
bugfix for FY5253 case with bunched yearends (#18550)
1 parent 537c06d commit 9434a5a

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

doc/source/whatsnew/v0.22.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,5 @@ Other
265265
- Fixed a bug where creating a Series from an array that contains both tz-naive and tz-aware values will result in a Series whose dtype is tz-aware instead of object (:issue:`16406`)
266266
- Fixed construction of a :class:`Series` from a ``dict`` containing ``NaN`` as key (:issue:`18480`)
267267
- Adding a ``Period`` object to a ``datetime`` or ``Timestamp`` object will now correctly raise a ``TypeError`` (:issue:`17983`)
268+
- Fixed a bug where ``FY5253`` date offsets could incorrectly raise an ``AssertionError`` in arithmetic operatons (:issue:`14774`)
269+
-

pandas/tests/tseries/offsets/test_fiscal.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import pandas.util.testing as tm
1111

12+
from pandas import Timestamp
1213
from pandas.tseries.frequencies import get_offset, _INVALID_FREQ_ERROR
1314
from pandas.tseries.offsets import FY5253Quarter, FY5253
1415
from pandas._libs.tslibs.offsets import WeekDay
@@ -604,3 +605,23 @@ def test_offset(self):
604605
assert_offset_equal(offset2,
605606
datetime(2013, 1, 15),
606607
datetime(2013, 3, 30))
608+
609+
610+
def test_bunched_yearends():
611+
# GH#14774 cases with two fiscal year-ends in the same calendar-year
612+
fy = FY5253(n=1, weekday=5, startingMonth=12, variation='nearest')
613+
dt = Timestamp('2004-01-01')
614+
assert fy.rollback(dt) == Timestamp('2002-12-28')
615+
assert (-fy).apply(dt) == Timestamp('2002-12-28')
616+
assert dt - fy == Timestamp('2002-12-28')
617+
618+
assert fy.rollforward(dt) == Timestamp('2004-01-03')
619+
assert fy.apply(dt) == Timestamp('2004-01-03')
620+
assert fy + dt == Timestamp('2004-01-03')
621+
assert dt + fy == Timestamp('2004-01-03')
622+
623+
# Same thing, but starting from a Timestamp in the previous year.
624+
dt = Timestamp('2003-12-31')
625+
assert fy.rollback(dt) == Timestamp('2002-12-28')
626+
assert (-fy).apply(dt) == Timestamp('2002-12-28')
627+
assert dt - fy == Timestamp('2002-12-28')

pandas/tseries/offsets.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,21 +1831,26 @@ def apply(self, other):
18311831
elif n > 0:
18321832
if other < prev_year:
18331833
n -= 2
1834-
# TODO: Not hit in tests
1835-
elif other < cur_year:
1834+
elif prev_year < other < cur_year:
18361835
n -= 1
1837-
elif other < next_year:
1836+
elif cur_year < other < next_year:
18381837
pass
18391838
else:
18401839
assert False
18411840
else:
1842-
if other > next_year:
1841+
if next_year < other:
18431842
n += 2
1844-
# TODO: Not hit in tests
1845-
elif other > cur_year:
1843+
# TODO: Not hit in tests; UPDATE: looks impossible
1844+
elif cur_year < other < next_year:
18461845
n += 1
1847-
elif other > prev_year:
1846+
elif prev_year < other < cur_year:
18481847
pass
1848+
elif (other.year == prev_year.year and other < prev_year and
1849+
prev_year - other <= timedelta(6)):
1850+
# GH#14774, error when next_year.year == cur_year.year
1851+
# e.g. prev_year == datetime(2004, 1, 3),
1852+
# other == datetime(2004, 1, 1)
1853+
n -= 1
18491854
else:
18501855
assert False
18511856

0 commit comments

Comments
 (0)