Skip to content

Commit

Permalink
bugfix case with bunched yearends
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Nov 28, 2017
1 parent 2a0e54b commit 605c8f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
15 changes: 15 additions & 0 deletions pandas/tests/tseries/offsets/test_fiscal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pandas.util.testing as tm

from pandas import Timestamp
from pandas.tseries.frequencies import get_offset, _INVALID_FREQ_ERROR
from pandas.tseries.offsets import FY5253Quarter, FY5253
from pandas._libs.tslibs.offsets import WeekDay
Expand Down Expand Up @@ -604,3 +605,17 @@ def test_offset(self):
assert_offset_equal(offset2,
datetime(2013, 1, 15),
datetime(2013, 3, 30))


def test_bunched_yearends():
# GH#14774 cases with two fiscal year-ends in the same calendar-year
fy = FY5253(n=1, weekday=5, startingMonth=12, variation='nearest')
dt = Timestamp('2004-01-01')
assert fy.rollback(dt) == Timestamp('2002-12-28')
assert (-fy).apply(dt) == Timestamp('2002-12-28')
assert dt - fy == Timestamp('2002-12-28')

assert fy.rollforward(dt) == Timestamp('2004-01-03')
assert fy.apply(dt) == Timestamp('2004-01-03')
assert fy + dt == Timestamp('2004-01-03')
assert dt + fy == Timestamp('2004-01-03')
13 changes: 9 additions & 4 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1869,21 +1869,26 @@ def apply(self, other):
elif n > 0:
if other < prev_year:
n -= 2
# TODO: Not hit in tests
elif other < cur_year:
elif prev_year < other < cur_year:
n -= 1
elif other < next_year:
elif cur_year < other < next_year:
pass
else:
assert False
else:
if other > next_year:
n += 2
# TODO: Not hit in tests
# TODO: Not hit in tests; UPDATE: looks impossible
elif other > cur_year:
n += 1
elif other > prev_year:
pass
elif (other.year == prev_year.year and other < prev_year and
prev_year - other <= timedelta(6)):
# GH#14774, error when next_year.year == cur_year.year
# e.g. prev_year == datetime(2004, 1, 3),
# other == datetime(2004, 1, 1)
n -= 1
else:
assert False

Expand Down

0 comments on commit 605c8f5

Please sign in to comment.