Skip to content

BUG: Properly handle lists for .mask #21934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ For situations where you need an ``ndarray`` of ``Interval`` objects, use
:meth:`numpy.asarray` or ``idx.astype(object)``.

.. ipython:: python

np.asarray(idx)
idx.values.astype(object)

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

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7941,6 +7941,10 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
inplace = validate_bool_kwarg(inplace, 'inplace')
cond = com._apply_if_callable(cond, self)

# see gh-21891
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this in .where itself

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do what exactly in .where ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i c we are using ~ here, ok then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cond could be a callable , this patch seems wouldn't work.

Copy link
Member Author

@gfyoung gfyoung Jul 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would. Because com._apply_if_callable (right above) returns an ndarray. Passing in callable for cond is covered (and passing) in the tests.

if not hasattr(cond, "__invert__"):
cond = np.array(cond)

return self.where(~cond, other=other, inplace=inplace, axis=axis,
level=level, try_cast=try_cast,
errors=errors)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2966,6 +2966,13 @@ def test_mask(self):
assert_frame_equal(rs, df.mask(df <= 0, other))
assert_frame_equal(rs, df.mask(~cond, other))

# see gh-21891
df = DataFrame([1, 2])
res = df.mask([[True], [False]])

exp = DataFrame([np.nan, 2])
tm.assert_frame_equal(res, exp)

def test_mask_inplace(self):
# GH8801
df = DataFrame(np.random.randn(5, 3))
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/indexing/test_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,13 @@ def test_mask():
expected = Series([1, 2, np.nan, np.nan])
assert_series_equal(result, expected)

# see gh-21891
s = Series([1, 2])
res = s.mask([True, False])

exp = Series([np.nan, 2])
tm.assert_series_equal(res, exp)


def test_mask_inplace():
s = Series(np.random.randn(5))
Expand Down