Skip to content

Commit 522df01

Browse files
committed
CLN: change getargspec -> signature
Change signature in compat to be able to return defaults, varargs, kwargs Closes #12171
1 parent 6100a76 commit 522df01

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

pandas/compat/__init__.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,30 @@ def bytes_to_str(b, encoding=None):
7171
return b.decode(encoding or 'utf-8')
7272

7373
def signature(f):
74-
return list(inspect.signature(f).parameters.keys())
74+
from collections import namedtuple
75+
sig = inspect.signature(f)
76+
args = [
77+
p.name for p in sig.parameters.values()
78+
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
79+
]
80+
varargs = [
81+
p.name for p in sig.parameters.values()
82+
if p.kind == inspect.Parameter.VAR_POSITIONAL
83+
]
84+
varargs = varargs[0] if varargs else None
85+
keywords = [
86+
p.name for p in sig.parameters.values()
87+
if p.kind == inspect.Parameter.VAR_KEYWORD
88+
]
89+
keywords = keywords[0] if keywords else None
90+
defaults = [
91+
p.default for p in sig.parameters.values()
92+
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
93+
and p.default is not p.empty
94+
] or None
95+
argspec = namedtuple('Signature',['args','defaults',
96+
'varargs','keywords'])
97+
return argspec(args,defaults,varargs,keywords)
7598

7699
# have to explicitly put builtins into the namespace
77100
range = range
@@ -110,7 +133,7 @@ def bytes_to_str(b, encoding='ascii'):
110133
return b
111134

112135
def signature(f):
113-
return inspect.getargspec(f).args
136+
return inspect.getargspec(f)
114137

115138
# import iterator versions of these functions
116139
range = xrange

pandas/tests/series/test_analytics.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# coding=utf-8
22
# pylint: disable-msg=E1101,W0612
33

4-
from inspect import getargspec
54
from itertools import product
65
from distutils.version import LooseVersion
76

@@ -465,7 +464,7 @@ def testit():
465464
self.assertRaises(ValueError, f, self.series, axis=1)
466465

467466
# Unimplemented numeric_only parameter.
468-
if 'numeric_only' in getargspec(f).args:
467+
if 'numeric_only' in compat.signature(f).args:
469468
self.assertRaisesRegexp(NotImplementedError, name, f,
470469
self.series, numeric_only=True)
471470

pandas/tests/test_panel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def wrapper(x):
201201
self.assertRaises(Exception, f, axis=obj.ndim)
202202

203203
# Unimplemented numeric_only parameter.
204-
if 'numeric_only' in signature(f):
204+
if 'numeric_only' in signature(f).args:
205205
self.assertRaisesRegexp(NotImplementedError, name, f,
206206
numeric_only=True)
207207

pandas/util/decorators.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pandas.compat import StringIO, callable
1+
from pandas.compat import StringIO, callable, signature
22
from pandas.lib import cache_readonly # noqa
33
import sys
44
import warnings
@@ -275,8 +275,7 @@ def make_signature(func):
275275
>>> print(_make_signature(f))
276276
a,b,c=2
277277
"""
278-
from inspect import getargspec
279-
spec = getargspec(func)
278+
spec = signature(func)
280279
if spec.defaults is None:
281280
n_wo_defaults = len(spec.args)
282281
defaults = ('',) * n_wo_defaults

0 commit comments

Comments
 (0)