Skip to content

Fix and test TimedeltaIndex.__rfloordiv__ bug #19125

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
Jan 7, 2018
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ Conversion
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`)
- Fixed bug where comparing :class:`DatetimeIndex` failed to raise ``TypeError`` when attempting to compare timezone-aware and timezone-naive datetimelike objects (:issue:`18162`)
- Bug in :class:`DatetimeIndex` where the repr was not showing high-precision time values at the end of a day (e.g., 23:59:59.999999999) (:issue:`19030`)

- Bug where dividing a scalar timedelta-like object with :class:`TimedeltaIndex` performed the reciprocal operation (:issue:`19125`)
-

Indexing
^^^^^^^^
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3861,7 +3861,7 @@ def dropna(self, how='any'):
return self._shallow_copy(self.values[~self._isnan])
return self._shallow_copy()

def _evaluate_with_timedelta_like(self, other, op, opstr):
def _evaluate_with_timedelta_like(self, other, op, opstr, reversed=False):
raise TypeError("can only perform ops with timedelta like values")

def _evaluate_with_datetime_like(self, other, op, opstr):
Expand Down Expand Up @@ -4025,7 +4025,8 @@ def _evaluate_numeric_binop(self, other):
# handle time-based others
if isinstance(other, (ABCDateOffset, np.timedelta64,
Timedelta, datetime.timedelta)):
return self._evaluate_with_timedelta_like(other, op, opstr)
return self._evaluate_with_timedelta_like(other, op, opstr,
reversed)
elif isinstance(other, (Timestamp, np.datetime64)):
return self._evaluate_with_datetime_like(other, op, opstr)

Expand Down
11 changes: 8 additions & 3 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def _add_delta(self, delta):
result = TimedeltaIndex(new_values, freq='infer', name=name)
return result

def _evaluate_with_timedelta_like(self, other, op, opstr):
def _evaluate_with_timedelta_like(self, other, op, opstr, reversed=False):
if isinstance(other, ABCSeries):
# GH#19042
return NotImplemented
Expand All @@ -386,10 +386,14 @@ def _evaluate_with_timedelta_like(self, other, op, opstr):
"division by pd.NaT not implemented")

i8 = self.asi8
left, right = i8, other.value
if reversed:
left, right = right, left

if opstr in ['__floordiv__']:
result = i8 // other.value
result = left // right
else:
result = op(i8, float(other.value))
result = op(left, float(right))
result = self._maybe_mask_results(result, convert='float64')
return Index(result, name=self.name, copy=False)

Expand Down Expand Up @@ -972,6 +976,7 @@ def _is_convertible_to_index(other):


def _is_convertible_to_td(key):
# TODO: Not all DateOffset objects are convertible to Timedelta
return isinstance(key, (DateOffset, timedelta, Timedelta,
np.timedelta64, compat.string_types))

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexes/timedeltas/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,23 @@ def test_tdi_radd_timestamp(self):

# -------------------------------------------------------------

@pytest.mark.parametrize('scalar_td', [
timedelta(minutes=10, seconds=7),
Timedelta('10m7s'),
Timedelta('10m7s').to_timedelta64()])
def test_tdi_floordiv_timedelta_scalar(self, scalar_td):
# GH#19125
tdi = TimedeltaIndex(['00:05:03', '00:05:03', pd.NaT], freq=None)
expected = pd.Index([2.0, 2.0, np.nan])

res = tdi.__rfloordiv__(scalar_td)
tm.assert_index_equal(res, expected)

expected = pd.Index([0.0, 0.0, np.nan])

res = tdi // (scalar_td)
tm.assert_index_equal(res, expected)

# TODO: Split by operation, better name
def test_ops_compat(self):

Expand Down