Skip to content
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

Merged
merged 17 commits into from May 27, 2021

Conversation

ShreyDixit
Copy link
Contributor

@ShreyDixit ShreyDixit commented May 20, 2021

@simonjayhawkins simonjayhawkins added the Deprecate Functionality to remove in pandas label May 21, 2021
@MarcoGorelli MarcoGorelli self-requested a review May 23, 2021 17:31
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Thanks @ShreyDixit

This is off to a good start! Just a few comments, and some pre-commit checks to fixup

@@ -9236,6 +9237,7 @@ def where(
name="mask",
name_other="where",
)
@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "cond"])
Copy link
Member

Choose a reason for hiding this comment

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

let's make this version=None

Copy link
Member

Choose a reason for hiding this comment

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

also, let's allow other to be positional as well, just like in where https://github.com/pandas-dev/pandas/pull/41523/files

Comment on lines 103 to 104
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_frame_equal(df.mask(cond, other), df.mask(cond, other=other))
Copy link
Member

Choose a reason for hiding this comment

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

can you write this as

with tm.assert_produces_warnnig...
    result = ...
expected = ...
tm.assert_frame_equal(result, expected)

and construct expected explicitly

Comment on lines 100 to 101
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_series_equal(s.mask(cond, np.nan), s.mask(cond, other=np.nan))
Copy link
Member

Choose a reason for hiding this comment

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

same comments as for the DataFrame test

Comment on lines 99 to 103
msg = (
r"Starting with Pandas version 2\.0 all arguments of mask except for the "
r"arguments 'self' and 'cond' will be keyword-only"
)
Copy link
Member

Choose a reason for hiding this comment

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

the message will need updating, there's been some changes in deprecate_nonkeyword_arguments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have made the changes and removed the message entirely.

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Generally looks good, just a few comments - also, please remove path_to_file.xlsx

@@ -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))
Copy link
Member

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

with tm.assert_produces_warning(FutureWarning):
result = df.mask(cond, other, False)

expected = df.mask(cond, other=other, inplace=False)
Copy link
Member

Choose a reason for hiding this comment

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

can you construct expected explicitly (otherwise both result and expected go through .mask)

cond = df > 0
other = DataFrame(np.random.randn(5, 3))

with tm.assert_produces_warning(FutureWarning):
Copy link
Member

Choose a reason for hiding this comment

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

can you check the warning message? see msg = ... from the example PR


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():
Copy link
Member

Choose a reason for hiding this comment

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

same comments as for the DataFrame test

@jreback
Copy link
Contributor

jreback commented May 24, 2021

hmm other is prob ok as a positional as well here

@MarcoGorelli
Copy link
Member

hmm other is prob ok as a positional as well here

OK, thanks for stepping in - I'll ask for it to be positional in the where PR too then

cond = df % 2 == 0

msg = (
r"In a future version of pandas all arguments of NDFrame.mask except for "
Copy link
Member

Choose a reason for hiding this comment

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

should be DataFrame.mask

cond = s % 2 == 0

msg = (
r"In a future version of pandas all arguments of NDFrame.mask except for "
Copy link
Member

Choose a reason for hiding this comment

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

Series.mask

@@ -678,6 +678,7 @@ Deprecations
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`)
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
- Deprecated passing arguments (apart from ``cond`` and ``other``) as positional in :meth:`DataFrame.mask` (:issue:`41485`)
Copy link
Member

Choose a reason for hiding this comment

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

mention Series.mask as well

@jreback
Copy link
Contributor

jreback commented May 27, 2021

@ShreyDixit can u rebase

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @ShreyDixit lgtm pending green

@jreback jreback merged commit beabf0a into pandas-dev:master May 27, 2021
@jreback
Copy link
Contributor

jreback commented May 27, 2021

thanks @ShreyDixit

@ShreyDixit ShreyDixit deleted the mask_pos_args_deprecation branch May 27, 2021 15:35
@ShreyDixit
Copy link
Contributor Author

Thank you everyone for you guidance

TLouf pushed a commit to TLouf/pandas that referenced this pull request Jun 1, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Deprecate Functionality to remove in pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants