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
45 changes: 41 additions & 4 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import (
Appender,
Substitution,
cache_readonly,
)
from pandas.util._validators import (
Expand Down Expand Up @@ -1669,9 +1667,48 @@ def factorize(
Categories (3, str): ['a', 'b', 'c']
"""

@Substitution(klass="ExtensionArray")
@Appender(_extension_array_shared_docs["repeat"])
def repeat(self, repeats: int | Sequence[int], axis: AxisInt | None = None) -> Self:
"""
Repeat elements of an ExtensionArray.

Returns a new ExtensionArray where each element of the current ExtensionArray
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
ExtensionArray.
axis : None
Must be ``None``. Has no effect but is accepted for compatibility
with numpy.

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

See Also
--------
Series.repeat : Equivalent function for Series.
Index.repeat : Equivalent function for Index.
numpy.repeat : Similar method for :class:`numpy.ndarray`.
ExtensionArray.take : Take arbitrary positions.

Examples
--------
>>> cat = pd.Categorical(["a", "b", "c"])
>>> cat
['a', 'b', 'c']
Categories (3, str): ['a', 'b', 'c']
>>> cat.repeat(2)
['a', 'a', 'b', 'b', 'c', 'c']
Categories (3, str): ['a', 'b', 'c']
>>> cat.repeat([1, 2, 3])
['a', 'b', 'b', 'c', 'c', 'c']
Categories (3, str): ['a', 'b', 'c']
"""
nv.validate_repeat((), {"axis": axis})
ind = np.arange(len(self)).repeat(repeats)
return self.take(ind)
Expand Down
Loading