Skip to content

BUG: datetime64 - Timestamp incorrectly raising TypeError #28286

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 4 commits into from
Sep 7, 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Datetimelike
- Bug in :meth:`Series.dt` property lookups when the underlying data is read-only (:issue:`27529`)
- Bug in ``HDFStore.__getitem__`` incorrectly reading tz attribute created in Python 2 (:issue:`26443`)
- Bug in :meth:`pandas.core.groupby.SeriesGroupBy.nunique` where ``NaT`` values were interfering with the count of unique values (:issue:`27951`)
- Bug in :class:`Timestamp` subtraction when subtracting a :class:`Timestamp` from a ``np.datetime64`` object incorrectly raising ``TypeError`` (:issue:`28286`)
-


Timedelta
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/tslibs/c_timestamp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ cdef class _Timestamp(datetime):
except (OverflowError, OutOfBoundsDatetime):
pass

elif is_datetime64_object(self):
# GH#28286 cython semantics for __rsub__, `other` is actually
# the Timestamp
return type(other)(self) - other

# scalar Timestamp/datetime - Timedelta -> yields a Timestamp (with
# same timezone if specified)
return datetime.__sub__(self, other)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/scalar/timestamp/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ def test_delta_preserve_nanos(self):
result = val + timedelta(1)
assert result.nanosecond == val.nanosecond

def test_rsub_dtscalars(self, tz_naive_fixture):
# In particular, check that datetime64 - Timestamp works GH#28286
td = Timedelta(1235345642000)
ts = Timestamp.now(tz_naive_fixture)
other = ts + td

assert other - ts == td
assert other.to_pydatetime() - ts == td
Copy link
Member

Choose a reason for hiding this comment

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

Is anything above this line not tested elsewhere? Just was a little counter intuitive to read as I thought this PR was only about datetime64 obects

Copy link
Member Author

Choose a reason for hiding this comment

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

trying to follow the pattern in these tests of testing all datetime-like scalars in the same place

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense

if tz_naive_fixture is None:
Copy link
Member

Choose a reason for hiding this comment

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

What should happen if this is not None? Should there be a test for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

should raise bc of tzawareness compat, will update test to include that

assert other.to_datetime64() - ts == td
else:
with pytest.raises(TypeError, match="subtraction must have"):
other.to_datetime64() - ts

def test_timestamp_sub_datetime(self):
dt = datetime(2013, 10, 12)
ts = Timestamp(datetime(2013, 10, 13))
Expand Down