Skip to content

Commit 94280f0

Browse files
authored
BUG: DataFrame.dropna must not allow to set both how= and thresh= parameters (#46644)
1 parent ff51b2f commit 94280f0

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ Missing
571571
- Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``downcast`` keyword not being respected in some cases where there are no NA values present (:issue:`45423`)
572572
- Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with :class:`IntervalDtype` and incompatible value raising instead of casting to a common (usually object) dtype (:issue:`45796`)
573573
- Bug in :meth:`DataFrame.interpolate` with object-dtype column not returning a copy with ``inplace=False`` (:issue:`45791`)
574-
-
574+
- Bug in :meth:`DataFrame.dropna` allows to set both ``how`` and ``thresh`` incompatible arguments (:issue:`46575`)
575575

576576
MultiIndex
577577
^^^^^^^^^^

pandas/core/frame.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
properties,
4343
)
4444
from pandas._libs.hashtable import duplicated
45-
from pandas._libs.lib import no_default
45+
from pandas._libs.lib import (
46+
NoDefault,
47+
no_default,
48+
)
4649
from pandas._typing import (
4750
AggFuncType,
4851
AnyArrayLike,
@@ -6110,8 +6113,8 @@ def notnull(self) -> DataFrame:
61106113
def dropna(
61116114
self,
61126115
axis: Axis = 0,
6113-
how: str = "any",
6114-
thresh=None,
6116+
how: str | NoDefault = no_default,
6117+
thresh: int | NoDefault = no_default,
61156118
subset: IndexLabel = None,
61166119
inplace: bool = False,
61176120
):
@@ -6143,7 +6146,7 @@ def dropna(
61436146
* 'all' : If all values are NA, drop that row or column.
61446147
61456148
thresh : int, optional
6146-
Require that many non-NA values.
6149+
Require that many non-NA values. Cannot be combined with how.
61476150
subset : column label or sequence of labels, optional
61486151
Labels along other axis to consider, e.g. if you are dropping rows
61496152
these would be a list of columns to include.
@@ -6218,6 +6221,14 @@ def dropna(
62186221
name toy born
62196222
1 Batman Batmobile 1940-04-25
62206223
"""
6224+
if (how is not no_default) and (thresh is not no_default):
6225+
raise TypeError(
6226+
"You cannot set both the how and thresh arguments at the same time."
6227+
)
6228+
6229+
if how is no_default:
6230+
how = "any"
6231+
62216232
inplace = validate_bool_kwarg(inplace, "inplace")
62226233
if isinstance(axis, (tuple, list)):
62236234
# GH20987
@@ -6238,7 +6249,7 @@ def dropna(
62386249
raise KeyError(np.array(subset)[check].tolist())
62396250
agg_obj = self.take(indices, axis=agg_axis)
62406251

6241-
if thresh is not None:
6252+
if thresh is not no_default:
62426253
count = agg_obj.count(axis=agg_axis)
62436254
mask = count >= thresh
62446255
elif how == "any":
@@ -6248,10 +6259,8 @@ def dropna(
62486259
# faster equivalent to 'agg_obj.count(agg_axis) > 0'
62496260
mask = notna(agg_obj).any(axis=agg_axis, bool_only=False)
62506261
else:
6251-
if how is not None:
6262+
if how is not no_default:
62526263
raise ValueError(f"invalid how option: {how}")
6253-
else:
6254-
raise TypeError("must specify how or thresh")
62556264

62566265
if np.all(mask):
62576266
result = self.copy()

pandas/tests/frame/methods/test_dropna.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ def test_dropna_corner(self, float_frame):
158158
msg = "invalid how option: foo"
159159
with pytest.raises(ValueError, match=msg):
160160
float_frame.dropna(how="foo")
161-
msg = "must specify how or thresh"
162-
with pytest.raises(TypeError, match=msg):
163-
float_frame.dropna(how=None)
164161
# non-existent column - 8303
165162
with pytest.raises(KeyError, match=r"^\['X'\]$"):
166163
float_frame.dropna(subset=["A", "X"])
@@ -274,3 +271,16 @@ def test_no_nans_in_frame(self, axis):
274271
expected = df.copy()
275272
result = df.dropna(axis=axis)
276273
tm.assert_frame_equal(result, expected, check_index_type=True)
274+
275+
def test_how_thresh_param_incompatible(self):
276+
# GH46575
277+
df = DataFrame([1, 2, pd.NA])
278+
msg = "You cannot set both the how and thresh arguments at the same time"
279+
with pytest.raises(TypeError, match=msg):
280+
df.dropna(how="all", thresh=2)
281+
282+
with pytest.raises(TypeError, match=msg):
283+
df.dropna(how="any", thresh=2)
284+
285+
with pytest.raises(TypeError, match=msg):
286+
df.dropna(how=None, thresh=None)

0 commit comments

Comments
 (0)