Skip to content

Commit 103f7d3

Browse files
cswarthjreback
authored andcommitted
DOC: Add example usage to DataFrame.filter
Author: Chris Warth <[email protected]> Closes #12399 from cswarth/doc/df_filter and squashes the following commits: f48e9ff [Chris Warth] DOC: Add example usage to DataFrame.filter
1 parent 2061e9e commit 103f7d3

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ Other API changes
286286

287287
- ``Float64Index.astype(int)`` will now raise ``ValueError`` if ``Float64Index`` contains ``NaN`` values (:issue:`13149`)
288288
- ``TimedeltaIndex.astype(int)`` and ``DatetimeIndex.astype(int)`` will now return ``Int64Index`` instead of ``np.array`` (:issue:`13209`)
289+
- ``.filter()`` enforces mutual exclusion of the keyword arguments. (:issue:`12399`)
289290

290291
.. _whatsnew_0182.deprecations:
291292

pandas/core/generic.py

+49-7
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,11 @@ def _reindex_axis(self, new_index, fill_method, axis, copy):
23572357

23582358
def filter(self, items=None, like=None, regex=None, axis=None):
23592359
"""
2360-
Restrict the info axis to set of items or wildcard
2360+
Subset rows or columns of dataframe according to labels in
2361+
the specified index.
2362+
2363+
Note that this routine does not filter a dataframe on its
2364+
contents. The filter is applied to the labels of the index.
23612365
23622366
Parameters
23632367
----------
@@ -2367,19 +2371,57 @@ def filter(self, items=None, like=None, regex=None, axis=None):
23672371
Keep info axis where "arg in col == True"
23682372
regex : string (regular expression)
23692373
Keep info axis with re.search(regex, col) == True
2370-
axis : int or None
2371-
The axis to filter on. By default this is the info axis. The "info
2372-
axis" is the axis that is used when indexing with ``[]``. For
2373-
example, ``df = DataFrame({'a': [1, 2, 3, 4]]}); df['a']``. So,
2374-
the ``DataFrame`` columns are the info axis.
2374+
axis : int or string axis name
2375+
The axis to filter on. By default this is the info axis,
2376+
'index' for Series, 'columns' for DataFrame
2377+
2378+
Returns
2379+
-------
2380+
same type as input object
2381+
2382+
Examples
2383+
--------
2384+
>>> df
2385+
one two three
2386+
mouse 1 2 3
2387+
rabbit 4 5 6
2388+
2389+
>>> # select columns by name
2390+
>>> df.filter(items=['one', 'three'])
2391+
one three
2392+
mouse 1 3
2393+
rabbit 4 6
2394+
2395+
>>> # select columns by regular expression
2396+
>>> df.filter(regex='e$', axis=1)
2397+
one three
2398+
mouse 1 3
2399+
rabbit 4 6
2400+
2401+
>>> # select rows containing 'bbi'
2402+
>>> df.filter(like='bbi', axis=0)
2403+
one two three
2404+
rabbit 4 5 6
2405+
2406+
See Also
2407+
--------
2408+
pandas.DataFrame.select
23752409
23762410
Notes
23772411
-----
2378-
Arguments are mutually exclusive, but this is not checked for
2412+
The ``items``, ``like``, and ``regex`` parameters are
2413+
enforced to be mutually exclusive.
23792414
2415+
``axis`` defaults to the info axis that is used when indexing
2416+
with ``[]``.
23802417
"""
23812418
import re
23822419

2420+
nkw = sum([x is not None for x in [items, like, regex]])
2421+
if nkw > 1:
2422+
raise TypeError('Keyword arguments `items`, `like`, or `regex` '
2423+
'are mutually exclusive')
2424+
23832425
if axis is None:
23842426
axis = self._info_axis_name
23852427
axis_name = self._get_axis_name(axis)

pandas/tests/frame/test_axis_select_reindex.py

+16
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,24 @@ def test_filter(self):
661661
assert_frame_equal(filtered, expected)
662662

663663
# pass in None
664+
with assertRaisesRegexp(TypeError, 'Must pass'):
665+
self.frame.filter()
664666
with assertRaisesRegexp(TypeError, 'Must pass'):
665667
self.frame.filter(items=None)
668+
with assertRaisesRegexp(TypeError, 'Must pass'):
669+
self.frame.filter(axis=1)
670+
671+
# test mutually exclusive arguments
672+
with assertRaisesRegexp(TypeError, 'mutually exclusive'):
673+
self.frame.filter(items=['one', 'three'], regex='e$', like='bbi')
674+
with assertRaisesRegexp(TypeError, 'mutually exclusive'):
675+
self.frame.filter(items=['one', 'three'], regex='e$', axis=1)
676+
with assertRaisesRegexp(TypeError, 'mutually exclusive'):
677+
self.frame.filter(items=['one', 'three'], regex='e$')
678+
with assertRaisesRegexp(TypeError, 'mutually exclusive'):
679+
self.frame.filter(items=['one', 'three'], like='bbi', axis=0)
680+
with assertRaisesRegexp(TypeError, 'mutually exclusive'):
681+
self.frame.filter(items=['one', 'three'], like='bbi')
666682

667683
# objects
668684
filtered = self.mixed_frame.filter(like='foo')

0 commit comments

Comments
 (0)