def test_sub_timedeltalike_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; # this construction ensures we get cases with other._reso < ts._reso # and cases with other._reso > ts._reso unit = { NpyDatetimeUnit.NPY_FR_us.value: "ms", NpyDatetimeUnit.NPY_FR_ms.value: "s", NpyDatetimeUnit.NPY_FR_s.value: "us", }[ts._reso] other = Timedelta(0)._as_unit(unit) assert other._reso != ts._reso result = ts + other assert isinstance(result, Timestamp) assert result == ts assert result._reso == min(ts._reso, other._reso) result = other + ts assert isinstance(result, Timestamp) assert result == ts assert result._reso == min(ts._reso, other._reso) msg = "Timestamp addition with mismatched resolutions" 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 msg = "Addition between Timestamp and Timedelta with mismatched resolutions" with pytest.raises(ValueError, match=msg): # With a mismatched td64 as opposed to Timedelta ts + np.timedelta64(1, "ns")
def test_as_unit_overflows(self): # microsecond that would be just out of bounds for nano us = 9223372800000000 td = Timedelta._from_value_and_reso(us, NpyDatetimeUnit.NPY_FR_us.value) msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow" with pytest.raises(OutOfBoundsTimedelta, match=msg): td._as_unit("ns") res = td._as_unit("ms") assert res.value == us // 1000 assert res._reso == NpyDatetimeUnit.NPY_FR_ms.value
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; # this construction ensures we get cases with other._reso < ts._reso # and cases with other._reso > ts._reso 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) msg = "Timestamp subtraction with mismatched resolutions" 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
def test_sub_preserves_reso(self, td, unit): res = td - td expected = Timedelta._from_value_and_reso(0, unit) assert res == expected assert res._reso == unit
def test_from_value_and_reso(self, unit, val): # Just checking that the fixture is giving us what we asked for td = Timedelta._from_value_and_reso(val, unit) assert td.value == val assert td._reso == unit assert td.days == 106752
def td(self, unit, val): return Timedelta._from_value_and_reso(val, unit)
def test_resolution(self, td): expected = Timedelta._from_value_and_reso(1, td._reso) result = td.resolution assert result == expected assert result._reso == expected._reso