Skip to content

Commit 0dac264

Browse files
DOC: Document and annotate Index.reindex (pandas-dev#40328).
1 parent 3976bba commit 0dac264

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

pandas/core/indexes/base.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -3939,21 +3939,69 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None:
39393939
raise ValueError("cannot reindex on an axis with duplicate labels")
39403940

39413941
def reindex(
3942-
self, target, method=None, level=None, limit=None, tolerance=None
3942+
self,
3943+
target,
3944+
method: str | None = None,
3945+
level: int | None = None,
3946+
limit: int | None = None,
3947+
tolerance: int | float | None = None,
39433948
) -> tuple[Index, npt.NDArray[np.intp] | None]:
39443949
"""
39453950
Create index with target's values.
39463951
39473952
Parameters
39483953
----------
39493954
target : an iterable
3955+
method : {None, 'pad'/'ffill', 'backfill'/'bfill', 'nearest'}, optional
3956+
* default: exact matches only.
3957+
* pad / ffill: find the PREVIOUS index value if no exact match.
3958+
* backfill / bfill: use NEXT index value if no exact match
3959+
* nearest: use the NEAREST index value if no exact match. Tied
3960+
distances are broken by preferring the larger index value.
3961+
level : int, optional
3962+
Level of multiindex.
3963+
limit : int, optional
3964+
Maximum number of consecutive labels in ``target`` to match for
3965+
inexact matches.
3966+
tolerance : int or float, optional
3967+
Maximum distance between original and new labels for inexact
3968+
matches. The values of the index at the matching locations must
3969+
satisfy the equation ``abs(index[indexer] - target) <= tolerance``.
3970+
3971+
Tolerance may be a scalar value, which applies the same tolerance
3972+
to all values, or list-like, which applies variable tolerance per
3973+
element. List-like includes list, tuple, array, Series, and must be
3974+
the same size as the index and its dtype must exactly match the
3975+
index's type.
39503976
39513977
Returns
39523978
-------
39533979
new_index : pd.Index
39543980
Resulting index.
39553981
indexer : np.ndarray[np.intp] or None
39563982
Indices of output values in original index.
3983+
3984+
Raises
3985+
------
3986+
TypeError
3987+
If ``method`` passed along with ``level``.
3988+
ValueError
3989+
If non-unique multi-index
3990+
ValueError
3991+
If non-unique index and ``method`` or ``limit`` passed.
3992+
3993+
See Also
3994+
--------
3995+
Series.reindex
3996+
DataFrame.reindex
3997+
3998+
Examples
3999+
--------
4000+
>>> idx = pd.Index(['car', 'bike', 'train', 'tractor'])
4001+
>>> idx
4002+
Index(['car', 'bike', 'train', 'tractor'], dtype='object')
4003+
>>> idx.reindex(['car', 'bike'])
4004+
(Index(['car', 'bike'], dtype='object'), array([0, 1], dtype=int64))
39574005
"""
39584006
# GH6552: preserve names when reindexing to non-named target
39594007
# (i.e. neither Index nor Series).

0 commit comments

Comments
 (0)