|
12 | 12 | from pandas.compat import PY3, set_function_name
|
13 | 13 | from pandas.compat.numpy import function as nv
|
14 | 14 | from pandas.errors import AbstractMethodError
|
| 15 | +from pandas.util._decorators import Appender, Substitution |
15 | 16 |
|
16 | 17 | from pandas.core.dtypes.common import is_list_like
|
17 | 18 | from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
|
|
20 | 21 |
|
21 | 22 | _not_implemented_message = "{} does not implement {}."
|
22 | 23 |
|
| 24 | +_extension_array_shared_docs = dict() |
| 25 | + |
23 | 26 |
|
24 | 27 | class ExtensionArray(object):
|
25 | 28 | """
|
@@ -580,32 +583,55 @@ def factorize(self, na_sentinel=-1):
|
580 | 583 | uniques = self._from_factorized(uniques, self)
|
581 | 584 | return labels, uniques
|
582 | 585 |
|
583 |
| - def repeat(self, repeats, axis=None): |
584 |
| - """ |
585 |
| - Repeat elements of an array. |
| 586 | + _extension_array_shared_docs['repeat'] = """ |
| 587 | + Repeat elements of a %(klass)s. |
586 | 588 |
|
587 |
| - .. versionadded:: 0.24.0 |
| 589 | + Returns a new %(klass)s where each element of the current %(klass)s |
| 590 | + is repeated consecutively a given number of times. |
588 | 591 |
|
589 | 592 | Parameters
|
590 | 593 | ----------
|
591 |
| - repeats : int |
592 |
| - This should be a non-negative integer. Repeating 0 times |
593 |
| - will return an empty array. |
| 594 | + repeats : int or array of ints |
| 595 | + The number of repetitions for each element. This should be a |
| 596 | + non-negative integer. Repeating 0 times will return an empty |
| 597 | + %(klass)s. |
| 598 | + *args |
| 599 | + Additional arguments have no effect but might be accepted for |
| 600 | + compatibility with numpy. |
| 601 | + **kwargs |
| 602 | + Additional keywords have no effect but might be accepted for |
| 603 | + compatibility with numpy. |
594 | 604 |
|
595 | 605 | Returns
|
596 | 606 | -------
|
597 |
| - repeated_array : ExtensionArray |
598 |
| - Same type as the input, with elements repeated `repeats` times. |
| 607 | + repeated_array : %(klass)s |
| 608 | + Newly created %(klass)s with repeated elements. |
599 | 609 |
|
600 | 610 | See Also
|
601 | 611 | --------
|
| 612 | + Series.repeat : Equivalent function for Series. |
| 613 | + Index.repeat : Equivalent function for Index. |
602 | 614 | numpy.repeat : Similar method for :class:`numpy.ndarray`.
|
603 | 615 | ExtensionArray.take : Take arbitrary positions.
|
| 616 | +
|
| 617 | + Examples |
| 618 | + -------- |
| 619 | + >>> cat = pd.Categorical(['a', 'b', 'c']) |
| 620 | + >>> cat |
| 621 | + [a, b, c] |
| 622 | + Categories (3, object): [a, b, c] |
| 623 | + >>> cat.repeat(2) |
| 624 | + [a, a, b, b, c, c] |
| 625 | + Categories (3, object): [a, b, c] |
| 626 | + >>> cat.repeat([1, 2, 3]) |
| 627 | + [a, b, b, c, c, c] |
| 628 | + Categories (3, object): [a, b, c] |
604 | 629 | """
|
605 |
| - if axis is not None: |
606 |
| - raise ValueError("'axis' must be None.") |
607 |
| - if repeats < 0: |
608 |
| - raise ValueError("negative repeats are not allowed.") |
| 630 | + |
| 631 | + @Substitution(klass='ExtensionArray') |
| 632 | + @Appender(_extension_array_shared_docs['repeat']) |
| 633 | + def repeat(self, repeats, *args, **kwargs): |
| 634 | + nv.validate_repeat(args, kwargs) |
609 | 635 | ind = np.arange(len(self)).repeat(repeats)
|
610 | 636 | return self.take(ind)
|
611 | 637 |
|
|
0 commit comments