From 6766803791cb2824ea4107ea70736acd066b9bbb Mon Sep 17 00:00:00 2001 From: Leonardo Rosenberg Date: Sun, 11 Apr 2021 16:04:15 +0300 Subject: [PATCH 1/8] DOC: added parameters to reindex function docstrigs --- pandas/core/indexes/base.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a0727500ecc81..4c517d06ee255 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3763,11 +3763,31 @@ 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’} + 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 + Broadcast across a level, matching Index values on the passed MultiIndex level. + limit : int, default None + Maximum number of consecutive elements to forward or backward fill. + tolerance : optional + 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 ------- From 9a657cde970cdfb5fb6b2d2132ec888092a5b1ae Mon Sep 17 00:00:00 2001 From: Leonardo Rosenberg Date: Sun, 11 Apr 2021 16:04:59 +0300 Subject: [PATCH 2/8] DOC: added parameters to reindex function docstrigs --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 4c517d06ee255..f4cec2598749a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3783,7 +3783,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): tolerance : optional 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. + 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 From d684b99c293299fbfeaecbc2483d34f87b262de2 Mon Sep 17 00:00:00 2001 From: Leonardo Rosenberg Date: Sun, 11 Apr 2021 16:23:27 +0300 Subject: [PATCH 3/8] DOC: added basic example reindex func GH40328 --- pandas/core/indexes/base.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f4cec2598749a..75360403776ef 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3795,6 +3795,27 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): 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]}, + ... 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']) + >>> 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). From 2bf0bf52c830959a501aa9a3566200144722d614 Mon Sep 17 00:00:00 2001 From: Leonardo Rosenberg Date: Sun, 11 Apr 2021 16:44:01 +0300 Subject: [PATCH 4/8] DOC: updated docstrings reindex func pep8 GH40328 --- pandas/core/indexes/base.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 75360403776ef..70e7b16f90114 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3769,24 +3769,25 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): ---------- target : an iterable method : {None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’} - Method to use for filling holes in reindexed DataFrame. - Please note: this is only applicable to DataFrames/Series + 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 - Broadcast across a level, matching Index values on the passed MultiIndex level. + Broadcast across a level, matching Index values on + the passed MultiIndex level. limit : int, default None Maximum number of consecutive elements to forward or backward fill. tolerance : optional - Maximum distance between original and new labels for inexact matches. + 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 + 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 @@ -3796,7 +3797,6 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): indexer : np.ndarray or None Indices of output values in original index. - Examples -------- >>> index = ['a', 'b', 'c', 'd'] From 010f942fc6c83b99f8c2a2687fe1c18982c95077 Mon Sep 17 00:00:00 2001 From: Leonardo Rosenberg Date: Sun, 11 Apr 2021 16:51:27 +0300 Subject: [PATCH 5/8] DOC: updated LINE 3786:79 whitespace GH40328 --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 70e7b16f90114..c8b765d16ad7b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3783,7 +3783,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): Maximum number of consecutive elements to forward or backward fill. tolerance : optional Maximum distance between original and new labels for inexact matches. - The values of the index at the matching locations most satisfy the + 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. From 9d894b26876d0d5d43883557af7812295bbf11b9 Mon Sep 17 00:00:00 2001 From: Leonardo Rosenberg Date: Mon, 12 Apr 2021 12:20:10 +0300 Subject: [PATCH 6/8] DOC: generalized parameters for reindex/get_indexer (#40328) --- pandas/core/indexes/base.py | 73 +++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c8b765d16ad7b..41a8ec8943193 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3348,34 +3348,45 @@ def get_loc(self, key, method=None, tolerance=None): return loc _index_shared_docs[ - "get_indexer" + "method" ] = """ - Compute indexer and mask for new index given the current index. The - indexer should be then used as an input to ndarray.take to align the - current data to the new index. - - Parameters - ---------- - target : %(target_klass)s - method : {None, 'pad'/'ffill', 'backfill'/'bfill', 'nearest'}, optional + {None, 'pad'/'ffill', 'backfill'/'bfill', 'nearest'}, optional * default: exact matches only. * pad / ffill: find the PREVIOUS index value if no exact match. * backfill / bfill: use NEXT index value if no exact match * nearest: use the NEAREST index value if no exact match. Tied distances are broken by preferring the larger index value. - limit : int, optional + """ + _index_shared_docs[ + "limit" + ] = """ + int, optional Maximum number of consecutive labels in ``target`` to match for inexact matches. - tolerance : optional + """ + + _index_shared_docs[ + "tolerance" + ] = """ + optional Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must 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. + _index_shared_docs[ + "get_indexer" + ] = """ + Compute indexer and mask for new index given the current index. The + indexer should be then used as an input to ndarray.take to align the + current data to the new index. + + Parameters + ---------- + target : %(target_klass)s + method : %(method)s + limit : %(limit)s + tolerance : %(tolerance)s Returns ------- @@ -3761,34 +3772,20 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None: if not self._index_as_unique and len(indexer): raise ValueError("cannot reindex from a duplicate axis") - def reindex(self, target, method=None, level=None, limit=None, tolerance=None): - """ + _index_shared_docs[ + "reindex" + ] = """ Create an index with target's values. Parameters ---------- target : an iterable - method : {None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’} - 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. + method : %(method)s level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level. - limit : int, default None - Maximum number of consecutive elements to forward or backward fill. - tolerance : optional - 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. + limit : %(limit)s + tolerance : %(tolerance) Returns ------- @@ -3817,6 +3814,10 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): >>> target, indexer (Index(['I', 'd', 'III', 'IV'], dtype='object'), array([-1, 3, 0, -1])) """ + + @Appender(_index_shared_docs["reindex"] % _index_doc_kwargs) + @final + def reindex(self, target, method=None, level=None, limit=None, tolerance=None): # GH6552: preserve names when reindexing to non-named target # (i.e. neither Index nor Series). preserve_names = not hasattr(target, "name") From 4628ec2dbd723754d62f59ec0f3935b58b26106c Mon Sep 17 00:00:00 2001 From: Leonardo Rosenberg Date: Mon, 12 Apr 2021 12:26:57 +0300 Subject: [PATCH 7/8] DOC: generalized parameters for reindex/get_indexer (#40328) --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 41a8ec8943193..ec7f0b72f7040 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3785,7 +3785,7 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None: Broadcast across a level, matching Index values on the passed MultiIndex level. limit : %(limit)s - tolerance : %(tolerance) + tolerance : %(tolerance)s Returns ------- From 79089845e487d93f20e73d258e0bf5aae25cc1b2 Mon Sep 17 00:00:00 2001 From: Leonardo Rosenberg Date: Mon, 10 May 2021 08:16:51 +0300 Subject: [PATCH 8/8] DOC: shared docs reindex/get_indexer, fixed example GH40328 --- pandas/core/indexes/base.py | 297 ++++++++++++++++++------------------ 1 file changed, 146 insertions(+), 151 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ec7f0b72f7040..530c26fc231b8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1,10 +1,11 @@ from __future__ import annotations +import functools +import operator +import warnings from copy import copy as copy_func from datetime import datetime -import functools from itertools import zip_longest -import operator from typing import ( TYPE_CHECKING, Any, @@ -14,20 +15,21 @@ TypeVar, cast, ) -import warnings import numpy as np +import pandas._libs.join as libjoin +from pandas._libs.lib import ( + is_datetime_array, + no_default, +) +import pandas.core.algorithms as algos +import pandas.core.common as com from pandas._libs import ( algos as libalgos, index as libindex, lib, ) -import pandas._libs.join as libjoin -from pandas._libs.lib import ( - is_datetime_array, - no_default, -) from pandas._libs.tslibs import ( IncompatibleFrequency, NaTType, @@ -46,16 +48,33 @@ final, ) from pandas.compat.numpy import function as nv -from pandas.errors import ( - DuplicateLabelError, - InvalidIndexError, +from pandas.core import ( + missing, + ops, ) -from pandas.util._decorators import ( - Appender, - cache_readonly, - doc, +from pandas.core.accessor import CachedAccessor +from pandas.core.array_algos.putmask import ( + setitem_datetimelike_compat, + validate_putmask, +) +from pandas.core.arrays import ( + Categorical, + ExtensionArray, +) +from pandas.core.arrays.datetimes import ( + tz_to_dtype, + validate_tz_from_dtype, +) +from pandas.core.arrays.sparse import SparseDtype +from pandas.core.base import ( + IndexOpsMixin, + PandasObject, +) +from pandas.core.construction import ( + ensure_wrapped_if_datetimelike, + extract_array, + sanitize_array, ) - from pandas.core.dtypes.cast import ( can_hold_element, find_common_type, @@ -109,36 +128,6 @@ is_valid_na_for_dtype, isna, ) - -from pandas.core import ( - missing, - ops, -) -from pandas.core.accessor import CachedAccessor -import pandas.core.algorithms as algos -from pandas.core.array_algos.putmask import ( - setitem_datetimelike_compat, - validate_putmask, -) -from pandas.core.arrays import ( - Categorical, - ExtensionArray, -) -from pandas.core.arrays.datetimes import ( - tz_to_dtype, - validate_tz_from_dtype, -) -from pandas.core.arrays.sparse import SparseDtype -from pandas.core.base import ( - IndexOpsMixin, - PandasObject, -) -import pandas.core.common as com -from pandas.core.construction import ( - ensure_wrapped_if_datetimelike, - extract_array, - sanitize_array, -) from pandas.core.indexers import deprecate_ndim_indexing from pandas.core.indexes.frozen import FrozenList from pandas.core.ops import get_op_result_name @@ -149,7 +138,10 @@ nargsort, ) from pandas.core.strings import StringMethods - +from pandas.errors import ( + DuplicateLabelError, + InvalidIndexError, +) from pandas.io.formats.printing import ( PrettyDict, default_pprint, @@ -157,6 +149,11 @@ format_object_summary, pprint_thing, ) +from pandas.util._decorators import ( + Appender, + cache_readonly, + doc, +) if TYPE_CHECKING: from pandas import ( @@ -168,7 +165,6 @@ Series, ) - __all__ = ["Index"] _unsortable_types = frozenset(("mixed", "mixed-integer")) @@ -184,7 +180,6 @@ _index_shared_docs = {} str_t = str - _o_dtype = np.dtype("object") @@ -195,12 +190,12 @@ def _maybe_return_indexers(meth: F) -> F: @functools.wraps(meth) def join( - self, - other, - how: str_t = "left", - level=None, - return_indexers: bool = False, - sort: bool = False, + self, + other, + how: str_t = "left", + level=None, + return_indexers: bool = False, + sort: bool = False, ): join_index, lidx, ridx = meth(self, other, how=how, level=level, sort=sort) if not return_indexers: @@ -290,9 +285,9 @@ class Index(IndexOpsMixin, PandasObject): # tolist is not actually deprecated, just suppressed in the __dir__ _hidden_attrs: frozenset[str] = ( - PandasObject._hidden_attrs - | IndexOpsMixin._hidden_attrs - | frozenset(["contains", "set_value"]) + PandasObject._hidden_attrs + | IndexOpsMixin._hidden_attrs + | frozenset(["contains", "set_value"]) ) # To hand over control to subclasses @@ -306,17 +301,17 @@ def _left_indexer_unique(self, left: np.ndarray, right: np.ndarray) -> np.ndarra return libjoin.left_join_indexer_unique(left, right) def _left_indexer( - self, left: np.ndarray, right: np.ndarray + self, left: np.ndarray, right: np.ndarray ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: return libjoin.left_join_indexer(left, right) def _inner_indexer( - self, left: np.ndarray, right: np.ndarray + self, left: np.ndarray, right: np.ndarray ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: return libjoin.inner_join_indexer(left, right) def _outer_indexer( - self, left: np.ndarray, right: np.ndarray + self, left: np.ndarray, right: np.ndarray ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: return libjoin.outer_join_indexer(left, right) @@ -350,7 +345,7 @@ def _outer_indexer( # Constructors def __new__( - cls, data=None, dtype=None, copy=False, name=None, tupleize_cols=True, **kwargs + cls, data=None, dtype=None, copy=False, name=None, tupleize_cols=True, **kwargs ) -> Index: if kwargs: @@ -828,7 +823,7 @@ def view(self, cls=None): dtype = pandas_dtype(cls) if isinstance(dtype, (np.dtype, ExtensionDtype)) and needs_i8_conversion( - dtype + dtype ): if dtype.kind == "m" and dtype != "m8[ns]": # e.g. m8[s] @@ -1012,11 +1007,11 @@ def repeat(self, repeats, axis=None): # Copying Methods def copy( - self: _IndexT, - name: Hashable | None = None, - deep: bool = False, - dtype: Dtype | None = None, - names: Sequence[Hashable] | None = None, + self: _IndexT, + name: Hashable | None = None, + deep: bool = False, + dtype: Dtype | None = None, + names: Sequence[Hashable] | None = None, ) -> _IndexT: """ Make a copy of this object. @@ -1144,10 +1139,10 @@ def _mpl_repr(self): return self.values def format( - self, - name: bool = False, - formatter: Callable | None = None, - na_rep: str_t = "NaN", + self, + name: bool = False, + formatter: Callable | None = None, + na_rep: str_t = "NaN", ) -> list[str_t]: """ Render a string representation of the Index. @@ -1166,7 +1161,7 @@ def format( return self._format_with_header(header, na_rep=na_rep) def _format_with_header( - self, header: list[str_t], na_rep: str_t = "NaN" + self, header: list[str_t], na_rep: str_t = "NaN" ) -> list[str_t]: from pandas.io.formats.format import format_array @@ -1438,7 +1433,7 @@ def name(self, value): @final def _validate_names( - self, name=None, names=None, deep: bool = False + self, name=None, names=None, deep: bool = False ) -> list[Hashable]: """ Handles the quirks of having a singular 'name' parameter for general @@ -2897,7 +2892,7 @@ def union(self, other, sort=None): if not is_dtype_equal(self.dtype, other.dtype): if isinstance(self, ABCMultiIndex) and not is_object_dtype( - unpack_nested_dtype(other) + unpack_nested_dtype(other) ): raise NotImplementedError( "Can only union MultiIndex with MultiIndex or Index of tuples, " @@ -2958,10 +2953,10 @@ def _union(self, other: Index, sort): rvals = other._values if ( - sort is None - and self.is_monotonic - and other.is_monotonic - and not (self.has_duplicates and other.has_duplicates) + sort is None + and self.is_monotonic + and other.is_monotonic + and not (self.has_duplicates and other.has_duplicates) ): # Both are unique and monotonic, so can use outer join try: @@ -3350,7 +3345,7 @@ def get_loc(self, key, method=None, tolerance=None): _index_shared_docs[ "method" ] = """ - {None, 'pad'/'ffill', 'backfill'/'bfill', 'nearest'}, optional + {'pad'/'ffill', 'backfill'/'bfill', 'nearest'}, optional * default: exact matches only. * pad / ffill: find the PREVIOUS index value if no exact match. * backfill / bfill: use NEXT index value if no exact match @@ -3368,7 +3363,7 @@ def get_loc(self, key, method=None, tolerance=None): _index_shared_docs[ "tolerance" ] = """ - optional + int, optional Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation ``abs(index[indexer] - target) <= tolerance``. @@ -3405,14 +3400,14 @@ def get_loc(self, key, method=None, tolerance=None): and ``x`` is marked by -1, as it is not in ``index``. """ - @Appender(_index_shared_docs["get_indexer"] % _index_doc_kwargs) + @Appender(_index_shared_docs["get_indexer"] % {**_index_doc_kwargs, **_index_shared_docs}) @final def get_indexer( - self, - target, - method: str_t | None = None, - limit: int | None = None, - tolerance=None, + self, + target, + method: str_t | None = None, + limit: int | None = None, + tolerance=None, ) -> np.ndarray: method = missing.clean_reindex_fill_method(method) @@ -3437,11 +3432,11 @@ def get_indexer( return self._get_indexer(target, method, limit, tolerance) def _get_indexer( - self, - target: Index, - method: str_t | None = None, - limit: int | None = None, - tolerance=None, + self, + target: Index, + method: str_t | None = None, + limit: int | None = None, + tolerance=None, ) -> np.ndarray: if tolerance is not None: tolerance = self._convert_tolerance(tolerance, target) @@ -3503,7 +3498,7 @@ def _convert_tolerance(self, tolerance, target: np.ndarray | Index) -> np.ndarra @final def _get_fill_indexer( - self, target: Index, method: str_t, limit: int | None = None, tolerance=None + self, target: Index, method: str_t, limit: int | None = None, tolerance=None ) -> np.ndarray: target_values = target._get_engine_target() @@ -3523,7 +3518,7 @@ def _get_fill_indexer( @final def _get_fill_indexer_searchsorted( - self, target: Index, method: str_t, limit: int | None = None + self, target: Index, method: str_t, limit: int | None = None ) -> np.ndarray: """ Fallback pad/backfill get_indexer that works for monotonic decreasing @@ -3557,7 +3552,7 @@ def _get_fill_indexer_searchsorted( @final def _get_nearest_indexer( - self, target: Index, limit: int | None, tolerance + self, target: Index, limit: int | None, tolerance ) -> np.ndarray: """ Get the indexer for the nearest index labels; requires an index with @@ -3587,10 +3582,10 @@ def _get_nearest_indexer( @final def _filter_indexer_tolerance( - self, - target: Index | np.ndarray | ExtensionArray, - indexer: np.ndarray, - tolerance, + self, + target: Index | np.ndarray | ExtensionArray, + indexer: np.ndarray, + tolerance, ) -> np.ndarray: own_values = self._get_engine_target() distance = abs(own_values[indexer] - target) @@ -3642,7 +3637,7 @@ def is_int(v): is_index_slice = is_int(start) and is_int(stop) and is_int(step) is_positional = is_index_slice and not ( - self.is_integer() or self.is_categorical() + self.is_integer() or self.is_categorical() ) if kind == "getitem": @@ -3781,7 +3776,7 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None: ---------- target : an iterable method : %(method)s - level : int or name + level : int, optional Broadcast across a level, matching Index values on the passed MultiIndex level. limit : %(limit)s @@ -3797,9 +3792,9 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None: Examples -------- >>> index = ['a', 'b', 'c', 'd'] - >>> df = pd.DataFrame({"A": [1,2,3,4], - ... "B": [5,6,7,8], - ... "C": [9,10,11,12]}, + >>> df = pd.DataFrame({"A": [1,2,3,4], + ... "B": [5,6,7,8], + ... "C": [9,10,11,12]}, ... index=index) >>> df A B C @@ -3811,11 +3806,11 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None: >>> target, indexer (Index(['I', 'II', 'III', 'IV'], dtype='object'), array([-1, -1, -1, -1])) >>> target, indexer = df.index.reindex(target=['I', 'd', 'a', 'IV']) - >>> target, indexer - (Index(['I', 'd', 'III', 'IV'], dtype='object'), array([-1, 3, 0, -1])) + >>> target, indexerpanda + (Index(['I', 'd', 'a', 'IV'], dtype='object'), array([-1, 3, 0, -1])) """ - @Appender(_index_shared_docs["reindex"] % _index_doc_kwargs) + @Appender(_index_shared_docs["reindex"] % _index_shared_docs) @final def reindex(self, target, method=None, level=None, limit=None, tolerance=None): # GH6552: preserve names when reindexing to non-named target @@ -3931,12 +3926,12 @@ def _reindex_non_unique(self, target): @_maybe_return_indexers def join( - self, - other, - how: str_t = "left", - level=None, - return_indexers: bool = False, - sort: bool = False, + self, + other, + how: str_t = "left", + level=None, + return_indexers: bool = False, + sort: bool = False, ): """ Compute join_index and indexers to conform data @@ -4010,12 +4005,12 @@ def join( else: return self._join_non_unique(other, how=how) elif ( - self.is_monotonic - and other.is_monotonic - and ( - not isinstance(self, ABCMultiIndex) - or not any(is_categorical_dtype(dtype) for dtype in self.dtypes) - ) + self.is_monotonic + and other.is_monotonic + and ( + not isinstance(self, ABCMultiIndex) + or not any(is_categorical_dtype(dtype) for dtype in self.dtypes) + ) ): # Categorical is monotonic if data are ordered as categories, but join can # not handle this in case of not lexicographically monotonic GH#38502 @@ -4254,7 +4249,7 @@ def _get_leaf_sorter(labels: list[np.ndarray]) -> np.ndarray: ) # missing values are placed first; drop them! - left_indexer = left_indexer[counts[0] :] + left_indexer = left_indexer[counts[0]:] new_codes = [lab[left_indexer] for lab in new_codes] else: # sort the leaves @@ -4344,7 +4339,7 @@ def _join_monotonic(self, other: Index, how="left"): return join_index, lidx, ridx def _wrap_joined_index( - self: _IndexT, joined: np.ndarray, other: _IndexT + self: _IndexT, joined: np.ndarray, other: _IndexT ) -> _IndexT: assert other.dtype == self.dtype @@ -4814,12 +4809,12 @@ def identical(self, other) -> bool: otherwise False. """ return ( - self.equals(other) - and all( - getattr(self, c, None) == getattr(other, c, None) - for c in self._comparables - ) - and type(self) == type(other) + self.equals(other) + and all( + getattr(self, c, None) == getattr(other, c, None) + for c in self._comparables + ) + and type(self) == type(other) ) @final @@ -4929,11 +4924,11 @@ def asof_locs(self, where: Index, mask) -> np.ndarray: @final def sort_values( - self, - return_indexer: bool = False, - ascending: bool = True, - na_position: str_t = "last", - key: Callable | None = None, + self, + return_indexer: bool = False, + ascending: bool = True, + na_position: str_t = "last", + key: Callable | None = None, ): """ Return a sorted copy of the index. @@ -5332,9 +5327,9 @@ def _maybe_promote(self, other: Index) -> tuple[Index, Index]: if isinstance(self, ABCDatetimeIndex) and isinstance(other, ABCDatetimeIndex): if ( - self.tz is not None - and other.tz is not None - and not tz_compare(self.tz, other.tz) + self.tz is not None + and other.tz is not None + and not tz_compare(self.tz, other.tz) ): # standardize on UTC return self.tz_convert("UTC"), other.tz_convert("UTC") @@ -5373,9 +5368,9 @@ def _find_common_type_compat(self, target) -> DtypeObj: if dtype.kind in ["i", "u"]: # TODO: what about reversed with self being categorical? if ( - isinstance(target, Index) - and is_categorical_dtype(target.dtype) - and target.hasnans + isinstance(target, Index) + and is_categorical_dtype(target.dtype) + and target.hasnans ): # FIXME: find_common_type incorrect with Categorical GH#38240 # FIXME: some cases where float64 cast can be lossy? @@ -5391,7 +5386,7 @@ def _should_compare(self, other: Index) -> bool: """ if (other.is_boolean() and self.is_numeric()) or ( - self.is_boolean() and other.is_numeric() + self.is_boolean() and other.is_numeric() ): # GH#16877 Treat boolean labels passed to a numeric index as not # found. Without this fix False and True would be treated as 0 and 1 @@ -5590,11 +5585,11 @@ def _get_string_slice(self, key: str_t): raise NotImplementedError def slice_indexer( - self, - start: Hashable | None = None, - end: Hashable | None = None, - step: int | None = None, - kind: str_t | None = None, + self, + start: Hashable | None = None, + end: Hashable | None = None, + step: int | None = None, + kind: str_t | None = None, ) -> slice: """ Compute the slice indexer for input labels and step. @@ -5983,7 +5978,7 @@ def _cmp_method(self, other, op): return np.zeros(len(self), dtype=bool) if isinstance(other, (np.ndarray, Index, ABCSeries, ExtensionArray)) and len( - self + self ) != len(other): raise ValueError("Lengths must match to compare") @@ -6150,11 +6145,11 @@ def _maybe_disable_logical_methods(self, opname: str_t): raise if this Index subclass does not support any or all. """ if ( - isinstance(self, ABCMultiIndex) - or needs_i8_conversion(self.dtype) - or is_interval_dtype(self.dtype) - or is_categorical_dtype(self.dtype) - or is_float_dtype(self.dtype) + isinstance(self, ABCMultiIndex) + or needs_i8_conversion(self.dtype) + or is_interval_dtype(self.dtype) + or is_categorical_dtype(self.dtype) + or is_float_dtype(self.dtype) ): # This call will raise make_invalid_op(opname)(self) @@ -6427,7 +6422,7 @@ def _maybe_cast_data_without_dtype(subarr): def _try_convert_to_int_array( - data: np.ndarray, copy: bool, dtype: np.dtype + data: np.ndarray, copy: bool, dtype: np.dtype ) -> np.ndarray: """ Attempt to convert an array of data into an integer array.