Skip to content

REF: misplaced Timedelta tests #32871

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 5 commits into from
Mar 21, 2020
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
64 changes: 64 additions & 0 deletions pandas/tests/indexes/timedeltas/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,67 @@ def test_tdi_round(self):
td.round(freq="M")
with pytest.raises(ValueError, match=msg):
elt.round(freq="M")

@pytest.mark.parametrize(
"freq,msg",
[
("Y", "<YearEnd: month=12> is a non-fixed frequency"),
("M", "<MonthEnd> is a non-fixed frequency"),
("foobar", "Invalid frequency: foobar"),
],
)
def test_tdi_round_invalid(self, freq, msg):
t1 = timedelta_range("1 days", periods=3, freq="1 min 2 s 3 us")

with pytest.raises(ValueError, match=msg):
t1.round(freq)
with pytest.raises(ValueError, match=msg):
# Same test for TimedeltaArray
t1._data.round(freq)

# TODO: de-duplicate with test_tdi_round
def test_round(self):
t1 = timedelta_range("1 days", periods=3, freq="1 min 2 s 3 us")
t2 = -1 * t1
t1a = timedelta_range("1 days", periods=3, freq="1 min 2 s")
t1c = TimedeltaIndex([1, 1, 1], unit="D")

# note that negative times round DOWN! so don't give whole numbers
for (freq, s1, s2) in [
("N", t1, t2),
("U", t1, t2),
(
"L",
t1a,
TimedeltaIndex(
["-1 days +00:00:00", "-2 days +23:58:58", "-2 days +23:57:56"],
),
),
(
"S",
t1a,
TimedeltaIndex(
["-1 days +00:00:00", "-2 days +23:58:58", "-2 days +23:57:56"],
),
),
("12T", t1c, TimedeltaIndex(["-1 days", "-1 days", "-1 days"],),),
("H", t1c, TimedeltaIndex(["-1 days", "-1 days", "-1 days"],),),
("d", t1c, TimedeltaIndex([-1, -1, -1], unit="D")),
]:

r1 = t1.round(freq)
tm.assert_index_equal(r1, s1)
r2 = t2.round(freq)
tm.assert_index_equal(r2, s2)

def test_components(self):
rng = timedelta_range("1 days, 10:11:12", periods=2, freq="s")
rng.components

# with nat
s = Series(rng)
s[1] = np.nan

result = s.dt.components
assert not result.iloc[0].isna().all()
assert result.iloc[1].isna().all()
160 changes: 160 additions & 0 deletions pandas/tests/scalar/timedelta/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def test_td_add_datetimelike_scalar(self, op):
result = op(td, NaT)
assert result is NaT

def test_td_add_timestamp_overflow(self):
with pytest.raises(OverflowError):
Timestamp("1700-01-01") + Timedelta(13 * 19999, unit="D")

with pytest.raises(OverflowError):
Timestamp("1700-01-01") + timedelta(days=13 * 19999)

@pytest.mark.parametrize("op", [operator.add, ops.radd])
def test_td_add_td(self, op):
td = Timedelta(10, unit="d")
Expand Down Expand Up @@ -365,6 +372,26 @@ def test_td_div_timedeltalike_scalar(self):

assert np.isnan(td / NaT)

def test_td_div_td64_non_nano(self):

# truediv
td = Timedelta("1 days 2 hours 3 ns")
result = td / np.timedelta64(1, "D")
assert result == td.value / float(86400 * 1e9)
result = td / np.timedelta64(1, "s")
assert result == td.value / float(1e9)
result = td / np.timedelta64(1, "ns")
assert result == td.value

# floordiv
td = Timedelta("1 days 2 hours 3 ns")
result = td // np.timedelta64(1, "D")
assert result == 1
result = td // np.timedelta64(1, "s")
assert result == 93600
result = td // np.timedelta64(1, "ns")
assert result == td.value

def test_td_div_numeric_scalar(self):
# GH#19738
td = Timedelta(10, unit="d")
Expand Down Expand Up @@ -589,6 +616,13 @@ def test_td_rfloordiv_timedeltalike_array(self):
expected = np.array([10, np.nan])
tm.assert_numpy_array_equal(res, expected)

def test_td_rfloordiv_intarray(self):
# deprecated GH#19761, enforced GH#29797
ints = np.array([1349654400, 1349740800, 1349827200, 1349913600]) * 10 ** 9

with pytest.raises(TypeError, match="Invalid dtype"):
ints // Timedelta(1, unit="s")

def test_td_rfloordiv_numeric_series(self):
# GH#18846
td = Timedelta(hours=3, minutes=3)
Expand Down Expand Up @@ -796,3 +830,129 @@ def test_rdivmod_invalid(self):
def test_td_op_timedelta_timedeltalike_array(self, op, arr):
with pytest.raises(TypeError):
op(arr, Timedelta("1D"))


class TestTimedeltaComparison:
def test_compare_tick(self, tick_classes):
cls = tick_classes

off = cls(4)
td = off.delta
assert isinstance(td, Timedelta)

assert td == off
assert not td != off
assert td <= off
assert td >= off
assert not td < off
assert not td > off

assert not td == 2 * off
assert td != 2 * off
assert td <= 2 * off
assert td < 2 * off
assert not td >= 2 * off
assert not td > 2 * off

def test_comparison_object_array(self):
# analogous to GH#15183
td = Timedelta("2 days")
other = Timedelta("3 hours")

arr = np.array([other, td], dtype=object)
res = arr == td
expected = np.array([False, True], dtype=bool)
assert (res == expected).all()

# 2D case
arr = np.array([[other, td], [td, other]], dtype=object)
res = arr != td
expected = np.array([[True, False], [False, True]], dtype=bool)
assert res.shape == expected.shape
assert (res == expected).all()

def test_compare_timedelta_ndarray(self):
# GH#11835
periods = [Timedelta("0 days 01:00:00"), Timedelta("0 days 01:00:00")]
arr = np.array(periods)
result = arr[0] > arr
expected = np.array([False, False])
tm.assert_numpy_array_equal(result, expected)

@pytest.mark.skip(reason="GH#20829 is reverted until after 0.24.0")
def test_compare_custom_object(self):
"""
Make sure non supported operations on Timedelta returns NonImplemented
and yields to other operand (GH#20829).
"""

class CustomClass:
def __init__(self, cmp_result=None):
self.cmp_result = cmp_result

def generic_result(self):
if self.cmp_result is None:
return NotImplemented
else:
return self.cmp_result

def __eq__(self, other):
return self.generic_result()

def __gt__(self, other):
return self.generic_result()

t = Timedelta("1s")

assert not (t == "string")
assert not (t == 1)
assert not (t == CustomClass())
assert not (t == CustomClass(cmp_result=False))

assert t < CustomClass(cmp_result=True)
assert not (t < CustomClass(cmp_result=False))

assert t == CustomClass(cmp_result=True)

@pytest.mark.parametrize("val", ["string", 1])
def test_compare_unknown_type(self, val):
# GH#20829
t = Timedelta("1s")
with pytest.raises(TypeError):
t >= val
with pytest.raises(TypeError):
t > val
with pytest.raises(TypeError):
t <= val
with pytest.raises(TypeError):
t < val


def test_ops_notimplemented():
class Other:
pass

other = Other()

td = Timedelta("1 day")
assert td.__add__(other) is NotImplemented
assert td.__sub__(other) is NotImplemented
assert td.__truediv__(other) is NotImplemented
assert td.__mul__(other) is NotImplemented
assert td.__floordiv__(other) is NotImplemented


def test_ops_error_str():
# GH#13624
td = Timedelta("1 day")

for left, right in [(td, "a"), ("a", td)]:

with pytest.raises(TypeError):
left + right

with pytest.raises(TypeError):
left > right

assert not left == right
assert left != right
Loading