Skip to content

BUG: fix Series[timedelta64] arithmetic with Timedelta scalars #18831

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 17 commits into from
Dec 31, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
1eab96f
Fix Series timedelta64 // timedelta
jbrockmendel Dec 19, 2017
25071a8
allow rtruediv
jbrockmendel Dec 19, 2017
45c7260
amend test case with pytimedelta, timedelta64
jbrockmendel Dec 19, 2017
95cc5f9
dummy commit to force CI
jbrockmendel Dec 19, 2017
6ff2d5f
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
jbrockmendel Dec 19, 2017
4fe8f74
xfail test
jbrockmendel Dec 20, 2017
4821f05
floordiv example in timedeltas.rst, move whatsnew note to conversion …
jbrockmendel Dec 20, 2017
10054de
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
jbrockmendel Dec 21, 2017
66561ef
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
jbrockmendel Dec 23, 2017
347a221
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
jbrockmendel Dec 24, 2017
8789be7
edit issue reference
jbrockmendel Dec 24, 2017
c3795d0
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
jbrockmendel Dec 27, 2017
ef8d6e2
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
jbrockmendel Dec 28, 2017
2b3484f
re-implement tests lost in rebase/merge mixups
jbrockmendel Dec 28, 2017
3250913
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
jbrockmendel Dec 29, 2017
3a5c3b5
Merge branch 'master' of https://github.com/pandas-dev/pandas into ti…
jbrockmendel Dec 29, 2017
729d240
separate explicit rfloordiv test
jbrockmendel Dec 29, 2017
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 @@ -354,3 +354,4 @@ Other

- Improved error message when attempting to use a Python keyword as an identifier in a ``numexpr`` backed query (:issue:`18221`)
- Bug in :class:`Timestamp` where comparison with an array of ``Timestamp`` objects would result in a ``RecursionError`` (:issue:`15183`)
- Bug in :func:`Series.__floordiv__` and :func:`Series.__rfloordiv__` where operating on a scalar ``timedelta`` raises an exception (:issue:`18824`)
Copy link
Contributor

Choose a reason for hiding this comment

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

don't put things in Other. This is conversion. Also note that a floordiv of scalar / Series was incorrect before.

Copy link
Contributor

Choose a reason for hiding this comment

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

verify that we have a doc example in timedelta.rst as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also note that a floordiv of scalar / Series was incorrect before.

Something other than the mention of Series.__rfloordiv__?

6 changes: 4 additions & 2 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def _validate(self, lvalues, rvalues, name):

if name not in ('__div__', '__rdiv__', '__truediv__',
'__rtruediv__', '__add__', '__radd__', '__sub__',
'__rsub__'):
'__rsub__', '__floordiv__', '__rfloordiv__'):
raise TypeError("can only operate on a timedeltas for addition"
", subtraction, and division, but the operator"
" [{name}] was passed".format(name=name))
Expand Down Expand Up @@ -595,7 +595,9 @@ def _offset(lvalues, rvalues):
# integer gets converted to timedelta in np < 1.6
if ((self.is_timedelta_lhs and self.is_timedelta_rhs) and
not self.is_integer_rhs and not self.is_integer_lhs and
self.name in ('__div__', '__truediv__')):
self.name in ('__div__', '__rdiv__',
'__truediv__', '__rtruediv__',
'__floordiv__', '__rfloordiv__')):
self.dtype = 'float64'
self.fill_value = np.nan
lvalues = lvalues.astype(np.float64)
Expand Down
31 changes: 21 additions & 10 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,16 +976,27 @@ def run_ops(ops, get_ser, test_ser):
# ## timedelta64 ###
td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
td1.iloc[2] = np.nan
td2 = timedelta(minutes=5, seconds=4)
ops = ['__mul__', '__floordiv__', '__pow__', '__rmul__',
'__rfloordiv__', '__rpow__']
run_ops(ops, td1, td2)
td1 + td2
td2 + td1
td1 - td2
td2 - td1
td1 / td2
td2 / td1
tdscalar = Timedelta(minutes=5, seconds=4)
ops = ['__mul__', '__pow__', '__rmul__', '__rpow__']
run_ops(ops, td1, tdscalar)
td1 + tdscalar
tdscalar + td1
td1 - tdscalar
tdscalar - td1
td1 / tdscalar
tdscalar / td1
tm.assert_series_equal(td1 // tdscalar, Series([0, 0, np.nan]))
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 parametrize these last ones they seem pretty similar

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 part of a 127 line test that is all over the place. I'd prefer to clean it up in a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, this needs parameterization / refactor, but can follow up.

tm.assert_series_equal(td1 // tdscalar.to_pytimedelta(),
Series([0, 0, np.nan]))
tm.assert_series_equal(td1 // tdscalar.to_timedelta64(),
Series([0, 0, np.nan]))
# TODO: the Timedelta // td1 fails because of a bug
# in Timedelta.__floordiv__, see GH#18824
# tm.assert_series_equal(tdscalar // td1, Series([1, 1, np.nan]))
Copy link
Member

Choose a reason for hiding this comment

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

I would consider breaking out into a separate test so we can xfail it separately.

Copy link
Member Author

Choose a reason for hiding this comment

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

Works for me. I've got a fix ready pending feedback on #18846.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes would clean up these tests here (IIRC you broke these out by operator elsewhere)

tm.assert_series_equal(tdscalar.to_pytimedelta() // td1,
Series([1, 1, np.nan]))
tm.assert_series_equal(tdscalar.to_timedelta64() // td1,
Series([1, 1, np.nan]))

# ## datetime64 ###
dt1 = Series([Timestamp('20111230'), Timestamp('20120101'),
Expand Down