Skip to content

Commit 48d0460

Browse files
jrebackjorisvandenbossche
authored andcommitted
DEPR: deprecate .select() in favor of .loc[] (#17633)
closes #12401
1 parent 81694dc commit 48d0460

File tree

10 files changed

+179
-53
lines changed

10 files changed

+179
-53
lines changed

doc/source/whatsnew/v0.21.0.txt

+25
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,31 @@ Deprecations
667667
- passing ``categories`` or ``ordered`` kwargs to :func:`Series.astype` is deprecated, in favor of passing a :ref:`CategoricalDtype <whatsnew_0210.enhancements.categorical_dtype>` (:issue:`17636`)
668668
- Passing a non-existant column in ``.to_excel(..., columns=)`` is deprecated and will raise a ``KeyError`` in the future (:issue:`17295`)
669669

670+
.. _whatsnew_0210.deprecations.select:
671+
672+
Series.select and DataFrame.select
673+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
674+
675+
The :meth:`Series.select` and :meth:`DataFrame.select` methods are deprecated in favor of using ``df.loc[labels.map(crit)]`` (:issue:`12401`)
676+
677+
.. ipython:: python
678+
679+
df = DataFrame({'A': [1, 2, 3]}, index=['foo', 'bar', 'baz'])
680+
681+
.. code-block:: ipython
682+
683+
In [3]: df.select(lambda x: x in ['bar', 'baz'])
684+
FutureWarning: select is deprecated and will be removed in a future release. You can use .loc[crit] as a replacement
685+
Out[3]:
686+
A
687+
bar 2
688+
baz 3
689+
690+
.. ipython:: python
691+
692+
df.loc[df.index.map(lambda x: x in ['bar', 'baz'])]
693+
694+
670695
.. _whatsnew_0210.deprecations.argmin_min:
671696

672697
Series.argmax and Series.argmin

pandas/core/common.py

+8
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,17 @@ def _apply_if_callable(maybe_callable, obj, **kwargs):
445445
"""
446446
Evaluate possibly callable input using obj and kwargs if it is callable,
447447
otherwise return as it is
448+
449+
Parameters
450+
----------
451+
maybe_callable : possibly a callable
452+
obj : NDFrame
453+
**kwargs
448454
"""
455+
449456
if callable(maybe_callable):
450457
return maybe_callable(obj, **kwargs)
458+
451459
return maybe_callable
452460

453461

pandas/core/generic.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,8 @@ def select(self, crit, axis=0):
23392339
"""
23402340
Return data corresponding to axis labels matching criteria
23412341
2342+
DEPRECATED: use df.loc[df.index.map(crit)] to select via labels
2343+
23422344
Parameters
23432345
----------
23442346
crit : function
@@ -2349,6 +2351,11 @@ def select(self, crit, axis=0):
23492351
-------
23502352
selection : type of caller
23512353
"""
2354+
warnings.warn("'select' is deprecated and will be removed in a "
2355+
"future release. You can use "
2356+
".loc[labels.map(crit)] as a replacement",
2357+
FutureWarning, stacklevel=2)
2358+
23522359
axis = self._get_axis_number(axis)
23532360
axis_name = self._get_axis_name(axis)
23542361
axis_values = self._get_axis(axis)
@@ -3101,7 +3108,7 @@ def filter(self, items=None, like=None, regex=None, axis=None):
31013108
31023109
See Also
31033110
--------
3104-
pandas.DataFrame.select
3111+
pandas.DataFrame.loc
31053112
31063113
Notes
31073114
-----
@@ -3120,20 +3127,23 @@ def filter(self, items=None, like=None, regex=None, axis=None):
31203127

31213128
if axis is None:
31223129
axis = self._info_axis_name
3123-
axis_name = self._get_axis_name(axis)
3124-
axis_values = self._get_axis(axis_name)
3130+
labels = self._get_axis(axis)
31253131

31263132
if items is not None:
3127-
return self.reindex(**{axis_name:
3128-
[r for r in items if r in axis_values]})
3133+
name = self._get_axis_name(axis)
3134+
return self.reindex(
3135+
**{name: [r for r in items if r in labels]})
31293136
elif like:
3130-
matchf = lambda x: (like in x if isinstance(x, string_types) else
3131-
like in str(x))
3132-
return self.select(matchf, axis=axis_name)
3137+
def f(x):
3138+
if not isinstance(x, string_types):
3139+
x = str(x)
3140+
return like in x
3141+
values = labels.map(f)
3142+
return self.loc(axis=axis)[values]
31333143
elif regex:
31343144
matcher = re.compile(regex)
3135-
return self.select(lambda x: matcher.search(str(x)) is not None,
3136-
axis=axis_name)
3145+
values = labels.map(lambda x: matcher.search(str(x)) is not None)
3146+
return self.loc(axis=axis)[values]
31373147
else:
31383148
raise TypeError('Must pass either `items`, `like`, or `regex`')
31393149

0 commit comments

Comments
 (0)