Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 81 additions & 4 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
UnsortedIndexError,
)
from pandas.util._decorators import (
Appender,
cache_readonly,
doc,
set_module,
Expand Down Expand Up @@ -101,7 +100,6 @@
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import (
Index,
_index_shared_docs,
ensure_index,
get_unanimous_names,
)
Expand Down Expand Up @@ -2290,7 +2288,6 @@ def _getitem_slice(self: MultiIndex, slobj: slice) -> MultiIndex:
verify_integrity=False,
)

@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
def take(
self: MultiIndex,
indices,
Expand All @@ -2299,6 +2296,51 @@ def take(
fill_value=None,
**kwargs,
) -> MultiIndex:
"""
Return a new MultiIndex of the values selected by the indices.

For internal compatibility with numpy arrays.

Parameters
----------
indices : array-like
Indices to be taken.
axis : int, optional
The axis over which to select values, always 0.
allow_fill : bool, default True
How to handle negative values in `indices`.

* False: negative values in `indices` indicate positional indices
from the right (the default). This is similar to
:func:`numpy.take`.

* True: negative values in `indices` indicate
missing values. These values are set to `fill_value`. Any other
other negative values raise a ``ValueError``.

fill_value : scalar, default None
If allow_fill=True and fill_value is not None, indices specified by
-1 are regarded as NA. If Index doesn't hold NA, raise ValueError.
**kwargs
Required for compatibility with numpy.

Returns
-------
Index
An index formed of elements at the given indices. Will be the same
type as self, except for RangeIndex.

See Also
--------
numpy.ndarray.take: Return an array formed from the
elements of a at the given indices.

Examples
--------
>>> idx = pd.Index(["a", "b", "c"])
>>> idx.take([2, 2, 1, 2])
Index(['c', 'c', 'b', 'c'], dtype='str')
Comment on lines +2340 to +2342
Copy link
Member

Choose a reason for hiding this comment

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

If you're interested in follow with with another PR, could you change these examples to use a MultiIndex instead of an Index

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mroeschke Thank you for the review! You're absolutely right, these examples should indeed use MultiIndex to better demonstrate the method's functionality.

I've understood what needs to be modified:

  • In the examples for both take and repeat methods, change the regular Index examples to MultiIndex examples

I'll fix these issues in a follow-up PR to ensure the example code uses proper MultiIndex to demonstrate the method usage.

Thanks for the guidance!

"""
nv.validate_take((), kwargs)
indices = ensure_platform_int(indices)

Expand Down Expand Up @@ -2454,8 +2496,43 @@ def argsort(
keys = [lev.codes for lev in target._get_codes_for_sorting()]
return lexsort_indexer(keys, na_position=na_position, codes_given=True)

@Appender(_index_shared_docs["repeat"] % _index_doc_kwargs)
def repeat(self, repeats: int, axis=None) -> MultiIndex:
"""
Repeat elements of a MultiIndex.

Returns a new MultiIndex where each element of the current MultiIndex
is repeated consecutively a given number of times.

Parameters
----------
repeats : int or array of ints
The number of repetitions for each element. This should be a
non-negative integer. Repeating 0 times will return an empty
MultiIndex.
axis : None
Must be ``None``. Has no effect but is accepted for compatibility
with numpy.

Returns
-------
MultiIndex
Newly created MultiIndex with repeated elements.

See Also
--------
Series.repeat : Equivalent function for Series.
numpy.repeat : Similar method for :class:`numpy.ndarray`.

Examples
--------
>>> idx = pd.Index(["a", "b", "c"])
>>> idx
Index(['a', 'b', 'c'], dtype='object')
>>> idx.repeat(2)
Index(['a', 'a', 'b', 'b', 'c', 'c'], dtype='object')
>>> idx.repeat([1, 2, 3])
Index(['a', 'b', 'b', 'c', 'c', 'c'], dtype='object')
"""
nv.validate_repeat((), {"axis": axis})
# error: Incompatible types in assignment (expression has type "ndarray",
# variable has type "int")
Expand Down
Loading