Skip to content

BUG: Easter works incorrectly in negative offsets #7195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ Bug Fixes
wouldn't test ``True`` when it encountered an ``inf``/``-inf``
(:issue:`7315`).
- Bug in inferred_freq results in None for eastern hemisphere timezones (:issue:`7310`)
- Bug in ``Easter`` returns incorrect date when offset is negative (:issue:`7195`)
14 changes: 9 additions & 5 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,12 +1846,16 @@ def apply(self, other):
currentEaster = datetime(currentEaster.year, currentEaster.month, currentEaster.day)

# NOTE: easter returns a datetime.date so we have to convert to type of other
if other >= currentEaster:
new = easter(other.year + self.n)
elif other < currentEaster:
new = easter(other.year + self.n - 1)
if self.n >= 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if other >= currentEaster:
new = easter(other.year + self.n)
else:
new = easter(other.year + self.n - 1)
else:
new = other
if other > currentEaster:
new = easter(other.year + self.n + 1)
else:
new = easter(other.year + self.n)

# FIXME: There has to be a better way to do this, but I don't know what it is
if isinstance(other, Timestamp):
Expand Down
14 changes: 14 additions & 0 deletions pandas/tseries/tests/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2483,16 +2483,30 @@ def test_onOffset(self):
def assertEq(offset, base, expected):
actual = offset + base
actual_swapped = base + offset
actual_apply = offset.apply(base)
try:
assert actual == expected
assert actual_swapped == expected
assert actual_apply == expected
except AssertionError:
raise AssertionError("\nExpected: %s\nActual: %s\nFor Offset: %s)"
"\nAt Date: %s" %
(expected, actual, offset, base))

def test_Easter():
assertEq(Easter(), datetime(2010, 1, 1), datetime(2010, 4, 4))
assertEq(Easter(), datetime(2010, 4, 5), datetime(2011, 4, 24))
assertEq(Easter(2), datetime(2010, 1, 1), datetime(2011, 4, 24))

assertEq(Easter(), datetime(2010, 4, 4), datetime(2011, 4, 24))
assertEq(Easter(2), datetime(2010, 4, 4), datetime(2012, 4, 8))

assertEq(-Easter(), datetime(2011, 1, 1), datetime(2010, 4, 4))
assertEq(-Easter(), datetime(2010, 4, 5), datetime(2010, 4, 4))
assertEq(-Easter(2), datetime(2011, 1, 1), datetime(2009, 4, 12))

assertEq(-Easter(), datetime(2010, 4, 4), datetime(2009, 4, 12))
assertEq(-Easter(2), datetime(2010, 4, 4), datetime(2008, 3, 23))

def test_Hour():
assertEq(Hour(), datetime(2010, 1, 1), datetime(2010, 1, 1, 1))
Expand Down