Skip to content

Commit

Permalink
DOC/CLN: clean-up shared_docs in generic.py (pandas-dev#20074)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored Oct 2, 2018
1 parent 9caf048 commit 1102a33
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 58 deletions.
9 changes: 6 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3629,7 +3629,8 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
fill_axis=fill_axis,
broadcast_axis=broadcast_axis)

@Appender(_shared_docs['reindex'] % _shared_doc_kwargs)
@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.reindex.__doc__)
@rewrite_axis_style_signature('labels', [('method', None),
('copy', True),
('level', None),
Expand Down Expand Up @@ -4479,7 +4480,8 @@ def f(vals):
# ----------------------------------------------------------------------
# Sorting

@Appender(_shared_docs['sort_values'] % _shared_doc_kwargs)
@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.sort_values.__doc__)
def sort_values(self, by, axis=0, ascending=True, inplace=False,
kind='quicksort', na_position='last'):
inplace = validate_bool_kwarg(inplace, 'inplace')
Expand Down Expand Up @@ -4521,7 +4523,8 @@ def sort_values(self, by, axis=0, ascending=True, inplace=False,
else:
return self._constructor(new_data).__finalize__(self)

@Appender(_shared_docs['sort_index'] % _shared_doc_kwargs)
@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.sort_index.__doc__)
def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
kind='quicksort', na_position='last', sort_remaining=True,
by=None):
Expand Down
65 changes: 18 additions & 47 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,8 @@ def _set_axis(self, axis, labels):
self._data.set_axis(axis, labels)
self._clear_item_cache()

_shared_docs['transpose'] = """
def transpose(self, *args, **kwargs):
"""
Permute the dimensions of the %(klass)s
Parameters
Expand All @@ -663,9 +664,6 @@ def _set_axis(self, axis, labels):
y : same as input
"""

@Appender(_shared_docs['transpose'] % _shared_doc_kwargs)
def transpose(self, *args, **kwargs):

# construct the args
axes, kwargs = self._construct_axes_from_arguments(args, kwargs,
require_all=True)
Expand Down Expand Up @@ -965,23 +963,20 @@ def swaplevel(self, i=-2, j=-1, axis=0):
# ----------------------------------------------------------------------
# Rename

# TODO: define separate funcs for DataFrame, Series and Panel so you can
# get completion on keyword arguments.
_shared_docs['rename'] = """
def rename(self, *args, **kwargs):
"""
Alter axes input function or functions. Function / dict values must be
unique (1-to-1). Labels not contained in a dict / Series will be left
as-is. Extra labels listed don't throw an error. Alternatively, change
``Series.name`` with a scalar value (Series only).
Parameters
----------
%(optional_mapper)s
%(axes)s : scalar, list-like, dict-like or function, optional
Scalar or list-like will alter the ``Series.name`` attribute,
and raise on DataFrame or Panel.
dict-like or functions are transformations to apply to
that axis' values
%(optional_axis)s
copy : boolean, default True
Also copy underlying data
inplace : boolean, default False
Expand Down Expand Up @@ -1069,12 +1064,6 @@ def swaplevel(self, i=-2, j=-1, axis=0):
See the :ref:`user guide <basics.rename>` for more.
"""

@Appender(_shared_docs['rename'] % dict(axes='axes keywords for this'
' object', klass='NDFrame',
optional_mapper='',
optional_axis=''))
def rename(self, *args, **kwargs):
axes, kwargs = self._construct_axes_from_arguments(args, kwargs)
copy = kwargs.pop('copy', True)
inplace = kwargs.pop('inplace', False)
Expand Down Expand Up @@ -1127,8 +1116,6 @@ def f(x):
else:
return result.__finalize__(self)

rename.__doc__ = _shared_docs['rename']

def rename_axis(self, mapper, axis=0, copy=True, inplace=False):
"""
Alter the name of the index or columns.
Expand Down Expand Up @@ -3024,7 +3011,8 @@ def __delitem__(self, key):
except KeyError:
pass

_shared_docs['_take'] = """
def _take(self, indices, axis=0, is_copy=True):
"""
Return the elements in the given *positional* indices along an axis.
This means that we are not indexing according to actual values in
Expand Down Expand Up @@ -3055,9 +3043,6 @@ def __delitem__(self, key):
numpy.ndarray.take
numpy.take
"""

@Appender(_shared_docs['_take'])
def _take(self, indices, axis=0, is_copy=True):
self._consolidate_inplace()

new_data = self._data.take(indices,
Expand All @@ -3072,7 +3057,8 @@ def _take(self, indices, axis=0, is_copy=True):

return result

_shared_docs['take'] = """
def take(self, indices, axis=0, convert=None, is_copy=True, **kwargs):
"""
Return the elements in the given *positional* indices along an axis.
This means that we are not indexing according to actual values in
Expand Down Expand Up @@ -3155,9 +3141,6 @@ class max_speed
1 monkey mammal NaN
3 lion mammal 80.5
"""

@Appender(_shared_docs['take'])
def take(self, indices, axis=0, convert=None, is_copy=True, **kwargs):
if convert is not None:
msg = ("The 'convert' parameter is deprecated "
"and will be removed in a future version.")
Expand Down Expand Up @@ -3580,7 +3563,9 @@ def add_suffix(self, suffix):
mapper = {self._info_axis_name: f}
return self.rename(**mapper)

_shared_docs['sort_values'] = """
def sort_values(self, by=None, axis=0, ascending=True, inplace=False,
kind='quicksort', na_position='last'):
"""
Sort by the values along either axis
Parameters
Expand Down Expand Up @@ -3665,17 +3650,12 @@ def add_suffix(self, suffix):
0 A 2 0
1 A 1 1
"""

def sort_values(self, by=None, axis=0, ascending=True, inplace=False,
kind='quicksort', na_position='last'):
"""
NOT IMPLEMENTED: do not call this method, as sorting values is not
supported for Panel objects and will raise an error.
"""
raise NotImplementedError("sort_values has not been implemented "
"on Panel or Panel4D objects.")

_shared_docs['sort_index'] = """
def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
kind='quicksort', na_position='last', sort_remaining=True):
"""
Sort object by labels (along an axis)
Parameters
Expand Down Expand Up @@ -3703,10 +3683,6 @@ def sort_values(self, by=None, axis=0, ascending=True, inplace=False,
-------
sorted_obj : %(klass)s
"""

@Appender(_shared_docs['sort_index'] % dict(axes="axes", klass="NDFrame"))
def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
kind='quicksort', na_position='last', sort_remaining=True):
inplace = validate_bool_kwarg(inplace, 'inplace')
axis = self._get_axis_number(axis)
axis_name = self._get_axis_name(axis)
Expand All @@ -3724,7 +3700,8 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
new_axis = labels.take(sort_index)
return self.reindex(**{axis_name: new_axis})

_shared_docs['reindex'] = """
def reindex(self, *args, **kwargs):
"""
Conform %(klass)s to new index with optional filling logic, placing
NA/NaN in locations having no value in the previous index. A new object
is produced unless the new index is equivalent to the current one and
Expand Down Expand Up @@ -3920,14 +3897,8 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
-------
reindexed : %(klass)s
"""

# TODO: Decide if we care about having different examples for different
# kinds

@Appender(_shared_docs['reindex'] % dict(axes="axes", klass="NDFrame",
optional_labels="",
optional_axis=""))
def reindex(self, *args, **kwargs):
# TODO: Decide if we care about having different examples for different
# kinds

# construct the args
axes, kwargs = self._construct_axes_from_arguments(args, kwargs)
Expand Down
16 changes: 13 additions & 3 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,8 @@ def _wrap_result(self, result, axis):

return self._construct_return_type(result, axes)

@Appender(_shared_docs['reindex'] % _shared_doc_kwargs)
@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.reindex.__doc__)
def reindex(self, *args, **kwargs):
major = kwargs.pop("major", None)
minor = kwargs.pop('minor', None)
Expand All @@ -1236,7 +1237,8 @@ def reindex(self, *args, **kwargs):
kwargs.pop('labels', None)
return super(Panel, self).reindex(**kwargs)

@Appender(_shared_docs['rename'] % _shared_doc_kwargs)
@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.rename.__doc__)
def rename(self, items=None, major_axis=None, minor_axis=None, **kwargs):
major_axis = (major_axis if major_axis is not None else
kwargs.pop('major', None))
Expand All @@ -1253,7 +1255,8 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
copy=copy, limit=limit,
fill_value=fill_value)

@Appender(_shared_docs['transpose'] % _shared_doc_kwargs)
@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.transpose.__doc__)
def transpose(self, *args, **kwargs):
# check if a list of axes was passed in instead as a
# single *args element
Expand Down Expand Up @@ -1536,6 +1539,13 @@ def _extract_axis(self, data, axis=0, intersect=False):

return ensure_index(index)

def sort_values(self, *args, **kwargs):
"""
NOT IMPLEMENTED: do not call this method, as sorting values is not
supported for Panel objects and will raise an error.
"""
super(Panel, self).sort_values(*args, **kwargs)


Panel._setup_axes(axes=['items', 'major_axis', 'minor_axis'], info_axis=0,
stat_axis=1, aliases={'major': 'major_axis',
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3496,7 +3496,8 @@ def rename(self, index=None, **kwargs):
return self._set_name(index, inplace=kwargs.get('inplace'))
return super(Series, self).rename(index=index, **kwargs)

@Appender(generic._shared_docs['reindex'] % _shared_doc_kwargs)
@Substitution(**_shared_doc_kwargs)
@Appender(generic.NDFrame.reindex.__doc__)
def reindex(self, index=None, **kwargs):
return super(Series, self).reindex(index=index, **kwargs)

Expand Down Expand Up @@ -3680,7 +3681,7 @@ def memory_usage(self, index=True, deep=False):
v += self.index.memory_usage(deep=deep)
return v

@Appender(generic._shared_docs['_take'])
@Appender(generic.NDFrame._take.__doc__)
def _take(self, indices, axis=0, is_copy=False):

indices = ensure_platform_int(indices)
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pandas.core.indexes.base as ibase
import pandas.core.ops as ops
import pandas._libs.index as libindex
from pandas.util._decorators import Appender
from pandas.util._decorators import Appender, Substitution

from pandas.core.sparse.array import (
make_sparse, SparseArray,
Expand Down Expand Up @@ -563,7 +563,8 @@ def copy(self, deep=True):
return self._constructor(new_data, sparse_index=self.sp_index,
fill_value=self.fill_value).__finalize__(self)

@Appender(generic._shared_docs['reindex'] % _shared_doc_kwargs)
@Substitution(**_shared_doc_kwargs)
@Appender(generic.NDFrame.reindex.__doc__)
def reindex(self, index=None, method=None, copy=True, limit=None,
**kwargs):

Expand Down Expand Up @@ -592,7 +593,7 @@ def sparse_reindex(self, new_index):
sparse_index=new_index,
fill_value=self.fill_value).__finalize__(self)

@Appender(generic._shared_docs['take'])
@Appender(generic.NDFrame.take.__doc__)
def take(self, indices, axis=0, convert=None, *args, **kwargs):
if convert is not None:
msg = ("The 'convert' parameter is deprecated "
Expand Down

0 comments on commit 1102a33

Please sign in to comment.