|
15 | 15 | class TestTimedeltaArithmetic(object):
|
16 | 16 | _multiprocess_can_split_ = True
|
17 | 17 |
|
| 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 | + |
18 | 40 | def test_to_timedelta_on_nanoseconds(self):
|
19 | 41 | # GH 9273
|
20 | 42 | result = Timedelta(nanoseconds=100)
|
@@ -93,38 +115,53 @@ def test_ops_offsets(self):
|
93 | 115 | assert Timedelta(239, unit='h') == td - pd.offsets.Hour(1)
|
94 | 116 | assert Timedelta(-239, unit='h') == pd.offsets.Hour(1) - td
|
95 | 117 |
|
96 |
| - # TODO: Split by op, better name |
97 |
| - def test_ops(self): |
| 118 | + def test_unary_ops(self): |
98 | 119 | td = Timedelta(10, unit='d')
|
| 120 | + |
| 121 | + # __neg__, __pos__ |
99 | 122 | assert -td == Timedelta(-10, unit='d')
|
| 123 | + assert -td == Timedelta('-10d') |
100 | 124 | 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 | + |
102 | 134 | assert (td - pd.NaT) is pd.NaT
|
103 |
| - assert td + td == Timedelta(20, unit='d') |
104 | 135 | assert (td + pd.NaT) is pd.NaT
|
105 |
| - assert td * 2 == Timedelta(20, unit='d') |
106 | 136 | 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 |
112 | 137 | assert (td / pd.NaT) is np.nan
|
113 | 138 | assert (td // pd.NaT) is np.nan
|
114 | 139 |
|
| 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 | + |
115 | 147 | # invert
|
116 |
| - assert -td == Timedelta('-10d') |
117 | 148 | assert td * -1 == Timedelta('-10d')
|
118 | 149 | 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) |
123 | 150 |
|
124 | 151 | # can't operate with integers
|
125 | 152 | pytest.raises(TypeError, lambda: td + 2)
|
126 | 153 | pytest.raises(TypeError, lambda: td - 2)
|
127 | 154 |
|
| 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 | + |
128 | 165 |
|
129 | 166 | class TestTimedeltas(object):
|
130 | 167 | _multiprocess_can_split_ = True
|
@@ -733,14 +770,6 @@ def test_timedelta_arithmetic(self):
|
733 | 770 | tm.assert_series_equal(result_operator, expected)
|
734 | 771 | tm.assert_series_equal(result_method, expected)
|
735 | 772 |
|
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 |
| - |
744 | 773 | def test_apply_to_timedelta(self):
|
745 | 774 | timedelta_NaT = pd.to_timedelta('NaT')
|
746 | 775 |
|
@@ -803,18 +832,3 @@ def test_isoformat(self):
|
803 | 832 | result = Timedelta(minutes=1).isoformat()
|
804 | 833 | expected = 'P0DT0H1M0S'
|
805 | 834 | 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