Skip to content
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

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

Merged
merged 17 commits into from
Dec 31, 2017
Merged
Show file tree
Hide file tree
Changes from 14 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
8 changes: 8 additions & 0 deletions doc/source/timedeltas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ yields another ``timedelta64[ns]`` dtypes Series.
td * -1
td * pd.Series([1, 2, 3, 4])

Rounded division (floor-division) of a ``timedelta64[ns]`` Series by a scalar
``Timedelta`` gives a series of integers.

.. ipython:: python

td // pd.Timedelta(days=3, hours=4)
pd.Timedelta(days=3, hours=4) // td

Attributes
----------

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ Conversion
- Bug in :class:`WeekOfMonth` and class:`Week` where addition and subtraction did not roll correctly (:issue:`18510`,:issue:`18672`,:issue:`18864`)
- Bug in :meth:`DatetimeIndex.astype` when converting between timezone aware dtypes, and converting from timezone aware to naive (:issue:`18951`)
- Bug in :class:`FY5253` where ``datetime`` addition and subtraction incremented incorrectly for dates on the year-end but not normalized to midnight (:issue:`18854`)
- Bug in :class:`Series` floor-division where operating on a scalar ``timedelta`` raises an exception (:issue:`18846`)


Indexing
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def _validate_timedelta(self, name):
# 2 timedeltas
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 @@ -629,7 +629,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
41 changes: 34 additions & 7 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,7 @@ def test_operators_timedelta64_with_timedelta(self, scalar_td):

@pytest.mark.parametrize('scalar_td', [
timedelta(minutes=5, seconds=4),
pytest.param(Timedelta('5m4s'),
marks=pytest.mark.xfail(reason="Timedelta.__floordiv__ "
"bug GH#18846")),
Timedelta('5m4s'),
Timedelta('5m4s').to_timedelta64()])
def test_operators_timedelta64_with_timedelta_invalid(self, scalar_td):
td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
Expand All @@ -993,15 +991,44 @@ def test_operators_timedelta64_with_timedelta_invalid(self, scalar_td):
td1 * scalar_td
with tm.assert_raises_regex(TypeError, pattern):
scalar_td * td1
with tm.assert_raises_regex(TypeError, pattern):
td1 // scalar_td
with tm.assert_raises_regex(TypeError, pattern):
scalar_td // td1
with tm.assert_raises_regex(TypeError, pattern):
scalar_td ** td1
with tm.assert_raises_regex(TypeError, pattern):
td1 ** scalar_td

@pytest.mark.parametrize('scalar_td', [
timedelta(minutes=5, seconds=4),
pytest.param(Timedelta('5m4s'),
marks=pytest.mark.xfail(reason="Timedelta.__floordiv__ "
Copy link
Contributor

Choose a reason for hiding this comment

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

floordiv -> rfloordiv

Copy link
Member Author

Choose a reason for hiding this comment

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

? Both Timedelta.__floordiv__ and Timedelta.__rfloordiv__ are wonky. Or are you referring to something else?

Copy link
Contributor

Choose a reason for hiding this comment

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

you reason is wrong, it should be rfloordiv

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean the reason="Timedelta.__floordiv__...? The test fails because Timedelta.__floordiv__ gets called, not Timedelta.__rfloordiv__.

"bug GH#18846")),
Timedelta('5m4s').to_timedelta64()])
def test_timedelta_rfloordiv(self, scalar_td):
# GH#18831
td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
td1.iloc[2] = np.nan
result = scalar_td // td1
expected = Series([1, 1, np.nan])
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize('scalar_td', [
timedelta(minutes=5, seconds=4),
Timedelta('5m4s'),
Timedelta('5m4s').to_timedelta64()])
def test_timedelta_floordiv(self, scalar_td):
# GH#18831
td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
td1.iloc[2] = np.nan

result = td1 // scalar_td
expected = Series([0, 0, np.nan])
tm.assert_series_equal(result, expected)

# We can test __rfloordiv__ using this syntax,
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be in the other test? (as the other one is rfloordiv), but this seems to work?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's kind of a man without a country regardless. I didn't want to put it in the rfloordiv test because that was xfailed so putting it there would be akin to not running it at all. But you're right it doesn't really belong in the floordiv test either. I'm open to suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

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

then make another test for it

# see `test_timedelta_rfloordiv`
result = td1.__rfloordiv__(scalar_td)
expected = Series([1, 1, np.nan])
tm.assert_series_equal(result, expected)


class TestDatetimeSeriesArithmetic(object):
@pytest.mark.parametrize(
Expand Down