Skip to content

Commit fe34b32

Browse files
jbrockmendeljreback
authored andcommitted
timestamp/timedelta test cleanup (#18619)
1 parent 5bf9486 commit fe34b32

File tree

2 files changed

+227
-211
lines changed

2 files changed

+227
-211
lines changed

pandas/tests/scalar/test_timedelta.py

+52-38
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@
1515
class TestTimedeltaArithmetic(object):
1616
_multiprocess_can_split_ = True
1717

18+
def test_arithmetic_overflow(self):
19+
with pytest.raises(OverflowError):
20+
pd.Timestamp('1700-01-01') + pd.Timedelta(13 * 19999, unit='D')
21+
22+
with pytest.raises(OverflowError):
23+
pd.Timestamp('1700-01-01') + timedelta(days=13 * 19999)
24+
25+
def test_ops_error_str(self):
26+
# GH 13624
27+
td = Timedelta('1 day')
28+
29+
for left, right in [(td, 'a'), ('a', td)]:
30+
31+
with pytest.raises(TypeError):
32+
left + right
33+
34+
with pytest.raises(TypeError):
35+
left > right
36+
37+
assert not left == right
38+
assert left != right
39+
1840
def test_to_timedelta_on_nanoseconds(self):
1941
# GH 9273
2042
result = Timedelta(nanoseconds=100)
@@ -93,38 +115,53 @@ def test_ops_offsets(self):
93115
assert Timedelta(239, unit='h') == td - pd.offsets.Hour(1)
94116
assert Timedelta(-239, unit='h') == pd.offsets.Hour(1) - td
95117

96-
# TODO: Split by op, better name
97-
def test_ops(self):
118+
def test_unary_ops(self):
98119
td = Timedelta(10, unit='d')
120+
121+
# __neg__, __pos__
99122
assert -td == Timedelta(-10, unit='d')
123+
assert -td == Timedelta('-10d')
100124
assert +td == Timedelta(10, unit='d')
101-
assert td - td == Timedelta(0, unit='ns')
125+
126+
# __abs__, __abs__(__neg__)
127+
assert abs(td) == td
128+
assert abs(-td) == td
129+
assert abs(-td) == Timedelta('10d')
130+
131+
def test_binary_ops_nat(self):
132+
td = Timedelta(10, unit='d')
133+
102134
assert (td - pd.NaT) is pd.NaT
103-
assert td + td == Timedelta(20, unit='d')
104135
assert (td + pd.NaT) is pd.NaT
105-
assert td * 2 == Timedelta(20, unit='d')
106136
assert (td * pd.NaT) is pd.NaT
107-
assert td / 2 == Timedelta(5, unit='d')
108-
assert td // 2 == Timedelta(5, unit='d')
109-
assert abs(td) == td
110-
assert abs(-td) == td
111-
assert td / td == 1
112137
assert (td / pd.NaT) is np.nan
113138
assert (td // pd.NaT) is np.nan
114139

140+
def test_binary_ops_integers(self):
141+
td = Timedelta(10, unit='d')
142+
143+
assert td * 2 == Timedelta(20, unit='d')
144+
assert td / 2 == Timedelta(5, unit='d')
145+
assert td // 2 == Timedelta(5, unit='d')
146+
115147
# invert
116-
assert -td == Timedelta('-10d')
117148
assert td * -1 == Timedelta('-10d')
118149
assert -1 * td == Timedelta('-10d')
119-
assert abs(-td) == Timedelta('10d')
120-
121-
# invalid multiply with another timedelta
122-
pytest.raises(TypeError, lambda: td * td)
123150

124151
# can't operate with integers
125152
pytest.raises(TypeError, lambda: td + 2)
126153
pytest.raises(TypeError, lambda: td - 2)
127154

155+
def test_binary_ops_with_timedelta(self):
156+
td = Timedelta(10, unit='d')
157+
158+
assert td - td == Timedelta(0, unit='ns')
159+
assert td + td == Timedelta(20, unit='d')
160+
assert td / td == 1
161+
162+
# invalid multiply with another timedelta
163+
pytest.raises(TypeError, lambda: td * td)
164+
128165

129166
class TestTimedeltas(object):
130167
_multiprocess_can_split_ = True
@@ -733,14 +770,6 @@ def test_timedelta_arithmetic(self):
733770
tm.assert_series_equal(result_operator, expected)
734771
tm.assert_series_equal(result_method, expected)
735772

736-
def test_arithmetic_overflow(self):
737-
738-
with pytest.raises(OverflowError):
739-
pd.Timestamp('1700-01-01') + pd.Timedelta(13 * 19999, unit='D')
740-
741-
with pytest.raises(OverflowError):
742-
pd.Timestamp('1700-01-01') + timedelta(days=13 * 19999)
743-
744773
def test_apply_to_timedelta(self):
745774
timedelta_NaT = pd.to_timedelta('NaT')
746775

@@ -803,18 +832,3 @@ def test_isoformat(self):
803832
result = Timedelta(minutes=1).isoformat()
804833
expected = 'P0DT0H1M0S'
805834
assert result == expected
806-
807-
def test_ops_error_str(self):
808-
# GH 13624
809-
td = Timedelta('1 day')
810-
811-
for l, r in [(td, 'a'), ('a', td)]:
812-
813-
with pytest.raises(TypeError):
814-
l + r
815-
816-
with pytest.raises(TypeError):
817-
l > r
818-
819-
assert not l == r
820-
assert l != r

0 commit comments

Comments
 (0)