-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
simplify algebra in Year offset apply methods #18280
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3a6fc5b
simplify algebra in Year offset apply methods
jbrockmendel d3553ab
docstrings+examples per request
jbrockmendel 99e9ec0
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 369da47
unit tests for liboffsets; fix an unraises ValueError
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Tests for helper functions in the cython tslibs.offsets | ||
""" | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
import pandas as pd | ||
|
||
import pandas._libs.tslibs.offsets as liboffsets | ||
|
||
|
||
def test_shift_month(): | ||
dt = datetime(2017, 11, 15) | ||
|
||
assert liboffsets.shift_month(dt, 0, day_opt=None) == dt | ||
assert liboffsets.shift_month(dt, 0, day_opt=15) == dt | ||
|
||
assert liboffsets.shift_month(dt, 1, | ||
day_opt='start') == datetime(2017, 12, 1) | ||
|
||
assert liboffsets.shift_month(dt, -145, | ||
day_opt='end') == datetime(2005, 10, 31) | ||
|
||
with pytest.raises(ValueError): | ||
liboffsets.shift_month(dt, 3, day_opt='this should raise') | ||
|
||
|
||
def test_get_day_of_month(): | ||
# get_day_of_month is not directly exposed; we test it via roll_yearday | ||
dt = datetime(2017, 11, 15) | ||
|
||
with pytest.raises(ValueError): | ||
# To hit the raising case we need month == dt.month and n > 0 | ||
liboffsets.roll_yearday(dt, n=3, month=11, day_opt='foo') | ||
|
||
|
||
def test_roll_yearday(): | ||
# Copied from doctest examples | ||
month = 3 | ||
day_opt = 'start' # `other` will be compared to March 1 | ||
other = datetime(2017, 2, 10) # before March 1 | ||
assert liboffsets.roll_yearday(other, 2, month, day_opt) == 1 | ||
assert liboffsets.roll_yearday(other, -7, month, day_opt) == -7 | ||
assert liboffsets.roll_yearday(other, 0, month, day_opt) == 0 | ||
|
||
other = pd.Timestamp('2014-03-15', tz='US/Eastern') # after March 1 | ||
assert liboffsets.roll_yearday(other, 2, month, day_opt) == 2 | ||
assert liboffsets.roll_yearday(other, -7, month, day_opt) == -6 | ||
assert liboffsets.roll_yearday(other, 0, month, day_opt) == 1 | ||
|
||
month = 6 | ||
day_opt = 'end' # `other` will be compared to June 30 | ||
other = datetime(1999, 6, 29) # before June 30 | ||
assert liboffsets.roll_yearday(other, 5, month, day_opt) == 4 | ||
assert liboffsets.roll_yearday(other, -7, month, day_opt) == -7 | ||
assert liboffsets.roll_yearday(other, 0, month, day_opt) == 0 | ||
|
||
other = pd.Timestamp(2072, 8, 24, 6, 17, 18) # after June 30 | ||
assert liboffsets.roll_yearday(other, 5, month, day_opt) == 5 | ||
assert liboffsets.roll_yearday(other, -7, month, day_opt) == -6 | ||
assert liboffsets.roll_yearday(other, 0, month, day_opt) == 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
theoretically you should assert day_opt here (because if n=0 it is not checked)
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.
You mean assert that it is valid? Sure.
BTW good call on testing the raising get_day_of_month. The exception gets ignored without an
except -1
.