Skip to content

Commit 4f75357

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 e0d3061 commit 4f75357

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
@@ -482,6 +482,7 @@ Other
482482
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
483483
- Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`)
484484
- Bug in :func:`unique` on :class:`Index` not always returning :class:`Index` (:issue:`57043`)
485+
- Bug in :func:`mask` to handle NaN values in condition of function. (:issue:`56844`)
485486
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which caused an exception when using NumPy attributes via ``@`` notation, e.g., ``df.eval("@np.floor(a)")``. (:issue:`58041`)
486487
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which did not allow to use ``tan`` function. (:issue:`55091`)
487488
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)

pandas/core/generic.py

+3
Original file line numberDiff line numberDiff line change
@@ -9999,6 +9999,9 @@ def mask(
99999999
cond = common.apply_if_callable(cond, self)
1000010000
other = common.apply_if_callable(other, self)
1000110001

10002+
if isinstance(cond, ABCDataFrame | ABCSeries):
10003+
cond = cond.fillna(False)
10004+
1000210005
# see gh-21891
1000310006
if not hasattr(cond, "__invert__"):
1000410007
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)