Skip to content

Commit

Permalink
DEPR: deprecate .select() in favor of .loc[] (pandas-dev#17633)
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback authored and jorisvandenbossche committed Oct 4, 2017
1 parent 81694dc commit 48d0460
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 53 deletions.
25 changes: 25 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,31 @@ Deprecations
- 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`)
- Passing a non-existant column in ``.to_excel(..., columns=)`` is deprecated and will raise a ``KeyError`` in the future (:issue:`17295`)

.. _whatsnew_0210.deprecations.select:

Series.select and DataFrame.select
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The :meth:`Series.select` and :meth:`DataFrame.select` methods are deprecated in favor of using ``df.loc[labels.map(crit)]`` (:issue:`12401`)

.. ipython:: python

df = DataFrame({'A': [1, 2, 3]}, index=['foo', 'bar', 'baz'])

.. code-block:: ipython

In [3]: df.select(lambda x: x in ['bar', 'baz'])
FutureWarning: select is deprecated and will be removed in a future release. You can use .loc[crit] as a replacement
Out[3]:
A
bar 2
baz 3

.. ipython:: python

df.loc[df.index.map(lambda x: x in ['bar', 'baz'])]


.. _whatsnew_0210.deprecations.argmin_min:

Series.argmax and Series.argmin
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,17 @@ def _apply_if_callable(maybe_callable, obj, **kwargs):
"""
Evaluate possibly callable input using obj and kwargs if it is callable,
otherwise return as it is
Parameters
----------
maybe_callable : possibly a callable
obj : NDFrame
**kwargs
"""

if callable(maybe_callable):
return maybe_callable(obj, **kwargs)

return maybe_callable


Expand Down
30 changes: 20 additions & 10 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,8 @@ def select(self, crit, axis=0):
"""
Return data corresponding to axis labels matching criteria
DEPRECATED: use df.loc[df.index.map(crit)] to select via labels
Parameters
----------
crit : function
Expand All @@ -2349,6 +2351,11 @@ def select(self, crit, axis=0):
-------
selection : type of caller
"""
warnings.warn("'select' is deprecated and will be removed in a "
"future release. You can use "
".loc[labels.map(crit)] as a replacement",
FutureWarning, stacklevel=2)

axis = self._get_axis_number(axis)
axis_name = self._get_axis_name(axis)
axis_values = self._get_axis(axis)
Expand Down Expand Up @@ -3101,7 +3108,7 @@ def filter(self, items=None, like=None, regex=None, axis=None):
See Also
--------
pandas.DataFrame.select
pandas.DataFrame.loc
Notes
-----
Expand All @@ -3120,20 +3127,23 @@ def filter(self, items=None, like=None, regex=None, axis=None):

if axis is None:
axis = self._info_axis_name
axis_name = self._get_axis_name(axis)
axis_values = self._get_axis(axis_name)
labels = self._get_axis(axis)

if items is not None:
return self.reindex(**{axis_name:
[r for r in items if r in axis_values]})
name = self._get_axis_name(axis)
return self.reindex(
**{name: [r for r in items if r in labels]})
elif like:
matchf = lambda x: (like in x if isinstance(x, string_types) else
like in str(x))
return self.select(matchf, axis=axis_name)
def f(x):
if not isinstance(x, string_types):
x = str(x)
return like in x
values = labels.map(f)
return self.loc(axis=axis)[values]
elif regex:
matcher = re.compile(regex)
return self.select(lambda x: matcher.search(str(x)) is not None,
axis=axis_name)
values = labels.map(lambda x: matcher.search(str(x)) is not None)
return self.loc(axis=axis)[values]
else:
raise TypeError('Must pass either `items`, `like`, or `regex`')

Expand Down
Loading

0 comments on commit 48d0460

Please sign in to comment.