Skip to content

DOC: Updates Index.reindex docstrings (#40328) #40879

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

Closed
wants to merge 10 commits into from
43 changes: 42 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3763,18 +3763,59 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None:

def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
"""
Create index with target's values.
Create an index with target's values.

Parameters
----------
target : an iterable
method : {None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to share parts with NDFrame.reindex?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe share parts with other functions in this same file, such as get_indexer? @phofl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case you can better use , optional at the end, instead of having None in the list.

I don't think we ever use these forward ticks, can you check other docstrings to see if we use quotes at all or what.

Method to use for filling holes in reindexed DataFrame.
Please note: this is only applicable to DataFrames/Series
with a monotonically increasing/decreasing index.
- None (default): don’t fill gaps
- pad / ffill: Propagate last valid observation forward to next valid.
- backfill / bfill: Use next valid observation to fill gap.
- nearest: Use nearest valid observations to fill gap.
level : int or name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are types, I guess it should be int or str?

Broadcast across a level, matching Index values on
the passed MultiIndex level.
limit : int, default None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int, optional

Maximum number of consecutive elements to forward or backward fill.
tolerance : optional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type should be something like int or list-like of int, optional I think.

Maximum distance between original and new labels for inexact matches.
The values of the index at the matching locations most satisfy the
equation `abs(index[indexer] - target) <= tolerance`.
Tolerance may be a scalar value, which applies the same tolerance
to all values, or list-like, which applies variable tolerance per element.
List-like includes list, tuple, array, Series, and must be the same size
as the index and its dtype must exactly match the index’s type.

Returns
-------
new_index : pd.Index
Resulting index.
indexer : np.ndarray or None
Indices of output values in original index.

Examples
--------
>>> index = ['a', 'b', 'c', 'd']
>>> df = pd.DataFrame({"A": [1,2,3,4],
... "B": [5,6,7,8],
... "C": [9,10,11,12]},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP-8 requires spaces ater commas, can you add them please, and make sure this is PEP-8 compliant.

... index=index)
>>> df
A B C
a 1 5 9
b 2 6 10
c 3 7 11
d 4 8 12
>>> target, indexer = df.index.reindex(target=['I', 'II', 'III', 'IV'])
>>> target, indexer
(Index(['I', 'II', 'III', 'IV'], dtype='object'), array([-1, -1, -1, -1]))
>>> target, indexer = df.index.reindex(target=['I', 'd', 'a', 'IV'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check the failure on the CI? These tests are failing, something is not right. You can test the code locally using the master version of pandas.

After you fix these things, can you check that the CI is green, so we can merge.

>>> target, indexer
(Index(['I', 'd', 'III', 'IV'], dtype='object'), array([-1, 3, 0, -1]))
"""
# GH6552: preserve names when reindexing to non-named target
# (i.e. neither Index nor Series).
Expand Down