Skip to content

Commit fa5f315

Browse files
committed
ENH: DataFrame.dropna can take a tuple/list of axes. close #924
1 parent 9c9f4cb commit fa5f315

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pandas 0.9.0
5252
methods (#1689)
5353
- If index_label=False in DataFrame.to_csv, do not print fields/commas in the
5454
text output. Results in easier importing into R (#1583)
55+
- Can pass tuple/list of axes to DataFrame.dropna to simplify repeated calls
56+
(dropping both columns and rows) (#924)
5557

5658
**API Changes**
5759

pandas/core/frame.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -2595,7 +2595,8 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None):
25952595
25962596
Parameters
25972597
----------
2598-
axis : {0, 1}
2598+
axis : {0, 1}, or tuple/list thereof
2599+
Pass tuple or list to drop on multiple axes
25992600
how : {'any', 'all'}
26002601
any : if any NA values are present, drop that label
26012602
all : if all values are NA, drop that label
@@ -2609,6 +2610,13 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None):
26092610
-------
26102611
dropped : DataFrame
26112612
"""
2613+
if isinstance(axis, (tuple, list)):
2614+
result = self
2615+
for ax in axis:
2616+
result = result.dropna(how=how, thresh=thresh,
2617+
subset=subset, axis=ax)
2618+
return result
2619+
26122620
axis_name = self._get_axis_name(axis)
26132621

26142622
if axis == 0:

pandas/tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -3980,6 +3980,19 @@ def test_dropna_corner(self):
39803980
self.assertRaises(ValueError, self.frame.dropna, how='foo')
39813981
self.assertRaises(ValueError, self.frame.dropna, how=None)
39823982

3983+
def test_dropna_multiple_axes(self):
3984+
df = DataFrame([[1, np.nan, 2, 3],
3985+
[4, np.nan, 5, 6],
3986+
[np.nan, np.nan, np.nan, np.nan],
3987+
[7, np.nan, 8, 9]])
3988+
3989+
result = df.dropna(how='all', axis=[0, 1])
3990+
result2 = df.dropna(how='all', axis=(0, 1))
3991+
expected = df.dropna(how='all').dropna(how='all', axis=1)
3992+
3993+
assert_frame_equal(result, expected)
3994+
assert_frame_equal(result2, expected)
3995+
39833996
def test_drop_duplicates(self):
39843997
df = DataFrame({'AAA' : ['foo', 'bar', 'foo', 'bar',
39853998
'foo', 'bar', 'bar', 'foo'],

0 commit comments

Comments
 (0)