Skip to content

Commit f6c0f8a

Browse files
jbrockmendeljreback
authored andcommitted
Fix and test TimedeltaIndex.__rfloordiv__ bug (pandas-dev#19125)
1 parent 3314ab1 commit f6c0f8a

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

doc/source/whatsnew/v0.23.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ Conversion
376376
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`)
377377
- Fixed bug where comparing :class:`DatetimeIndex` failed to raise ``TypeError`` when attempting to compare timezone-aware and timezone-naive datetimelike objects (:issue:`18162`)
378378
- 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`)
379-
379+
- Bug where dividing a scalar timedelta-like object with :class:`TimedeltaIndex` performed the reciprocal operation (:issue:`19125`)
380+
-
380381

381382
Indexing
382383
^^^^^^^^

pandas/core/indexes/base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3861,7 +3861,7 @@ def dropna(self, how='any'):
38613861
return self._shallow_copy(self.values[~self._isnan])
38623862
return self._shallow_copy()
38633863

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

38673867
def _evaluate_with_datetime_like(self, other, op, opstr):
@@ -4025,7 +4025,8 @@ def _evaluate_numeric_binop(self, other):
40254025
# handle time-based others
40264026
if isinstance(other, (ABCDateOffset, np.timedelta64,
40274027
Timedelta, datetime.timedelta)):
4028-
return self._evaluate_with_timedelta_like(other, op, opstr)
4028+
return self._evaluate_with_timedelta_like(other, op, opstr,
4029+
reversed)
40294030
elif isinstance(other, (Timestamp, np.datetime64)):
40304031
return self._evaluate_with_datetime_like(other, op, opstr)
40314032

pandas/core/indexes/timedeltas.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def _add_delta(self, delta):
372372
result = TimedeltaIndex(new_values, freq='infer', name=name)
373373
return result
374374

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

388388
i8 = self.asi8
389+
left, right = i8, other.value
390+
if reversed:
391+
left, right = right, left
392+
389393
if opstr in ['__floordiv__']:
390-
result = i8 // other.value
394+
result = left // right
391395
else:
392-
result = op(i8, float(other.value))
396+
result = op(left, float(right))
393397
result = self._maybe_mask_results(result, convert='float64')
394398
return Index(result, name=self.name, copy=False)
395399

@@ -972,6 +976,7 @@ def _is_convertible_to_index(other):
972976

973977

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

pandas/tests/indexes/timedeltas/test_arithmetic.py

+17
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,23 @@ def test_tdi_radd_timestamp(self):
286286

287287
# -------------------------------------------------------------
288288

289+
@pytest.mark.parametrize('scalar_td', [
290+
timedelta(minutes=10, seconds=7),
291+
Timedelta('10m7s'),
292+
Timedelta('10m7s').to_timedelta64()])
293+
def test_tdi_floordiv_timedelta_scalar(self, scalar_td):
294+
# GH#19125
295+
tdi = TimedeltaIndex(['00:05:03', '00:05:03', pd.NaT], freq=None)
296+
expected = pd.Index([2.0, 2.0, np.nan])
297+
298+
res = tdi.__rfloordiv__(scalar_td)
299+
tm.assert_index_equal(res, expected)
300+
301+
expected = pd.Index([0.0, 0.0, np.nan])
302+
303+
res = tdi // (scalar_td)
304+
tm.assert_index_equal(res, expected)
305+
289306
# TODO: Split by operation, better name
290307
def test_ops_compat(self):
291308

0 commit comments

Comments
 (0)