Skip to content

Commit 674e619

Browse files
DOC: Replace @appender decorator with inline docstrings for Index.take, Index.repeat, and Index.get_indexer_non_unique (#62798)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9e81c90 commit 674e619

File tree

1 file changed

+109
-16
lines changed

1 file changed

+109
-16
lines changed

pandas/core/indexes/base.py

Lines changed: 109 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,12 @@ def astype(self, dtype: Dtype, copy: bool = True):
11851185
How to handle negative values in `indices`.
11861186
11871187
* False: negative values in `indices` indicate positional indices
1188-
from the right (the default). This is similar to
1189-
:func:`numpy.take`.
1188+
from the right (the default). This is similar to
1189+
:func:`numpy.take`.
11901190
11911191
* True: negative values in `indices` indicate
1192-
missing values. These values are set to `fill_value`. Any other
1193-
other negative values raise a ``ValueError``.
1192+
missing values. These values are set to `fill_value`. Any other
1193+
other negative values raise a ``ValueError``.
11941194
11951195
fill_value : scalar, default None
11961196
If allow_fill=True and fill_value is not None, indices specified by
@@ -1216,7 +1216,6 @@ def astype(self, dtype: Dtype, copy: bool = True):
12161216
Index(['c', 'c', 'b', 'c'], dtype='str')
12171217
"""
12181218

1219-
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
12201219
def take(
12211220
self,
12221221
indices,
@@ -1225,6 +1224,51 @@ def take(
12251224
fill_value=None,
12261225
**kwargs,
12271226
) -> Self:
1227+
"""
1228+
Return a new Index of the values selected by the indices.
1229+
1230+
For internal compatibility with numpy arrays.
1231+
1232+
Parameters
1233+
----------
1234+
indices : array-like
1235+
Indices to be taken.
1236+
axis : int, optional
1237+
The axis over which to select values, always 0.
1238+
allow_fill : bool, default True
1239+
How to handle negative values in `indices`.
1240+
1241+
* False: negative values in `indices` indicate positional indices
1242+
from the right (the default). This is similar to
1243+
:func:`numpy.take`.
1244+
1245+
* True: negative values in `indices` indicate
1246+
missing values. These values are set to `fill_value`. Any
1247+
other negative values raise a ``ValueError``.
1248+
1249+
fill_value : scalar, default None
1250+
If allow_fill=True and fill_value is not None, indices specified by
1251+
-1 are regarded as NA. If Index doesn't hold NA, raise ValueError.
1252+
**kwargs
1253+
Required for compatibility with numpy.
1254+
1255+
Returns
1256+
-------
1257+
Index
1258+
An index formed of elements at the given indices. Will be the same
1259+
type as self, except for RangeIndex.
1260+
1261+
See Also
1262+
--------
1263+
numpy.ndarray.take: Return an array formed from the
1264+
elements of a at the given indices.
1265+
1266+
Examples
1267+
--------
1268+
>>> idx = pd.Index(["a", "b", "c"])
1269+
>>> idx.take([2, 2, 1, 2])
1270+
Index(['c', 'c', 'b', 'c'], dtype='str')
1271+
"""
12281272
if kwargs:
12291273
nv.validate_take((), kwargs)
12301274
if is_scalar(indices):
@@ -1272,26 +1316,27 @@ def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
12721316
allow_fill = False
12731317
return allow_fill
12741318

1275-
_index_shared_docs["repeat"] = """
1276-
Repeat elements of a %(klass)s.
1319+
def repeat(self, repeats, axis: None = None) -> Self:
1320+
"""
1321+
Repeat elements of a Index.
12771322
1278-
Returns a new %(klass)s where each element of the current %(klass)s
1323+
Returns a new Index where each element of the current Index
12791324
is repeated consecutively a given number of times.
12801325
12811326
Parameters
12821327
----------
12831328
repeats : int or array of ints
12841329
The number of repetitions for each element. This should be a
12851330
non-negative integer. Repeating 0 times will return an empty
1286-
%(klass)s.
1331+
Index.
12871332
axis : None
12881333
Must be ``None``. Has no effect but is accepted for compatibility
12891334
with numpy.
12901335
12911336
Returns
12921337
-------
1293-
%(klass)s
1294-
Newly created %(klass)s with repeated elements.
1338+
Index
1339+
Newly created Index with repeated elements.
12951340
12961341
See Also
12971342
--------
@@ -1300,17 +1345,14 @@ def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
13001345
13011346
Examples
13021347
--------
1303-
>>> idx = pd.Index(['a', 'b', 'c'])
1348+
>>> idx = pd.Index(["a", "b", "c"])
13041349
>>> idx
13051350
Index(['a', 'b', 'c'], dtype='object')
13061351
>>> idx.repeat(2)
13071352
Index(['a', 'a', 'b', 'b', 'c', 'c'], dtype='object')
13081353
>>> idx.repeat([1, 2, 3])
13091354
Index(['a', 'b', 'b', 'c', 'c', 'c'], dtype='object')
13101355
"""
1311-
1312-
@Appender(_index_shared_docs["repeat"] % _index_doc_kwargs)
1313-
def repeat(self, repeats, axis: None = None) -> Self:
13141356
repeats = ensure_platform_int(repeats)
13151357
nv.validate_repeat((), {"axis": axis})
13161358
res_values = self._values.repeat(repeats)
@@ -5993,10 +6035,61 @@ def _should_fallback_to_positional(self) -> bool:
59936035
(array([-1, 1, 3, 4, -1]), array([0, 2]))
59946036
"""
59956037

5996-
@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
59976038
def get_indexer_non_unique(
59986039
self, target
59996040
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
6041+
"""
6042+
Compute indexer and mask for new index given the current index.
6043+
6044+
The indexer should be then used as an input to ndarray.take to align the
6045+
current data to the new index.
6046+
6047+
Parameters
6048+
----------
6049+
target : Index
6050+
An iterable containing the values to be used for computing indexer.
6051+
6052+
Returns
6053+
-------
6054+
indexer : np.ndarray[np.intp]
6055+
Integers from 0 to n - 1 indicating that the index at these
6056+
positions matches the corresponding target values. Missing values
6057+
in the target are marked by -1.
6058+
missing : np.ndarray[np.intp]
6059+
An indexer into the target of the values not found.
6060+
These correspond to the -1 in the indexer array.
6061+
6062+
See Also
6063+
--------
6064+
Index.get_indexer : Computes indexer and mask for new index given
6065+
the current index.
6066+
Index.get_indexer_for : Returns an indexer even when non-unique.
6067+
6068+
Examples
6069+
--------
6070+
>>> index = pd.Index(["c", "b", "a", "b", "b"])
6071+
>>> index.get_indexer_non_unique(["b", "b"])
6072+
(array([1, 3, 4, 1, 3, 4]), array([], dtype=int64))
6073+
6074+
In the example below there are no matched values.
6075+
6076+
>>> index = pd.Index(["c", "b", "a", "b", "b"])
6077+
>>> index.get_indexer_non_unique(["q", "r", "t"])
6078+
(array([-1, -1, -1]), array([0, 1, 2]))
6079+
6080+
For this reason, the returned ``indexer`` contains only integers equal to -1.
6081+
It demonstrates that there's no match between the index and the ``target``
6082+
values at these positions. The mask [0, 1, 2] in the return value shows that
6083+
the first, second, and third elements are missing.
6084+
6085+
Notice that the return value is a tuple contains two items. In the example
6086+
below the first item is an array of locations in ``index``. The second
6087+
item is a mask shows that the first and third elements are missing.
6088+
6089+
>>> index = pd.Index(["c", "b", "a", "b", "b"])
6090+
>>> index.get_indexer_non_unique(["f", "b", "s"])
6091+
(array([-1, 1, 3, 4, -1]), array([0, 2]))
6092+
"""
60006093
target = self._maybe_cast_listlike_indexer(target)
60016094

60026095
if not self._should_compare(target) and not self._should_partial_index(target):

0 commit comments

Comments
 (0)