Skip to content

Commit 9b42a9c

Browse files
Brian TuBrian Tu
Brian Tu
authored and
Brian Tu
committed
Added rest of Jeff's changes
1 parent 3c42dc7 commit 9b42a9c

File tree

8 files changed

+30
-20
lines changed

8 files changed

+30
-20
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Other Enhancements
234234
- :meth:`DataFrame.assign` will preserve the original order of ``**kwargs`` for Python 3.6+ users instead of sorting the column names. (:issue:`14207`)
235235
- Improved the import time of pandas by about 2.25x. (:issue:`16764`)
236236
- :func:`read_json` and :func:`to_json` now accept a ``compression`` argument which allows them to transparently handle compressed files. (:issue:`17798`)
237-
- :func:`Series.reindex`, :func:`DataFrame.reindex`, :func:`Index.get_indexer` now support list-like argument for ``tolerance``
237+
- :func:`Series.reindex`, :func:`DataFrame.reindex`, :func:`Index.get_indexer` now support list-like argument for ``tolerance``. (PR #17367)
238238

239239
.. _whatsnew_0210.api_breaking:
240240

pandas/core/indexes/base.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -2661,10 +2661,7 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
26612661
method = missing.clean_reindex_fill_method(method)
26622662
target = _ensure_index(target)
26632663
if tolerance is not None:
2664-
tolerance = self._convert_tolerance(tolerance)
2665-
if target.size != tolerance.size and tolerance.size > 1:
2666-
raise ValueError('list-like tolerance size must match '
2667-
'target index size')
2664+
tolerance = self._convert_tolerance(tolerance, target)
26682665

26692666
# Treat boolean labels passed to a numeric index as not found. Without
26702667
# this fix False and True would be treated as 0 and 1 respectively.
@@ -2702,9 +2699,13 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
27022699
indexer = self._engine.get_indexer(target._values)
27032700
return _ensure_platform_int(indexer)
27042701

2705-
def _convert_tolerance(self, tolerance):
2702+
def _convert_tolerance(self, tolerance, target):
27062703
# override this method on subclasses
2707-
return np.asarray(tolerance)
2704+
tolerance = np.asarray(tolerance)
2705+
if target.size != tolerance.size and tolerance.size > 1:
2706+
raise ValueError('list-like tolerance size must match '
2707+
'target index size')
2708+
return tolerance
27082709

27092710
def _get_fill_indexer(self, target, method, limit=None, tolerance=None):
27102711
if self.is_monotonic_increasing and target.is_monotonic_increasing:

pandas/core/indexes/datetimelike.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,12 @@ def asobject(self):
432432
from pandas.core.index import Index
433433
return Index(self._box_values(self.asi8), name=self.name, dtype=object)
434434

435-
def _convert_tolerance(self, tolerance):
436-
return np.asarray(to_timedelta(tolerance, box=False))
435+
def _convert_tolerance(self, tolerance, target):
436+
tolerance = np.asarray(to_timedelta(tolerance, box=False))
437+
if target.size != tolerance.size and tolerance.size > 1:
438+
raise ValueError('list-like tolerance size must match '
439+
'target index size')
440+
return tolerance
437441

438442
def _maybe_mask_results(self, result, fill_value=None, convert=None):
439443
"""

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ def get_loc(self, key, method=None, tolerance=None):
14231423
if tolerance is not None:
14241424
# try converting tolerance now, so errors don't get swallowed by
14251425
# the try/except clauses below
1426-
tolerance = self._convert_tolerance(tolerance)
1426+
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
14271427

14281428
if isinstance(key, datetime):
14291429
# needed to localize naive datetimes

pandas/core/indexes/numeric.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ def _convert_for_op(self, value):
7171

7272
return value
7373

74-
def _convert_tolerance(self, tolerance):
74+
def _convert_tolerance(self, tolerance, target):
7575
tolerance = np.asarray(tolerance)
76+
if target.size != tolerance.size and tolerance.size > 1:
77+
raise ValueError('list-like tolerance size must match '
78+
'target index size')
7679
if not np.issubdtype(tolerance.dtype, np.number):
7780
if tolerance.ndim > 0:
7881
raise ValueError(('tolerance argument for %s must contain '

pandas/core/indexes/period.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,7 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
787787
target = target.asi8
788788

789789
if tolerance is not None:
790-
tolerance = self._convert_tolerance(tolerance)
791-
if target.size != tolerance.size and tolerance.size > 1:
792-
raise ValueError('list-like tolerance size must match '
793-
'target index size')
790+
tolerance = self._convert_tolerance(tolerance, target)
794791
return Index.get_indexer(self._int64index, target, method,
795792
limit, tolerance)
796793

@@ -833,7 +830,8 @@ def get_loc(self, key, method=None, tolerance=None):
833830
try:
834831
ordinal = tslib.iNaT if key is tslib.NaT else key.ordinal
835832
if tolerance is not None:
836-
tolerance = self._convert_tolerance(tolerance)
833+
tolerance = self._convert_tolerance(tolerance,
834+
np.asarray(key))
837835
return self._int64index.get_loc(ordinal, method, tolerance)
838836

839837
except KeyError:
@@ -916,8 +914,12 @@ def _get_string_slice(self, key):
916914
return slice(self.searchsorted(t1.ordinal, side='left'),
917915
self.searchsorted(t2.ordinal, side='right'))
918916

919-
def _convert_tolerance(self, tolerance):
920-
tolerance = DatetimeIndexOpsMixin._convert_tolerance(self, tolerance)
917+
def _convert_tolerance(self, tolerance, target):
918+
tolerance = DatetimeIndexOpsMixin._convert_tolerance(self, tolerance,
919+
target)
920+
if target.size != tolerance.size and tolerance.size > 1:
921+
raise ValueError('list-like tolerance size must match '
922+
'target index size')
921923
return self._maybe_convert_timedelta(tolerance)
922924

923925
def insert(self, loc, item):

pandas/core/indexes/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def get_loc(self, key, method=None, tolerance=None):
699699
if tolerance is not None:
700700
# try converting tolerance now, so errors don't get swallowed by
701701
# the try/except clauses below
702-
tolerance = self._convert_tolerance(tolerance)
702+
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
703703

704704
if _is_convertible_to_td(key):
705705
key = Timedelta(key)

pandas/core/tools/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'):
8686
elif is_list_like(arg) and getattr(arg, 'ndim', 1) <= 1:
8787
if getattr(arg, 'ndim', 1) == 0:
8888
# extract array scalar and process below
89-
arg = arg[()]
89+
arg = arg.item()
9090
else:
9191
return _convert_listlike(arg, unit=unit, box=box, errors=errors)
9292
elif getattr(arg, 'ndim', 1) > 1:

0 commit comments

Comments
 (0)