8000 Rename signature fix by TomAugspurger · Pull Request #17966 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Rename signature fix #17966

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

Merged
merged 13 commits into from
Oct 27, 2017
Prev Previous commit
Next Next commit
Signature rewriting
  • Loading branch information
TomAugspurger committed Oct 24, 2017
commit 44e01da43668051b2b3cbda469c9c90f087c6be6
12 changes: 11 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
from pandas import compat
from pandas.compat import PY36
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution
from pandas.util._decorators import (Appender, Substitution,
rewrite_axis_style_signature)
from pandas.util._validators import validate_bool_kwarg

from pandas.core.indexes.period import PeriodIndex
Expand Down Expand Up @@ -2917,6 +2918,12 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
broadcast_axis=broadcast_axis)

@Appender(_shared_docs['reindex'] % _shared_doc_kwargs)
@rewrite_axis_style_signature('labels', [('method', None),
('copy', True),
('level', None),
('fill_value', np.nan),
('limit', None),
('tolerance', None)])
def reindex(self, *args, **kwargs):
axes = self._validate_axis_style_args(args, kwargs, 'labels',
'reindex')
Expand All @@ -2933,6 +2940,9 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
method=method, level=level, copy=copy,
limit=limit, fill_value=fill_value)

@rewrite_axis_style_signature('mapper', [('copy', True),
('inplace', False),
('level', None)])
def rename(self, *args, **kwargs):
"""Alter axes labels.

Expand Down
18 changes: 17 additions & 1 deletion pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from __future__ import print_function

import inspect
import pytest

from datetime import datetime, timedelta

import numpy as np

from pandas.compat import lrange
from pandas.compat import lrange, PY2
from pandas import (DataFrame, Series, Index, MultiIndex,
RangeIndex, date_range, IntervalIndex,
to_datetime)
Expand Down Expand Up @@ -998,6 +999,21 @@ def test_ambiguous_warns(self):
with tm.assert_produces_warning(UserWarning):
df.rename({0: 10}, {"A": "B"})

@pytest.mark.skipif(PY2, reason="inspect.signature")
def test_rename_signature(self):
sig = inspect.signature(pd.DataFrame.rename)
parameters = set(sig.parameters)
assert parameters == {"self", "mapper", "index", "columns", "axis",
"inplace", "copy", "level"}

@pytest.mark.skipif(PY2, reason="inspect.signature")
def test_reindex_signature(self):
sig = inspect.signature(pd.DataFrame.reindex)
parameters = set(sig.parameters)
assert parameters == {"self", "labels", "index", "columns", "axis",
"limit", "copy", "level", "method",
"fill_value", "tolerance"}


class TestIntervalIndex(object):

Expand Down
28 changes: 27 additions & 1 deletion pandas/util/_decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pandas.compat import callable, signature
from pandas.compat import callable, signature, PY2
from pandas._libs.properties import cache_readonly # noqa
import inspect
import types
import warnings
from textwrap import dedent
Expand Down Expand Up @@ -122,6 +123,31 @@ def wrapper(*args, **kwargs):
# Substitution and Appender are derived from matplotlib.docstring (1.1.0)
# module http://matplotlib.org/users/license.html

def rewrite_axis_style_signature(name, extra_params):
def decorate(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)

if not PY2:
kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
params = [
inspect.Parameter('self', kind),
inspect.Parameter(name, kind, default=None),
inspect.Parameter('index', kind, default=None),
inspect.Parameter('columns', kind, default=None),
inspect.Parameter('axis', kind, default=None),
]

for pname, default in extra_params:
params.append(inspect.Parameter(pname, kind, default=default))

sig = inspect.Signature(params)

func.__signature__ = sig
Copy link
Contributor

Choose a reason for hiding this comment

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

cool that u can do this

return wrapper
return decorate


class Substitution(object):
"""
Expand Down
0