-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLN: Make repeat method consistent #24395
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,12 +20,13 @@ | |
ABCDatetimeIndex, ABCInterval, ABCIntervalIndex, ABCPeriodIndex, ABCSeries) | ||
from pandas.core.dtypes.missing import isna, notna | ||
|
||
from pandas.core.arrays.base import ( | ||
ExtensionArray, _extension_array_shared_docs) | ||
from pandas.core.arrays.categorical import Categorical | ||
import pandas.core.common as com | ||
from pandas.core.config import get_option | ||
from pandas.core.indexes.base import Index, ensure_index | ||
|
||
from . import Categorical, ExtensionArray | ||
|
||
_VALID_CLOSED = {'left', 'right', 'both', 'neither'} | ||
_interval_shared_docs = {} | ||
|
||
|
@@ -1000,35 +1001,11 @@ def to_tuples(self, na_tuple=True): | |
tuples = np.where(~self.isna(), tuples, np.nan) | ||
return tuples | ||
|
||
def repeat(self, repeats, **kwargs): | ||
""" | ||
Repeat elements of an IntervalArray. | ||
|
||
Returns a new IntervalArray where each element of the current | ||
IntervalArray is repeated consecutively a given number of times. | ||
|
||
Parameters | ||
---------- | ||
repeats : int | ||
The number of repetitions for each element. | ||
|
||
**kwargs | ||
Additional keywords have no effect but might be accepted for | ||
compatibility with numpy. | ||
|
||
Returns | ||
------- | ||
IntervalArray | ||
Newly created IntervalArray with repeated elements. | ||
|
||
See Also | ||
-------- | ||
Index.repeat : Equivalent function for Index. | ||
Series.repeat : Equivalent function for Series. | ||
numpy.repeat : Underlying implementation. | ||
""" | ||
left_repeat = self.left.repeat(repeats, **kwargs) | ||
right_repeat = self.right.repeat(repeats, **kwargs) | ||
@Appender(_extension_array_shared_docs['repeat'] % _shared_docs_kwargs) | ||
def repeat(self, repeats, *args, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The base class implementation works for |
||
nv.validate_repeat(args, kwargs) | ||
left_repeat = self.left.repeat(repeats) | ||
right_repeat = self.right.repeat(repeats) | ||
return self._shallow_copy(left=left_repeat, right=right_repeat) | ||
|
||
_interval_shared_docs['overlaps'] = """ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1038,12 +1038,61 @@ def _set_values(self, key, value): | |
|
||
def repeat(self, repeats, *args, **kwargs): | ||
""" | ||
Repeat elements of an Series. Refer to `numpy.ndarray.repeat` | ||
for more information about the `repeats` argument. | ||
Repeat elements of a Series. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any way to re-use the doc-string you defined for EA (meaning here and for Index), maybe make it even more generic and parametrize on the types? (ok that we are duplicating code, but the more can share the better). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had the same thought but was unsure how to do it in a clean way. I think you could split the docstring to reuse to everything but the See Also and Examples section, then separately define only those sections for EA/Index/Series. Seems a bit convoluted though; certainly interested to hear if there's a better way. |
||
|
||
Returns a new Series where each element of the current Series | ||
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 | ||
Series. | ||
*args | ||
Additional arguments have no effect but might be accepted for | ||
compatibility with numpy. | ||
**kwargs | ||
Additional keywords have no effect but might be accepted for | ||
compatibility with numpy. | ||
|
||
Returns | ||
------- | ||
repeated_series : Series | ||
Newly created Series with repeated elements. | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.repeat | ||
Index.repeat : Equivalent function for Index. | ||
numpy.repeat : Similar method for :class:`numpy.ndarray`. | ||
|
||
Examples | ||
-------- | ||
>>> s = pd.Series(['a', 'b', 'c']) | ||
>>> s | ||
0 a | ||
1 b | ||
2 c | ||
dtype: object | ||
>>> s.repeat(2) | ||
0 a | ||
0 a | ||
1 b | ||
1 b | ||
2 c | ||
2 c | ||
dtype: object | ||
>>> s.repeat(3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you mean to have 2 very similar examples? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was mimicking the existing documentation for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is ok, just thinking if this can be made simpler. |
||
0 a | ||
0 a | ||
0 a | ||
1 b | ||
1 b | ||
1 b | ||
2 c | ||
2 c | ||
2 c | ||
dtype: object | ||
""" | ||
nv.validate_repeat(args, kwargs) | ||
new_index = self.index.repeat(repeats) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,22 +292,6 @@ def test_validate_inplace(self): | |
with pytest.raises(ValueError): | ||
cat.sort_values(inplace=value) | ||
|
||
def test_repeat(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deleted as these appear to be testing the same thing as the base class tests in |
||
# GH10183 | ||
cat = Categorical(["a", "b"], categories=["a", "b"]) | ||
exp = Categorical(["a", "a", "b", "b"], categories=["a", "b"]) | ||
res = cat.repeat(2) | ||
tm.assert_categorical_equal(res, exp) | ||
|
||
def test_numpy_repeat(self): | ||
cat = Categorical(["a", "b"], categories=["a", "b"]) | ||
exp = Categorical(["a", "a", "b", "b"], categories=["a", "b"]) | ||
tm.assert_categorical_equal(np.repeat(cat, 2), exp) | ||
|
||
msg = "the 'axis' parameter is not supported" | ||
with pytest.raises(ValueError, match=msg): | ||
np.repeat(cat, 2, axis=1) | ||
|
||
def test_isna(self): | ||
exp = np.array([False, False, True]) | ||
c = Categorical(["a", "b", np.nan]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that
repeat
can be an array of ints in the numpy implementation, and this appears to work for the pandas implementation as well. Added tests for this behavior in places it didn't previously exist.