-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
ENH: implement Timedelta.__mod__ and __divmod__ #19755
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
48cb394
implement Timedelta.__mod__ etc, test but do _not_ fix related bugs
jbrockmendel 5782463
try to reference doc section
jbrockmendel d6ac27d
Merge branch 'master' of https://github.com/pandas-dev/pandas into td…
jbrockmendel c252eff
move tests to test_arithmetic
jbrockmendel c78ed1b
remove duplicated doc line
jbrockmendel 3e8529e
Merge branch 'master' into PR_TOOL_MERGE_PR_19755
jreback aefa6d6
remove issue reference & fix ref
jreback 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 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 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 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 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 |
---|---|---|
|
@@ -420,3 +420,189 @@ def test_td_rfloordiv_numeric_series(self): | |
assert res is NotImplemented | ||
with pytest.raises(TypeError): | ||
ser // td | ||
|
||
def test_mod_timedeltalike(self): | ||
# GH#19365 | ||
td = Timedelta(hours=37) | ||
|
||
# Timedelta-like others | ||
result = td % Timedelta(hours=6) | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(hours=1) | ||
|
||
result = td % timedelta(minutes=60) | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(0) | ||
|
||
result = td % NaT | ||
assert result is NaT | ||
|
||
@pytest.mark.xfail(reason='GH#19378 floordiv td64 returns td64') | ||
def test_mod_timedelta64_nat(self): | ||
# GH#19365 | ||
td = Timedelta(hours=37) | ||
|
||
result = td % np.timedelta64('NaT', 'ns') | ||
assert result is NaT | ||
|
||
@pytest.mark.xfail(reason='GH#19378 floordiv td64 returns td64') | ||
def test_mod_timedelta64(self): | ||
# GH#19365 | ||
td = Timedelta(hours=37) | ||
|
||
result = td % np.timedelta64(2, 'h') | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(hours=1) | ||
|
||
@pytest.mark.xfail(reason='GH#19378 floordiv by Tick not implemented') | ||
def test_mod_offset(self): | ||
# GH#19365 | ||
td = Timedelta(hours=37) | ||
|
||
result = td % pd.offsets.Hour(5) | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(hours=2) | ||
|
||
# ---------------------------------------------------------------- | ||
# Timedelta.__mod__, __rmod__ | ||
|
||
def test_mod_numeric(self): | ||
# GH#19365 | ||
td = Timedelta(hours=37) | ||
|
||
# Numeric Others | ||
result = td % 2 | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(0) | ||
|
||
result = td % 1e12 | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(minutes=3, seconds=20) | ||
|
||
result = td % int(1e12) | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(minutes=3, seconds=20) | ||
|
||
def test_mod_invalid(self): | ||
# GH#19365 | ||
td = Timedelta(hours=37) | ||
|
||
with pytest.raises(TypeError): | ||
td % pd.Timestamp('2018-01-22') | ||
|
||
with pytest.raises(TypeError): | ||
td % [] | ||
|
||
def test_rmod_pytimedelta(self): | ||
# GH#19365 | ||
td = Timedelta(minutes=3) | ||
|
||
result = timedelta(minutes=4) % td | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(minutes=1) | ||
|
||
@pytest.mark.xfail(reason='GH#19378 floordiv by Tick not implemented') | ||
def test_rmod_timedelta64(self): | ||
# GH#19365 | ||
td = Timedelta(minutes=3) | ||
result = np.timedelta64(5, 'm') % td | ||
assert isinstance(result, Timedelta) | ||
assert result == Timedelta(minutes=2) | ||
|
||
def test_rmod_invalid(self): | ||
# GH#19365 | ||
td = Timedelta(minutes=3) | ||
|
||
with pytest.raises(TypeError): | ||
pd.Timestamp('2018-01-22') % td | ||
|
||
with pytest.raises(TypeError): | ||
15 % td | ||
|
||
with pytest.raises(TypeError): | ||
16.0 % td | ||
|
||
with pytest.raises(TypeError): | ||
np.array([22, 24]) % td | ||
|
||
# ---------------------------------------------------------------- | ||
# Timedelta.__divmod__, __rdivmod__ | ||
|
||
def test_divmod_numeric(self): | ||
# GH#19365 | ||
td = Timedelta(days=2, hours=6) | ||
|
||
result = divmod(td, 53 * 3600 * 1e9) | ||
assert result[0] == Timedelta(1, unit='ns') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slightly OT: we have test_nat.py so either should add this there, or have here, or maybe both. |
||
assert isinstance(result[1], Timedelta) | ||
assert result[1] == Timedelta(hours=1) | ||
|
||
assert result | ||
result = divmod(td, np.nan) | ||
assert result[0] is pd.NaT | ||
assert result[1] is pd.NaT | ||
|
||
def test_divmod(self): | ||
# GH#19365 | ||
td = Timedelta(days=2, hours=6) | ||
|
||
result = divmod(td, timedelta(days=1)) | ||
assert result[0] == 2 | ||
assert isinstance(result[1], Timedelta) | ||
assert result[1] == Timedelta(hours=6) | ||
|
||
result = divmod(td, 54) | ||
assert result[0] == Timedelta(hours=1) | ||
assert isinstance(result[1], Timedelta) | ||
assert result[1] == Timedelta(0) | ||
|
||
result = divmod(td, pd.NaT) | ||
assert np.isnan(result[0]) | ||
assert result[1] is pd.NaT | ||
|
||
@pytest.mark.xfail(reason='GH#19378 floordiv by Tick not implemented') | ||
def test_divmod_offset(self): | ||
# GH#19365 | ||
td = Timedelta(days=2, hours=6) | ||
|
||
result = divmod(td, pd.offsets.Hour(-4)) | ||
assert result[0] == -14 | ||
assert isinstance(result[1], Timedelta) | ||
assert result[1] == Timedelta(hours=-2) | ||
|
||
def test_divmod_invalid(self): | ||
# GH#19365 | ||
td = Timedelta(days=2, hours=6) | ||
|
||
with pytest.raises(TypeError): | ||
divmod(td, pd.Timestamp('2018-01-22')) | ||
|
||
def test_rdivmod_pytimedelta(self): | ||
# GH#19365 | ||
result = divmod(timedelta(days=2, hours=6), Timedelta(days=1)) | ||
assert result[0] == 2 | ||
assert isinstance(result[1], Timedelta) | ||
assert result[1] == Timedelta(hours=6) | ||
|
||
@pytest.mark.xfail(reason='GH#19378 floordiv by Tick not implemented') | ||
def test_rdivmod_offset(self): | ||
result = divmod(pd.offsets.Hour(54), Timedelta(hours=-4)) | ||
assert result[0] == -14 | ||
assert isinstance(result[1], Timedelta) | ||
assert result[1] == Timedelta(hours=-2) | ||
|
||
def test_rdivmod_invalid(self): | ||
# GH#19365 | ||
td = Timedelta(minutes=3) | ||
|
||
with pytest.raises(TypeError): | ||
divmod(pd.Timestamp('2018-01-22'), td) | ||
|
||
with pytest.raises(TypeError): | ||
divmod(15, td) | ||
|
||
with pytest.raises(TypeError): | ||
divmod(16.0, td) | ||
|
||
with pytest.raises(TypeError): | ||
divmod(np.array([22, 24]), td) |
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.
nice!