Skip to content

Commit fb0a647

Browse files
committed
Fix timedelta scalars
1 parent fe98d72 commit fb0a647

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

pandas/core/indexes/datetimelike.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,7 @@ def asobject(self):
433433
return Index(self._box_values(self.asi8), name=self.name, dtype=object)
434434

435435
def _convert_tolerance(self, tolerance):
436-
if not is_list_like(tolerance):
437-
tolerance = [tolerance]
438-
return np.asarray(to_timedelta(tolerance))
436+
return np.asarray(to_timedelta(tolerance, box=False))
439437

440438
def _maybe_mask_results(self, result, fill_value=None, convert=None):
441439
"""

pandas/core/indexes/period.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,10 @@ def _maybe_convert_timedelta(self, other):
637637
other, (timedelta, np.timedelta64, offsets.Tick, np.ndarray)):
638638
offset = frequencies.to_offset(self.freq.rule_code)
639639
if isinstance(offset, offsets.Tick):
640-
nanos = tslib._delta_to_nanoseconds(other)
640+
if isinstance(other, np.ndarray):
641+
nanos = np.vectorize(tslib._delta_to_nanoseconds)(other)
642+
else:
643+
nanos = tslib._delta_to_nanoseconds(other)
641644
offset_nanos = tslib._delta_to_nanoseconds(offset)
642645
check = np.all(nanos % offset_nanos == 0)
643646
if check:

pandas/core/tools/timedeltas.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'):
8383
elif isinstance(arg, ABCIndexClass):
8484
return _convert_listlike(arg, unit=unit, box=box,
8585
errors=errors, name=arg.name)
86-
elif is_list_like(arg) and getattr(arg, 'ndim', 1) == 1:
87-
return _convert_listlike(arg, unit=unit, box=box, errors=errors)
86+
elif is_list_like(arg) and getattr(arg, 'ndim', 1) <= 1:
87+
if getattr(arg, 'ndim', 1) == 0:
88+
# extract array scalar and process below
89+
arg = arg[()]
90+
else:
91+
return _convert_listlike(arg, unit=unit, box=box, errors=errors)
8892
elif getattr(arg, 'ndim', 1) > 1:
8993
raise TypeError('arg must be a string, timedelta, list, tuple, '
9094
'1-d array, or Series')

pandas/tests/indexes/datetimes/test_datetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_get_loc(self):
4242
assert idx.get_loc('2000-01-01T12', method='nearest',
4343
tolerance=timedelta(1)) == 1
4444
with tm.assert_raises_regex(ValueError,
45-
'unit abbreviation w/o a number'):
45+
'unit abbreviation w/o a number'):
4646
idx.get_loc('2000-01-01T12', method='nearest', tolerance='foo')
4747
with pytest.raises(KeyError):
4848
idx.get_loc('2000-01-01T03', method='nearest', tolerance='2 hours')

pandas/tests/indexes/period/test_period.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_get_loc(self):
8585
assert idx.get_loc('2000-01-02T12', method='nearest',
8686
tolerance=timedelta(1)) == 1
8787
with tm.assert_raises_regex(ValueError,
88-
'unit abbreviation w/o a number'):
88+
'unit abbreviation w/o a number'):
8989
idx.get_loc('2000-01-10', method='nearest', tolerance='foo')
9090

9191
msg = 'Input has different freq from PeriodIndex\\(freq=D\\)'
@@ -177,8 +177,8 @@ def test_get_indexer(self):
177177
pd.Timedelta('1 hour').to_timedelta64(),
178178
np.timedelta64(1, 'M'), ]
179179
with pytest.raises(
180-
IncompatibleFrequency,
181-
match='Input has different freq from'):
180+
IncompatibleFrequency,
181+
match='Input has different freq from'):
182182
idx.get_indexer(target, 'nearest', tolerance=tol_bad)
183183

184184
def test_repeat(self):

pandas/tests/indexes/timedeltas/test_timedelta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_get_loc(self):
6161
tolerance=timedelta(0)) == 1
6262

6363
with tm.assert_raises_regex(ValueError,
64-
'unit abbreviation w/o a number'):
64+
'unit abbreviation w/o a number'):
6565
idx.get_loc(idx[1], method='nearest', tolerance='foo')
6666

6767
with pytest.raises(

0 commit comments

Comments
 (0)