Skip to content

Commit

Permalink
DEPR: deprecate .select() in favor of .loc(axis=)[]
Browse files Browse the repository at this point in the history
closes #12401
  • Loading branch information
jreback committed Oct 3, 2017
1 parent 8e89cb3 commit dbd2473
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 60 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
11 changes: 10 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,22 @@ def _get_callable_name(obj):
return None


def _apply_if_callable(maybe_callable, obj, **kwargs):
def _apply_if_callable(maybe_callable, obj, axis=None, **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
axis : int, optional
**kwargs
"""

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

return maybe_callable


Expand Down
36 changes: 23 additions & 13 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 Expand Up @@ -5756,7 +5766,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
inplace = validate_bool_kwarg(inplace, 'inplace')

# align the cond to same shape as myself
cond = com._apply_if_callable(cond, self)
cond = com._apply_if_callable(cond, self, axis=axis)
if isinstance(cond, NDFrame):
cond, _ = cond.align(self, join='right', broadcast_axis=1)
else:
Expand Down Expand Up @@ -5997,7 +6007,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
try_cast=False, raise_on_error=True):

other = com._apply_if_callable(other, self)
other = com._apply_if_callable(other, self, axis=axis)
return self._where(cond, other, inplace, axis, level, try_cast,
raise_on_error)

Expand All @@ -6008,7 +6018,7 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
try_cast=False, raise_on_error=True):

inplace = validate_bool_kwarg(inplace, 'inplace')
cond = com._apply_if_callable(cond, self)
cond = com._apply_if_callable(cond, self, axis=axis)

return self.where(~cond, other=other, inplace=inplace, axis=axis,
level=level, try_cast=try_cast,
Expand Down
Loading

0 comments on commit dbd2473

Please sign in to comment.