-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: Timestamp.__sub__(datetimelike) support non-nano #47346
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
from pandas._libs.tslibs.timezones import ( | ||
dateutil_gettz as gettz, | ||
get_timezone, | ||
maybe_get_tz, | ||
tz_compare, | ||
) | ||
from pandas.errors import OutOfBoundsDatetime | ||
|
@@ -712,6 +713,11 @@ def dt64(self, reso): | |
def ts(self, dt64): | ||
return Timestamp._from_dt64(dt64) | ||
|
||
@pytest.fixture | ||
def ts_tz(self, ts, tz_aware_fixture): | ||
tz = maybe_get_tz(tz_aware_fixture) | ||
return Timestamp._from_value_and_reso(ts.value, ts._reso, tz) | ||
|
||
def test_non_nano_construction(self, dt64, ts, reso): | ||
assert ts.value == dt64.view("i8") | ||
|
||
|
@@ -893,6 +899,60 @@ def test_addsub_timedeltalike_non_nano(self, dt64, ts, td): | |
assert result._reso == ts._reso | ||
assert result == expected | ||
|
||
@pytest.mark.xfail(reason="tz_localize not yet implemented for non-nano") | ||
def test_addsub_offset(self, ts_tz): | ||
# specifically non-Tick offset | ||
off = offsets.YearBegin(1) | ||
result = ts_tz + off | ||
|
||
assert isinstance(result, Timestamp) | ||
assert result._reso == ts_tz._reso | ||
# If ts_tz is ever on the last day of the year, the year would be | ||
# incremented by one | ||
assert result.year == ts_tz.year | ||
assert result.day == 31 | ||
assert result.month == 12 | ||
assert tz_compare(result.tz, ts_tz.tz) | ||
|
||
def test_sub_datetimelike_mismatched_reso(self, ts_tz): | ||
# case with non-lossy rounding | ||
ts = ts_tz | ||
|
||
# choose a unit for `other` that doesn't match ts_tz's | ||
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. Mind expanding on this comment that reso parameterization does test the |
||
unit = { | ||
NpyDatetimeUnit.NPY_FR_us.value: "ms", | ||
NpyDatetimeUnit.NPY_FR_ms.value: "s", | ||
NpyDatetimeUnit.NPY_FR_s.value: "us", | ||
}[ts._reso] | ||
other = ts._as_unit(unit) | ||
assert other._reso != ts._reso | ||
|
||
result = ts - other | ||
assert isinstance(result, Timedelta) | ||
assert result.value == 0 | ||
assert result._reso == min(ts._reso, other._reso) | ||
|
||
result = other - ts | ||
assert isinstance(result, Timedelta) | ||
assert result.value == 0 | ||
assert result._reso == min(ts._reso, other._reso) | ||
|
||
# TODO: clarify in message that add/sub is allowed only when lossless? | ||
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.
|
||
msg = "Cannot losslessly convert units" | ||
if ts._reso < other._reso: | ||
# Case where rounding is lossy | ||
other2 = other + Timedelta._from_value_and_reso(1, other._reso) | ||
with pytest.raises(ValueError, match=msg): | ||
ts - other2 | ||
with pytest.raises(ValueError, match=msg): | ||
other2 - ts | ||
else: | ||
ts2 = ts + Timedelta._from_value_and_reso(1, ts._reso) | ||
with pytest.raises(ValueError, match=msg): | ||
ts2 - other | ||
with pytest.raises(ValueError, match=msg): | ||
other - ts2 | ||
|
||
|
||
class TestAsUnit: | ||
def test_as_unit(self): | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.