-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Deprecate non-keyword arguments in mask #41580
Changes from 5 commits
06dcad6
6963374
e406b0f
6e72701
f7be5e0
106ca0c
154135b
fcc9b70
383202b
4840f66
4a3e82b
9f7adc6
7861647
31c98c1
698389a
3480092
7b25587
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,8 @@ def test_mask(self): | |
|
||
other = DataFrame(np.random.randn(5, 3)) | ||
rs = df.where(cond, other) | ||
tm.assert_frame_equal(rs, df.mask(df <= 0, other)) | ||
tm.assert_frame_equal(rs, df.mask(~cond, other)) | ||
tm.assert_frame_equal(rs, df.mask(df <= 0, other=other)) | ||
tm.assert_frame_equal(rs, df.mask(~cond, other=other)) | ||
|
||
# see GH#21891 | ||
df = DataFrame([1, 2]) | ||
|
@@ -51,7 +51,7 @@ def test_mask_inplace(self): | |
return_value = rdf.where(cond, -df, inplace=True) | ||
assert return_value is None | ||
tm.assert_frame_equal(rdf, df.where(cond, -df)) | ||
tm.assert_frame_equal(rdf, df.mask(~cond, -df)) | ||
tm.assert_frame_equal(rdf, df.mask(~cond, other=-df)) | ||
|
||
def test_mask_edge_case_1xN_frame(self): | ||
# GH#4071 | ||
|
@@ -63,22 +63,22 @@ def test_mask_edge_case_1xN_frame(self): | |
def test_mask_callable(self): | ||
# GH#12533 | ||
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) | ||
result = df.mask(lambda x: x > 4, lambda x: x + 1) | ||
result = df.mask(lambda x: x > 4, other=lambda x: x + 1) | ||
exp = DataFrame([[1, 2, 3], [4, 6, 7], [8, 9, 10]]) | ||
tm.assert_frame_equal(result, exp) | ||
tm.assert_frame_equal(result, df.mask(df > 4, df + 1)) | ||
tm.assert_frame_equal(result, df.mask(df > 4, other=df + 1)) | ||
|
||
# return ndarray and scalar | ||
result = df.mask(lambda x: (x % 2 == 0).values, lambda x: 99) | ||
result = df.mask(lambda x: (x % 2 == 0).values, other=lambda x: 99) | ||
exp = DataFrame([[1, 99, 3], [99, 5, 99], [7, 99, 9]]) | ||
tm.assert_frame_equal(result, exp) | ||
tm.assert_frame_equal(result, df.mask(df % 2 == 0, 99)) | ||
tm.assert_frame_equal(result, df.mask(df % 2 == 0, other=99)) | ||
|
||
# chain | ||
result = (df + 2).mask(lambda x: x > 8, lambda x: x + 10) | ||
result = (df + 2).mask(lambda x: x > 8, other=lambda x: x + 10) | ||
exp = DataFrame([[3, 4, 5], [6, 7, 8], [19, 20, 21]]) | ||
tm.assert_frame_equal(result, exp) | ||
tm.assert_frame_equal(result, (df + 2).mask((df + 2) > 8, (df + 2) + 10)) | ||
tm.assert_frame_equal(result, (df + 2).mask((df + 2) > 8, other=(df + 2) + 10)) | ||
|
||
def test_mask_dtype_bool_conversion(self): | ||
# GH#3733 | ||
|
@@ -90,6 +90,18 @@ def test_mask_dtype_bool_conversion(self): | |
result = bools.mask(mask) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_mask_pos_args_deprecation(self): | ||
# https://github.com/pandas-dev/pandas/issues/41485 | ||
df = DataFrame(np.random.randn(5, 5)) | ||
cond = df > 0 | ||
other = DataFrame(np.random.randn(5, 3)) | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you check the warning message? see |
||
result = df.mask(cond, other, False) | ||
|
||
expected = df.mask(cond, other=other, inplace=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you construct |
||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_mask_try_cast_deprecated(frame_or_series): | ||
|
||
|
@@ -101,7 +113,7 @@ def test_mask_try_cast_deprecated(frame_or_series): | |
|
||
with tm.assert_produces_warning(FutureWarning): | ||
# try_cast keyword deprecated | ||
obj.mask(mask, -1, try_cast=True) | ||
obj.mask(mask, other=-1, try_cast=True) | ||
|
||
|
||
def test_mask_stringdtype(): | ||
|
@@ -115,7 +127,7 @@ def test_mask_stringdtype(): | |
{"A": ["this", "that"]}, index=["id2", "id3"], dtype=StringDtype() | ||
) | ||
filter_ser = Series([False, True, True, False]) | ||
result = df.mask(filter_ser, filtered_df) | ||
result = df.mask(filter_ser, other=filtered_df) | ||
|
||
expected = DataFrame( | ||
{"A": [NA, "this", "that", NA]}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ def test_mask(): | |
tm.assert_series_equal(rs, rs2) | ||
|
||
rs = s.where(~cond, -s) | ||
rs2 = s.mask(cond, -s) | ||
rs2 = s.mask(cond, other=-s) | ||
tm.assert_series_equal(rs, rs2) | ||
|
||
cond = Series([True, False, False, True, False], index=s.index) | ||
|
@@ -32,18 +32,18 @@ def test_mask(): | |
tm.assert_series_equal(rs, rs2) | ||
|
||
rs = s2.where(~cond[:3], -s2) | ||
rs2 = s2.mask(cond[:3], -s2) | ||
rs2 = s2.mask(cond[:3], other=-s2) | ||
tm.assert_series_equal(rs, rs2) | ||
|
||
msg = "Array conditional must be same shape as self" | ||
with pytest.raises(ValueError, match=msg): | ||
s.mask(1) | ||
with pytest.raises(ValueError, match=msg): | ||
s.mask(cond[:3].values, -s) | ||
s.mask(cond[:3].values, other=-s) | ||
|
||
# dtype changes | ||
s = Series([1, 2, 3, 4]) | ||
result = s.mask(s > 2, np.nan) | ||
result = s.mask(s > 2, other=np.nan) | ||
expected = Series([1, 2, np.nan, np.nan]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
@@ -65,8 +65,8 @@ def test_mask_inplace(): | |
tm.assert_series_equal(rs, s.mask(cond)) | ||
|
||
rs = s.copy() | ||
rs.mask(cond, -s, inplace=True) | ||
tm.assert_series_equal(rs, s.mask(cond, -s)) | ||
rs.mask(cond, other=-s, inplace=True) | ||
tm.assert_series_equal(rs, s.mask(cond, other=-s)) | ||
|
||
|
||
def test_mask_stringdtype(): | ||
|
@@ -78,11 +78,23 @@ def test_mask_stringdtype(): | |
) | ||
filtered_ser = Series(["this", "that"], index=["id2", "id3"], dtype=StringDtype()) | ||
filter_ser = Series([False, True, True, False]) | ||
result = ser.mask(filter_ser, filtered_ser) | ||
result = ser.mask(filter_ser, other=filtered_ser) | ||
|
||
expected = Series( | ||
[NA, "this", "that", NA], | ||
index=["id1", "id2", "id3", "id4"], | ||
dtype=StringDtype(), | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_mask_pos_args_deprecation(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comments as for the DataFrame test |
||
# https://github.com/pandas-dev/pandas/issues/41485 | ||
s = Series(np.random.randn(6)) | ||
cond = s > 0 | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
result = s.mask(cond, np.nan, False) | ||
|
||
expected = s.mask(cond, other=np.nan, inplace=False) | ||
tm.assert_series_equal(result, expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you construct it explicitly instead of passing in randomly generated numbers? a small dataframe with a single column should be fine