Skip to content

Commit ce87d06

Browse files
gfyoungjreback
authored andcommitted
BUG: Properly handle lists for .mask (#21934)
Closes gh-21891.
1 parent 537b65c commit ce87d06

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ For situations where you need an ``ndarray`` of ``Interval`` objects, use
160160
:meth:`numpy.asarray` or ``idx.astype(object)``.
161161

162162
.. ipython:: python
163-
163+
164164
np.asarray(idx)
165165
idx.values.astype(object)
166166

@@ -487,6 +487,7 @@ Reshaping
487487
- Bug in :func:`pandas.concat` when joining resampled DataFrames with timezone aware index (:issue:`13783`)
488488
- Bug in :meth:`Series.combine_first` with ``datetime64[ns, tz]`` dtype which would return tz-naive result (:issue:`21469`)
489489
- Bug in :meth:`Series.where` and :meth:`DataFrame.where` with ``datetime64[ns, tz]`` dtype (:issue:`21546`)
490+
- Bug in :meth:`Series.mask` and :meth:`DataFrame.mask` with ``list`` conditionals (:issue:`21891`)
490491
-
491492
-
492493

pandas/core/generic.py

+4
Original file line numberDiff line numberDiff line change
@@ -7941,6 +7941,10 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
79417941
inplace = validate_bool_kwarg(inplace, 'inplace')
79427942
cond = com._apply_if_callable(cond, self)
79437943

7944+
# see gh-21891
7945+
if not hasattr(cond, "__invert__"):
7946+
cond = np.array(cond)
7947+
79447948
return self.where(~cond, other=other, inplace=inplace, axis=axis,
79457949
level=level, try_cast=try_cast,
79467950
errors=errors)

pandas/tests/frame/test_indexing.py

+7
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,13 @@ def test_mask(self):
29662966
assert_frame_equal(rs, df.mask(df <= 0, other))
29672967
assert_frame_equal(rs, df.mask(~cond, other))
29682968

2969+
# see gh-21891
2970+
df = DataFrame([1, 2])
2971+
res = df.mask([[True], [False]])
2972+
2973+
exp = DataFrame([np.nan, 2])
2974+
tm.assert_frame_equal(res, exp)
2975+
29692976
def test_mask_inplace(self):
29702977
# GH8801
29712978
df = DataFrame(np.random.randn(5, 3))

pandas/tests/series/indexing/test_boolean.py

+7
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,13 @@ def test_mask():
617617
expected = Series([1, 2, np.nan, np.nan])
618618
assert_series_equal(result, expected)
619619

620+
# see gh-21891
621+
s = Series([1, 2])
622+
res = s.mask([True, False])
623+
624+
exp = Series([np.nan, 2])
625+
tm.assert_series_equal(res, exp)
626+
620627

621628
def test_mask_inplace():
622629
s = Series(np.random.randn(5))

0 commit comments

Comments
 (0)