Skip to content
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

API: Simplify repeat signature #24447

Merged
merged 1 commit into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
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
13 changes: 5 additions & 8 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,9 @@ def factorize(self, na_sentinel=-1):
The number of repetitions for each element. This should be a
non-negative integer. Repeating 0 times will return an empty
%(klass)s.
*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.
axis : None
Must be ``None``. Has no effect but is accepted for compatibility
with numpy.

Returns
-------
Expand Down Expand Up @@ -640,8 +637,8 @@ def factorize(self, na_sentinel=-1):

@Substitution(klass='ExtensionArray')
@Appender(_extension_array_shared_docs['repeat'])
def repeat(self, repeats, *args, **kwargs):
nv.validate_repeat(args, kwargs)
def repeat(self, repeats, axis=None):
nv.validate_repeat(tuple(), dict(axis=axis))
Copy link
Member Author

Choose a reason for hiding this comment

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

I think these could be written more explicitly as:

if axis is not None:
    raise ValueError('...')

I don't think nv.validate_repeat is doing more than that but could be missing something. The advantage to using nv.validate_repeat simply being that the error message is kept in a single place instead of being explicitly written in each method.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is exactly why we have it this way

ind = np.arange(len(self)).repeat(repeats)
return self.take(ind)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2402,8 +2402,8 @@ def describe(self):

@Substitution(klass='Categorical')
@Appender(_extension_array_shared_docs['repeat'])
def repeat(self, repeats, *args, **kwargs):
nv.validate_repeat(args, kwargs)
def repeat(self, repeats, axis=None):
nv.validate_repeat(tuple(), dict(axis=axis))
codes = self._codes.repeat(repeats)
return self._constructor(values=codes, dtype=self.dtype, fastpath=True)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,8 @@ def to_tuples(self, na_tuple=True):
return tuples

@Appender(_extension_array_shared_docs['repeat'] % _shared_docs_kwargs)
def repeat(self, repeats, *args, **kwargs):
nv.validate_repeat(args, kwargs)
def repeat(self, repeats, axis=None):
nv.validate_repeat(tuple(), dict(axis=axis))
left_repeat = self.left.repeat(repeats)
right_repeat = self.right.repeat(repeats)
return self._shallow_copy(left=left_repeat, right=right_repeat)
Expand Down
13 changes: 5 additions & 8 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,12 +846,9 @@ def _assert_take_fillable(self, values, indices, allow_fill=True,
The number of repetitions for each element. This should be a
non-negative integer. Repeating 0 times will return an empty
%(klass)s.
*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.
axis : None
Must be ``None``. Has no effect but is accepted for compatibility
with numpy.

Returns
-------
Expand All @@ -875,8 +872,8 @@ def _assert_take_fillable(self, values, indices, allow_fill=True,
"""

@Appender(_index_shared_docs['repeat'] % _index_doc_kwargs)
def repeat(self, repeats, *args, **kwargs):
nv.validate_repeat(args, kwargs)
def repeat(self, repeats, axis=None):
nv.validate_repeat(tuple(), dict(axis=axis))
return self._shallow_copy(self._values.repeat(repeats))

# --------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ def isin(self, values):
return algorithms.isin(self.asi8, values.asi8)

@Appender(_index_shared_docs['repeat'] % _index_doc_kwargs)
def repeat(self, repeats, *args, **kwargs):
nv.validate_repeat(args, kwargs)
def repeat(self, repeats, axis=None):
nv.validate_repeat(tuple(), dict(axis=axis))
freq = self.freq if is_period_dtype(self) else None
return self._shallow_copy(self.asi8.repeat(repeats), freq=freq)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1858,8 +1858,8 @@ def argsort(self, *args, **kwargs):
return self.values.argsort(*args, **kwargs)

@Appender(_index_shared_docs['repeat'] % _index_doc_kwargs)
def repeat(self, repeats, *args, **kwargs):
nv.validate_repeat(args, kwargs)
def repeat(self, repeats, axis=None):
nv.validate_repeat(tuple(), dict(axis=axis))
return MultiIndex(levels=self.levels,
codes=[level_codes.view(np.ndarray).repeat(repeats)
for level_codes in self.codes],
Expand Down
13 changes: 5 additions & 8 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ def _set_values(self, key, value):
self._data = self._data.setitem(indexer=key, value=value)
self._maybe_update_cacher()

def repeat(self, repeats, *args, **kwargs):
def repeat(self, repeats, axis=None):
"""
Repeat elements of a Series.

Expand All @@ -1050,12 +1050,9 @@ def repeat(self, repeats, *args, **kwargs):
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.
axis : None
Must be ``None``. Has no effect but is accepted for compatibility
with numpy.

Returns
-------
Expand Down Expand Up @@ -1092,7 +1089,7 @@ def repeat(self, repeats, *args, **kwargs):
2 c
dtype: object
"""
nv.validate_repeat(args, kwargs)
nv.validate_repeat(tuple(), dict(axis=axis))
new_index = self.index.repeat(repeats)
new_values = self._values.repeat(repeats)
return self._constructor(new_values,
Expand Down