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 1 commit
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
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
18 changes: 18 additions & 0 deletions pandas/tests/scalar/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,24 @@ 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')

with pytest.raises(TypeError):
Copy link
Member

Choose a reason for hiding this comment

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

Let's make sure that the right error message is hit (use tm.assert_raises_regex to be safe, unclear ATM whether we're going to leverage the most recent pytest API).

lbox(ts) + rbox(per)

with pytest.raises(TypeError):
lbox(per) + rbox(ts)
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 a similar test with Series of periods & Timestamp; I think these are now in pandas/tests/series/test_timestamp.py IIRC (maybe make a test_period.py in series ok too).

Copy link
Member Author

Choose a reason for hiding this comment

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

woops, just pushed without addressing this. Do you mean a Series that mixed, contains both Periods and Timestamps?


with pytest.raises(TypeError):
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