Skip to content

Commit 9cebdf8

Browse files
committed
Fix pandas-dev#56844: function mask handling None values as condition
This commit addresses an issue where using pandas.NA in conjunction with the mask() method resulted in unexpected behavior. The problem arose when comparing a Series containing pandas.NA with a condition within mask(), causing inconsistencies in the output. This fix ensures that pandas.NA behaves consistently with False in logical operations within mask().
1 parent 6f39c4f commit 9cebdf8

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ Performance improvements
329329
Bug fixes
330330
~~~~~~~~~
331331
- Fixed bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
332+
- Fixed bug in :func:`mask` to handle NaN values in condition of function. (:issue:`56844`)
332333
- Fixed bug in :meth:`.DataFrameGroupBy.median` where nat values gave an incorrect result. (:issue:`57926`)
333334
- Fixed bug in :meth:`DataFrame.cumsum` which was raising ``IndexError`` if dtype is ``timedelta64[ns]`` (:issue:`57956`)
334335
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)

pandas/core/generic.py

+3
Original file line numberDiff line numberDiff line change
@@ -9960,6 +9960,9 @@ def mask(
99609960
cond = common.apply_if_callable(cond, self)
99619961
other = common.apply_if_callable(other, self)
99629962

9963+
if isinstance(cond, ABCDataFrame | ABCSeries):
9964+
cond = cond.fillna(False)
9965+
99639966
# see gh-21891
99649967
if not hasattr(cond, "__invert__"):
99659968
cond = np.array(cond)

pandas/tests/frame/indexing/test_mask.py

+8
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,11 @@ def test_mask_inplace_no_other():
150150
df.mask(cond, inplace=True)
151151
expected = DataFrame({"a": [np.nan, 2], "b": ["x", np.nan]})
152152
tm.assert_frame_equal(df, expected)
153+
154+
155+
def test_mask_with_NA():
156+
df = DataFrame({"A": [0, 1, 2]})
157+
cond = Series([-1, 1, None]).convert_dtypes() < 0
158+
result = df.mask(cond, other=100)
159+
expected = DataFrame({"A": [100, 1, 2]})
160+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)