Skip to content

Commit a29bba6

Browse files
committed
Standardize function signatures
Standardize the following function signatures: 1) repeat(reps, *args, **kwargs) 2) searchsorted(key, side='left', sorter=None) Closes gh-12662.
1 parent 52241a7 commit a29bba6

File tree

11 files changed

+72
-23
lines changed

11 files changed

+72
-23
lines changed

doc/source/whatsnew/v0.20.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ Other API Changes
5353
Deprecations
5454
^^^^^^^^^^^^
5555

56+
- ``Series.repeat()`` has deprecated the ``reps`` parameter in favor of ``repeats`` (:issue:`12662`)
57+
- ``Index.repeat()`` and ``MultiIndex.repeat()`` have deprecated the ``n`` parameter in favor of ``repeats`` (:issue:`12662`)
58+
- ``Categorical.searchsorted()`` and ``Series.searchsorted()`` have deprecated the ``v`` parameter in favor of ``key`` (:issue:`12662`)
5659

5760

5861

pandas/core/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1091,12 +1091,12 @@ def factorize(self, sort=False, na_sentinel=-1):
10911091
"""Find indices where elements should be inserted to maintain order.
10921092
10931093
Find the indices into a sorted %(klass)s `self` such that, if the
1094-
corresponding elements in `v` were inserted before the indices, the
1094+
corresponding elements in `key` were inserted before the indices, the
10951095
order of `self` would be preserved.
10961096
10971097
Parameters
10981098
----------
1099-
%(value)s : array_like
1099+
key : array_like
11001100
Values to insert into `self`.
11011101
side : {'left', 'right'}, optional
11021102
If 'left', the index of the first suitable location found is given.
@@ -1109,7 +1109,7 @@ def factorize(self, sort=False, na_sentinel=-1):
11091109
Returns
11101110
-------
11111111
indices : array of ints
1112-
Array of insertion points with the same shape as `v`.
1112+
Array of insertion points with the same shape as `key`.
11131113
11141114
See Also
11151115
--------

pandas/core/categorical.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1076,17 +1076,18 @@ def memory_usage(self, deep=False):
10761076
"""
10771077
return self._codes.nbytes + self._categories.memory_usage(deep=deep)
10781078

1079-
@Substitution(klass='Categorical', value='v')
1079+
@Substitution(klass='Categorical')
10801080
@Appender(_shared_docs['searchsorted'])
1081-
def searchsorted(self, v, side='left', sorter=None):
1081+
@deprecate_kwarg(old_arg_name='v', new_arg_name='key')
1082+
def searchsorted(self, key, side='left', sorter=None):
10821083
if not self.ordered:
10831084
raise ValueError("Categorical not ordered\nyou can use "
10841085
".as_ordered() to change the Categorical to an "
10851086
"ordered one")
10861087

10871088
from pandas.core.series import Series
10881089
values_as_codes = self.categories.values.searchsorted(
1089-
Series(v).values, side=side)
1090+
Series(key).values, side=side)
10901091

10911092
return self.codes.searchsorted(values_as_codes, sorter=sorter)
10921093

pandas/core/series.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -831,18 +831,19 @@ def _set_values(self, key, value):
831831
self._data = self._data.setitem(indexer=key, value=value)
832832
self._maybe_update_cacher()
833833

834-
def repeat(self, reps, *args, **kwargs):
834+
@deprecate_kwarg(old_arg_name='reps', new_arg_name='repeats')
835+
def repeat(self, repeats, *args, **kwargs):
835836
"""
836837
Repeat elements of an Series. Refer to `numpy.ndarray.repeat`
837-
for more information about the `reps` argument.
838+
for more information about the `repeats` argument.
838839
839840
See also
840841
--------
841842
numpy.ndarray.repeat
842843
"""
843844
nv.validate_repeat(args, kwargs)
844-
new_index = self.index.repeat(reps)
845-
new_values = self._values.repeat(reps)
845+
new_index = self.index.repeat(repeats)
846+
new_values = self._values.repeat(repeats)
846847
return self._constructor(new_values,
847848
index=new_index).__finalize__(self)
848849

@@ -1514,12 +1515,13 @@ def dot(self, other):
15141515
else: # pragma: no cover
15151516
raise TypeError('unsupported type: %s' % type(other))
15161517

1517-
@Substitution(klass='Series', value='v')
1518+
@Substitution(klass='Series')
15181519
@Appender(base._shared_docs['searchsorted'])
1519-
def searchsorted(self, v, side='left', sorter=None):
1520+
@deprecate_kwarg(old_arg_name='v', new_arg_name='key')
1521+
def searchsorted(self, key, side='left', sorter=None):
15201522
if sorter is not None:
15211523
sorter = _ensure_platform_int(sorter)
1522-
return self._values.searchsorted(Series(v)._values,
1524+
return self._values.searchsorted(Series(key)._values,
15231525
side=side, sorter=sorter)
15241526

15251527
# -------------------------------------------------------------------

pandas/indexes/base.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -535,17 +535,18 @@ def tolist(self):
535535
"""
536536
return list(self.values)
537537

538-
def repeat(self, n, *args, **kwargs):
538+
@deprecate_kwarg(old_arg_name='n', new_arg_name='repeats')
539+
def repeat(self, repeats, *args, **kwargs):
539540
"""
540541
Repeat elements of an Index. Refer to `numpy.ndarray.repeat`
541-
for more information about the `n` argument.
542+
for more information about the `repeats` argument.
542543
543544
See also
544545
--------
545546
numpy.ndarray.repeat
546547
"""
547548
nv.validate_repeat(args, kwargs)
548-
return self._shallow_copy(self._values.repeat(n))
549+
return self._shallow_copy(self._values.repeat(repeats))
549550

550551
def where(self, cond, other=None):
551552
"""

pandas/indexes/multi.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,11 @@ def append(self, other):
11661166
def argsort(self, *args, **kwargs):
11671167
return self.values.argsort(*args, **kwargs)
11681168

1169-
def repeat(self, n, *args, **kwargs):
1169+
@deprecate_kwarg(old_arg_name='n', new_arg_name='repeats')
1170+
def repeat(self, repeats, *args, **kwargs):
11701171
nv.validate_repeat(args, kwargs)
11711172
return MultiIndex(levels=self.levels,
1172-
labels=[label.view(np.ndarray).repeat(n)
1173+
labels=[label.view(np.ndarray).repeat(repeats)
11731174
for label in self.labels], names=self.names,
11741175
sortorder=self.sortorder, verify_integrity=False)
11751176

pandas/tests/indexes/test_base.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Float64Index, Int64Index,
1818
CategoricalIndex, DatetimeIndex, TimedeltaIndex,
1919
PeriodIndex)
20+
from pandas.core.index import _get_combined_index
2021
from pandas.util.testing import assert_almost_equal
2122
from pandas.compat.numpy import np_datetime64_compat
2223

@@ -1963,8 +1964,19 @@ def test_dropna(self):
19631964
with tm.assertRaisesRegexp(ValueError, msg):
19641965
pd.Index([1, 2, 3]).dropna(how='xxx')
19651966

1967+
def test_get_combined_index(self):
1968+
result = _get_combined_index([])
1969+
tm.assert_index_equal(result, Index([]))
19661970

1967-
def test_get_combined_index():
1968-
from pandas.core.index import _get_combined_index
1969-
result = _get_combined_index([])
1970-
tm.assert_index_equal(result, Index([]))
1971+
def test_repeat(self):
1972+
repeats = 2
1973+
idx = pd.Index([1, 2, 3])
1974+
expected = pd.Index([1, 1, 2, 2, 3, 3])
1975+
1976+
result = idx.repeat(repeats)
1977+
tm.assert_index_equal(result, expected)
1978+
1979+
with tm.assert_produces_warning(FutureWarning,
1980+
check_stacklevel=False):
1981+
result = idx.repeat(n=repeats)
1982+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/test_multi.py

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def test_repeat(self):
9797
numbers, names.repeat(reps)], names=names)
9898
tm.assert_index_equal(m.repeat(reps), expected)
9999

100+
with tm.assert_produces_warning(FutureWarning,
101+
check_stacklevel=False):
102+
tm.assert_index_equal(m.repeat(n=reps), expected)
103+
100104
def test_numpy_repeat(self):
101105
reps = 2
102106
numbers = [1, 2, 3]

pandas/tests/series/test_analytics.py

+19
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,11 @@ def test_repeat(self):
13631363
exp = Series(s.values.repeat(5), index=s.index.values.repeat(5))
13641364
assert_series_equal(reps, exp)
13651365

1366+
with tm.assert_produces_warning(FutureWarning,
1367+
check_stacklevel=False):
1368+
result = s.repeat(reps=5)
1369+
assert_series_equal(result, exp)
1370+
13661371
to_rep = [2, 3, 4]
13671372
reps = s.repeat(to_rep)
13681373
exp = Series(s.values.repeat(to_rep),
@@ -1378,6 +1383,20 @@ def test_numpy_repeat(self):
13781383
msg = "the 'axis' parameter is not supported"
13791384
tm.assertRaisesRegexp(ValueError, msg, np.repeat, s, 2, axis=0)
13801385

1386+
def test_searchsorted(self):
1387+
s = Series([1, 2, 3])
1388+
1389+
idx = s.searchsorted(1, side='left')
1390+
tm.assert_numpy_array_equal(idx, np.array([0], dtype=np.intp))
1391+
1392+
idx = s.searchsorted(1, side='right')
1393+
tm.assert_numpy_array_equal(idx, np.array([1], dtype=np.intp))
1394+
1395+
with tm.assert_produces_warning(FutureWarning,
1396+
check_stacklevel=False):
1397+
idx = s.searchsorted(v=1, side='left')
1398+
tm.assert_numpy_array_equal(idx, np.array([0], dtype=np.intp))
1399+
13811400
def test_searchsorted_numeric_dtypes_scalar(self):
13821401
s = Series([1, 2, 90, 1000, 3e9])
13831402
r = s.searchsorted(30)

pandas/tests/test_categorical.py

+6
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,12 @@ def test_searchsorted(self):
15931593
self.assert_numpy_array_equal(res, exp)
15941594
self.assert_numpy_array_equal(res, chk)
15951595

1596+
with tm.assert_produces_warning(FutureWarning,
1597+
check_stacklevel=False):
1598+
res = c1.searchsorted(v=['bread'])
1599+
exp = np.array([1], dtype=np.intp)
1600+
tm.assert_numpy_array_equal(res, exp)
1601+
15961602
def test_deprecated_labels(self):
15971603
# TODO: labels is deprecated and should be removed in 0.18 or 2017,
15981604
# whatever is earlier

pandas/tseries/tdi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ def _partial_td_slice(self, key, freq, use_lhs=True, use_rhs=True):
785785
# # try to find a the dates
786786
# return (lhs_mask & rhs_mask).nonzero()[0]
787787

788-
@Substitution(klass='TimedeltaIndex', value='key')
788+
@Substitution(klass='TimedeltaIndex')
789789
@Appender(_shared_docs['searchsorted'])
790790
def searchsorted(self, key, side='left', sorter=None):
791791
if isinstance(key, (np.ndarray, Index)):

0 commit comments

Comments
 (0)