|
6 | 6 | from pandas.errors import OutOfBoundsDatetime
|
7 | 7 |
|
8 | 8 | from pandas import Timedelta, Timestamp
|
| 9 | +import pandas._testing as tm |
9 | 10 |
|
10 | 11 | from pandas.tseries import offsets
|
11 | 12 | from pandas.tseries.frequencies import to_offset
|
@@ -177,29 +178,6 @@ def test_timestamp_add_timedelta64_unit(self, other, expected_difference):
|
177 | 178 | valdiff = result.value - ts.value
|
178 | 179 | assert valdiff == expected_difference
|
179 | 180 |
|
180 |
| - @pytest.mark.parametrize("ts", [Timestamp.now(), Timestamp.now("utc")]) |
181 |
| - @pytest.mark.parametrize( |
182 |
| - "other", |
183 |
| - [ |
184 |
| - 1, |
185 |
| - np.int64(1), |
186 |
| - np.array([1, 2], dtype=np.int32), |
187 |
| - np.array([3, 4], dtype=np.uint64), |
188 |
| - ], |
189 |
| - ) |
190 |
| - def test_add_int_no_freq_raises(self, ts, other): |
191 |
| - msg = "Addition/subtraction of integers and integer-arrays" |
192 |
| - with pytest.raises(TypeError, match=msg): |
193 |
| - ts + other |
194 |
| - with pytest.raises(TypeError, match=msg): |
195 |
| - other + ts |
196 |
| - |
197 |
| - with pytest.raises(TypeError, match=msg): |
198 |
| - ts - other |
199 |
| - msg = "unsupported operand type" |
200 |
| - with pytest.raises(TypeError, match=msg): |
201 |
| - other - ts |
202 |
| - |
203 | 181 | @pytest.mark.parametrize(
|
204 | 182 | "ts",
|
205 | 183 | [
|
@@ -229,3 +207,52 @@ def test_add_int_with_freq(self, ts, other):
|
229 | 207 | msg = "unsupported operand type"
|
230 | 208 | with pytest.raises(TypeError, match=msg):
|
231 | 209 | other - ts
|
| 210 | + |
| 211 | + @pytest.mark.parametrize("shape", [(6,), (2, 3,)]) |
| 212 | + def test_addsub_m8ndarray(self, shape): |
| 213 | + # GH#33296 |
| 214 | + ts = Timestamp("2020-04-04 15:45") |
| 215 | + other = np.arange(6).astype("m8[h]").reshape(shape) |
| 216 | + |
| 217 | + result = ts + other |
| 218 | + |
| 219 | + ex_stamps = [ts + Timedelta(hours=n) for n in range(6)] |
| 220 | + expected = np.array([x.asm8 for x in ex_stamps], dtype="M8[ns]").reshape(shape) |
| 221 | + tm.assert_numpy_array_equal(result, expected) |
| 222 | + |
| 223 | + result = other + ts |
| 224 | + tm.assert_numpy_array_equal(result, expected) |
| 225 | + |
| 226 | + result = ts - other |
| 227 | + ex_stamps = [ts - Timedelta(hours=n) for n in range(6)] |
| 228 | + expected = np.array([x.asm8 for x in ex_stamps], dtype="M8[ns]").reshape(shape) |
| 229 | + tm.assert_numpy_array_equal(result, expected) |
| 230 | + |
| 231 | + msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timestamp'" |
| 232 | + with pytest.raises(TypeError, match=msg): |
| 233 | + other - ts |
| 234 | + |
| 235 | + @pytest.mark.parametrize("shape", [(6,), (2, 3,)]) |
| 236 | + def test_addsub_m8ndarray_tzaware(self, shape): |
| 237 | + # GH#33296 |
| 238 | + ts = Timestamp("2020-04-04 15:45", tz="US/Pacific") |
| 239 | + |
| 240 | + other = np.arange(6).astype("m8[h]").reshape(shape) |
| 241 | + |
| 242 | + result = ts + other |
| 243 | + |
| 244 | + ex_stamps = [ts + Timedelta(hours=n) for n in range(6)] |
| 245 | + expected = np.array(ex_stamps).reshape(shape) |
| 246 | + tm.assert_numpy_array_equal(result, expected) |
| 247 | + |
| 248 | + result = other + ts |
| 249 | + tm.assert_numpy_array_equal(result, expected) |
| 250 | + |
| 251 | + result = ts - other |
| 252 | + ex_stamps = [ts - Timedelta(hours=n) for n in range(6)] |
| 253 | + expected = np.array(ex_stamps).reshape(shape) |
| 254 | + tm.assert_numpy_array_equal(result, expected) |
| 255 | + |
| 256 | + msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timestamp'" |
| 257 | + with pytest.raises(TypeError, match=msg): |
| 258 | + other - ts |
0 commit comments