Skip to content

Commit 30fb087

Browse files
jbrockmendelWillAyd
authored andcommitted
BUG: fix+test Timestamp with int array (#28161)
1 parent 2cd7888 commit 30fb087

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

pandas/_libs/tslibs/c_timestamp.pyx

+16
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ cdef class _Timestamp(datetime):
251251
result = result.normalize()
252252
return result
253253

254+
elif is_array(other):
255+
if other.dtype.kind in ['i', 'u']:
256+
maybe_integer_op_deprecated(self)
257+
if self.freq is None:
258+
raise ValueError("Cannot add integer-dtype array "
259+
"to Timestamp without freq.")
260+
return self.freq * other + self
261+
254262
# index/series like
255263
elif hasattr(other, '_typ'):
256264
return NotImplemented
@@ -268,6 +276,14 @@ cdef class _Timestamp(datetime):
268276
neg_other = -other
269277
return self + neg_other
270278

279+
elif is_array(other):
280+
if other.dtype.kind in ['i', 'u']:
281+
maybe_integer_op_deprecated(self)
282+
if self.freq is None:
283+
raise ValueError("Cannot subtract integer-dtype array "
284+
"from Timestamp without freq.")
285+
return self - self.freq * other
286+
271287
typ = getattr(other, '_typ', None)
272288
if typ is not None:
273289
return NotImplemented

pandas/tests/scalar/timestamp/test_arithmetic.py

+53
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,56 @@ def test_timestamp_add_timedelta64_unit(self, other, expected_difference):
151151
result = ts + other
152152
valdiff = result.value - ts.value
153153
assert valdiff == expected_difference
154+
155+
@pytest.mark.parametrize("ts", [Timestamp.now(), Timestamp.now("utc")])
156+
@pytest.mark.parametrize(
157+
"other",
158+
[
159+
1,
160+
np.int64(1),
161+
np.array([1, 2], dtype=np.int32),
162+
np.array([3, 4], dtype=np.uint64),
163+
],
164+
)
165+
def test_add_int_no_freq_raises(self, ts, other):
166+
with pytest.raises(ValueError, match="without freq"):
167+
ts + other
168+
with pytest.raises(ValueError, match="without freq"):
169+
other + ts
170+
171+
with pytest.raises(ValueError, match="without freq"):
172+
ts - other
173+
with pytest.raises(TypeError):
174+
other - ts
175+
176+
@pytest.mark.parametrize(
177+
"ts",
178+
[
179+
Timestamp("1776-07-04", freq="D"),
180+
Timestamp("1776-07-04", tz="UTC", freq="D"),
181+
],
182+
)
183+
@pytest.mark.parametrize(
184+
"other",
185+
[
186+
1,
187+
np.int64(1),
188+
np.array([1, 2], dtype=np.int32),
189+
np.array([3, 4], dtype=np.uint64),
190+
],
191+
)
192+
def test_add_int_with_freq(self, ts, other):
193+
with tm.assert_produces_warning(FutureWarning):
194+
result1 = ts + other
195+
with tm.assert_produces_warning(FutureWarning):
196+
result2 = other + ts
197+
198+
assert np.all(result1 == result2)
199+
200+
with tm.assert_produces_warning(FutureWarning):
201+
result = result1 - other
202+
203+
assert np.all(result == ts)
204+
205+
with pytest.raises(TypeError):
206+
other - ts

0 commit comments

Comments
 (0)