Skip to content

BUG: Timestamp+- ndarray[td64] #33296

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
Apr 6, 2020
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ Datetimelike
- :class:`Timestamp` raising confusing error message when year, month or day is missing (:issue:`31200`)
- Bug in :class:`DatetimeIndex` constructor incorrectly accepting ``bool``-dtyped inputs (:issue:`32668`)
- Bug in :meth:`DatetimeIndex.searchsorted` not accepting a ``list`` or :class:`Series` as its argument (:issue:`32762`)
- Bug in :class:`Timestamp` arithmetic when adding or subtracting a ``np.ndarray`` with ``timedelta64`` dtype (:issue:`33296`)

Timedelta
^^^^^^^^^
Expand Down
14 changes: 14 additions & 0 deletions pandas/_libs/tslibs/c_timestamp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ cdef class _Timestamp(datetime):
elif is_array(other):
if other.dtype.kind in ['i', 'u']:
raise integer_op_not_supported(self)
if other.dtype.kind == "m":
if self.tz is None:
return self.asm8 + other
return np.asarray(
[self + other[n] for n in range(len(other))],
dtype=object,
)

# index/series like
elif hasattr(other, '_typ'):
Expand All @@ -275,6 +282,13 @@ cdef class _Timestamp(datetime):
elif is_array(other):
if other.dtype.kind in ['i', 'u']:
raise integer_op_not_supported(self)
if other.dtype.kind == "m":
if self.tz is None:
return self.asm8 - other
return np.asarray(
[self - other[n] for n in range(len(other))],
dtype=object,
)

typ = getattr(other, '_typ', None)
if typ is not None:
Expand Down
73 changes: 50 additions & 23 deletions pandas/tests/scalar/timestamp/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pandas.errors import OutOfBoundsDatetime

from pandas import Timedelta, Timestamp
import pandas._testing as tm

from pandas.tseries import offsets
from pandas.tseries.frequencies import to_offset
Expand Down Expand Up @@ -177,29 +178,6 @@ def test_timestamp_add_timedelta64_unit(self, other, expected_difference):
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):
msg = "Addition/subtraction of integers and integer-arrays"
with pytest.raises(TypeError, match=msg):
ts + other
with pytest.raises(TypeError, match=msg):
other + ts

with pytest.raises(TypeError, match=msg):
ts - other
msg = "unsupported operand type"
with pytest.raises(TypeError, match=msg):
other - ts

@pytest.mark.parametrize(
"ts",
[
Expand Down Expand Up @@ -229,3 +207,52 @@ def test_add_int_with_freq(self, ts, other):
msg = "unsupported operand type"
with pytest.raises(TypeError, match=msg):
other - ts

@pytest.mark.parametrize("shape", [(6,), (2, 3,)])
def test_addsub_m8ndarray(self, shape):
# GH#33296
ts = Timestamp("2020-04-04 15:45")
other = np.arange(6).astype("m8[h]").reshape(shape)

result = ts + other

ex_stamps = [ts + Timedelta(hours=n) for n in range(6)]
expected = np.array([x.asm8 for x in ex_stamps], dtype="M8[ns]").reshape(shape)
tm.assert_numpy_array_equal(result, expected)

result = other + ts
tm.assert_numpy_array_equal(result, expected)

result = ts - other
ex_stamps = [ts - Timedelta(hours=n) for n in range(6)]
expected = np.array([x.asm8 for x in ex_stamps], dtype="M8[ns]").reshape(shape)
tm.assert_numpy_array_equal(result, expected)

msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timestamp'"
with pytest.raises(TypeError, match=msg):
other - ts

@pytest.mark.parametrize("shape", [(6,), (2, 3,)])
def test_addsub_m8ndarray_tzaware(self, shape):
# GH#33296
ts = Timestamp("2020-04-04 15:45", tz="US/Pacific")

other = np.arange(6).astype("m8[h]").reshape(shape)

result = ts + other

ex_stamps = [ts + Timedelta(hours=n) for n in range(6)]
expected = np.array(ex_stamps).reshape(shape)
tm.assert_numpy_array_equal(result, expected)

result = other + ts
tm.assert_numpy_array_equal(result, expected)

result = ts - other
ex_stamps = [ts - Timedelta(hours=n) for n in range(6)]
expected = np.array(ex_stamps).reshape(shape)
tm.assert_numpy_array_equal(result, expected)

msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timestamp'"
with pytest.raises(TypeError, match=msg):
other - ts