Skip to content

BUG: fix+test Timestamp with int array #28161

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 2 commits into from
Sep 2, 2019
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
16 changes: 16 additions & 0 deletions pandas/_libs/tslibs/c_timestamp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ cdef class _Timestamp(datetime):
result = result.normalize()
return result

elif is_array(other):
if other.dtype.kind in ['i', 'u']:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just use is_integer_dtype here instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the pattern we use in the cython code

maybe_integer_op_deprecated(self)
if self.freq is None:
raise ValueError("Cannot add integer-dtype array "
"to Timestamp without freq.")
return self.freq * other + self

# index/series like
elif hasattr(other, '_typ'):
return NotImplemented
Expand All @@ -268,6 +276,14 @@ cdef class _Timestamp(datetime):
neg_other = -other
return self + neg_other

elif is_array(other):
if other.dtype.kind in ['i', 'u']:
maybe_integer_op_deprecated(self)
if self.freq is None:
raise ValueError("Cannot subtract integer-dtype array "
"from Timestamp without freq.")
return self - self.freq * other

typ = getattr(other, '_typ', None)

# a Timestamp-DatetimeIndex -> yields a negative TimedeltaIndex
Expand Down
53 changes: 53 additions & 0 deletions pandas/tests/scalar/timestamp/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,56 @@ def test_timestamp_add_timedelta64_unit(self, other, expected_difference):
result = ts + other
valdiff = result.value - ts.value
assert valdiff == expected_difference

@pytest.mark.parametrize("ts", [Timestamp.now(), Timestamp.now("utc")])
@pytest.mark.parametrize(
"other",
[
1,
np.int64(1),
np.array([1, 2], dtype=np.int32),
np.array([3, 4], dtype=np.uint64),
],
)
def test_add_int_no_freq_raises(self, ts, other):
with pytest.raises(ValueError, match="without freq"):
ts + other
with pytest.raises(ValueError, match="without freq"):
other + ts

with pytest.raises(ValueError, match="without freq"):
ts - other
with pytest.raises(TypeError):
other - ts

@pytest.mark.parametrize(
"ts",
[
Timestamp("1776-07-04", freq="D"),
Timestamp("1776-07-04", tz="UTC", freq="D"),
],
)
@pytest.mark.parametrize(
"other",
[
1,
np.int64(1),
np.array([1, 2], dtype=np.int32),
np.array([3, 4], dtype=np.uint64),
],
)
def test_add_int_with_freq(self, ts, other):
with tm.assert_produces_warning(FutureWarning):
result1 = ts + other
with tm.assert_produces_warning(FutureWarning):
result2 = other + ts

assert np.all(result1 == result2)

with tm.assert_produces_warning(FutureWarning):
result = result1 - other

assert np.all(result == ts)

with pytest.raises(TypeError):
other - ts