Skip to content

Commit 6cf0a4e

Browse files
committed
Merge pull request #8384 from mcjcode/master
non-existent columns in DataFrame.dropna subset argument now raises KeyError
2 parents 0ffef1d + d7f6021 commit 6cf0a4e

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,4 +979,4 @@ Bug Fixes
979979
- Bug in DataFrame terminal display: Setting max_column/max_rows to zero did not trigger auto-resizing of dfs to fit terminal width/height (:issue:`7180`).
980980
- Bug in OLS where running with "cluster" and "nw_lags" parameters did not work correctly, but also did not throw an error
981981
(:issue:`5884').
982-
982+
- Bug in ``DataFrame.dropna`` that interpreted non-existent columns in the subset argument as the 'last column' (:issue:`8303`)

pandas/core/frame.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2552,7 +2552,11 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None,
25522552
agg_obj = self
25532553
if subset is not None:
25542554
ax = self._get_axis(agg_axis)
2555-
agg_obj = self.take(ax.get_indexer_for(subset),axis=agg_axis)
2555+
indices = ax.get_indexer_for(subset)
2556+
check = indices == -1
2557+
if check.any():
2558+
raise KeyError(list(np.compress(check,subset)))
2559+
agg_obj = self.take(indices,axis=agg_axis)
25562560

25572561
count = agg_obj.count(axis=agg_axis)
25582562

pandas/tests/test_frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -7200,6 +7200,8 @@ def test_dropna_corner(self):
72007200
# bad input
72017201
self.assertRaises(ValueError, self.frame.dropna, how='foo')
72027202
self.assertRaises(TypeError, self.frame.dropna, how=None)
7203+
# non-existent column - 8303
7204+
self.assertRaises(KeyError, self.frame.dropna, subset=['A','X'])
72037205

72047206
def test_dropna_multiple_axes(self):
72057207
df = DataFrame([[1, np.nan, 2, 3],

0 commit comments

Comments
 (0)