Skip to content

check for datetime+period addition #18524

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
Nov 29, 2017
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/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,5 @@ Other

- Improved error message when attempting to use a Python keyword as an identifier in a numexpr query (:issue:`18221`)
- Fixed a bug where creating a Series from an array that contains both tz-naive and tz-aware values will result in a Series whose dtype is tz-aware instead of object (:issue:`16406`)
- Adding a ``Period`` object to a ``datetime`` or ``Timestamp`` object will now correctly raise a ``TypeError`` (:issue:`17983`)
-
14 changes: 14 additions & 0 deletions pandas/_libs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ from pandas.compat import PY2

cimport cython

from cpython.datetime cimport PyDateTime_Check, PyDateTime_IMPORT
# import datetime C API
PyDateTime_IMPORT

from tslibs.np_datetime cimport (pandas_datetimestruct,
dtstruct_to_dt64, dt64_to_dtstruct,
is_leapyear)
Expand Down Expand Up @@ -647,9 +651,19 @@ cdef class _Period(object):
elif util.is_integer_object(other):
ordinal = self.ordinal + other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
elif (PyDateTime_Check(other) or
is_period_object(other) or util.is_datetime64_object(other)):
# can't add datetime-like
# GH#17983
sname = type(self).__name__
oname = type(other).__name__
raise TypeError("unsupported operand type(s) for +: '{self}' "
"and '{other}'".format(self=sname,
other=oname))
else: # pragma: no cover
return NotImplemented
elif is_period_object(other):
# this can be reached via __radd__ because of cython rules
return other + self
else:
return NotImplemented
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/scalar/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,29 @@ def test_add_raises(self):
with tm.assert_raises_regex(TypeError, msg):
dt1 + dt2

boxes = [lambda x: x, lambda x: pd.Series([x]), lambda x: pd.Index([x])]

@pytest.mark.parametrize('lbox', boxes)
@pytest.mark.parametrize('rbox', boxes)
def test_add_timestamp_raises(self, rbox, lbox):
# GH # 17983
ts = pd.Timestamp('2017')
per = pd.Period('2017', freq='M')

# We may get a different message depending on which class raises
# the error.
msg = (r"cannot add|unsupported operand|"
r"can only operate on a|incompatible type|"
r"ufunc add cannot use operands")
with tm.assert_raises_regex(TypeError, msg):
lbox(ts) + rbox(per)

with tm.assert_raises_regex(TypeError, msg):
lbox(per) + rbox(ts)

with tm.assert_raises_regex(TypeError, msg):
lbox(per) + rbox(per)

Copy link
Contributor

Choose a reason for hiding this comment

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

can you add similar tests for sub

Copy link
Member Author

Choose a reason for hiding this comment

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

OK for todo list? I'd like to keep this narrow, busy day ahead.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

def test_sub(self):
dt1 = Period('2011-01-01', freq='D')
dt2 = Period('2011-01-15', freq='D')
Expand Down