Skip to content

Commit 2c44f57

Browse files
committed
ENH: add Panel.dropna, close #171
1 parent da5e2c1 commit 2c44f57

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pandas 0.8.1
3535
- Add ``radviz`` plot function (#1566)
3636
- Add ``multi_sparse`` option to ``set_printoptions`` to modify display of
3737
hierarchical indexes (#1538)
38+
- Add ``dropna`` method to Panel (#171)
3839

3940
**Improvements to existing features**
4041

pandas/core/panel.py

+35
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,41 @@ def reindex_like(self, other, method=None):
744744
return self.reindex(major=other.major_axis, items=other.items,
745745
minor=other.minor_axis, method=method)
746746

747+
def dropna(self, axis=0, how='any'):
748+
"""
749+
Drop 2D from panel, holding passed axis constant
750+
751+
Parameters
752+
----------
753+
axis : int, default 0
754+
Axis to hold constant. E.g. axis=1 will drop major_axis entries
755+
having a certain amount of NA data
756+
how : {'all', 'any'}, default 'any'
757+
'any': one or more values are NA in the DataFrame along the
758+
axis. For 'all' they all must be.
759+
760+
Returns
761+
-------
762+
dropped : Panel
763+
"""
764+
axis = self._get_axis_number(axis)
765+
766+
values = self.values
767+
mask = com.notnull(values)
768+
769+
for ax in reversed(sorted(set(range(3)) - set([axis]))):
770+
mask = mask.sum(ax)
771+
772+
per_slice = np.prod(values.shape[:axis] + values.shape[axis + 1:])
773+
774+
if how == 'all':
775+
cond = mask > 0
776+
else:
777+
cond = mask == per_slice
778+
779+
new_ax = self._get_axis(axis)[cond]
780+
return self.reindex_axis(new_ax, axis=axis)
781+
747782
def _combine(self, other, func, axis=0):
748783
if isinstance(other, Panel):
749784
return self._combine_panel(other, func)

pandas/tests/test_panel.py

+32
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,38 @@ def test_to_excel(self):
11081108
assert_frame_equal(df, recdf)
11091109
os.remove(path)
11101110

1111+
def test_dropna(self):
1112+
p = Panel(np.random.randn(4, 5, 6), major_axis=list('abcde'))
1113+
p.ix[:, ['b', 'd'], 0] = np.nan
1114+
1115+
result = p.dropna(axis=1)
1116+
exp = p.ix[:, ['a', 'c', 'e'], :]
1117+
assert_panel_equal(result, exp)
1118+
1119+
result = p.dropna(axis=1, how='all')
1120+
assert_panel_equal(result, p)
1121+
1122+
p.ix[:, ['b', 'd'], :] = np.nan
1123+
result = p.dropna(axis=1, how='all')
1124+
exp = p.ix[:, ['a', 'c', 'e'], :]
1125+
assert_panel_equal(result, exp)
1126+
1127+
p = Panel(np.random.randn(4, 5, 6), items=list('abcd'))
1128+
p.ix[['b'], :, 0] = np.nan
1129+
1130+
result = p.dropna()
1131+
exp = p.ix[['a', 'c', 'd']]
1132+
assert_panel_equal(result, exp)
1133+
1134+
result = p.dropna(how='all')
1135+
assert_panel_equal(result, p)
1136+
1137+
p.ix['b'] = np.nan
1138+
result = p.dropna(how='all')
1139+
exp = p.ix[['a', 'c', 'd']]
1140+
assert_panel_equal(result, exp)
1141+
1142+
11111143
class TestLongPanel(unittest.TestCase):
11121144
"""
11131145
LongPanel no longer exists, but...

0 commit comments

Comments
 (0)