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

DEPR: Raise FutureWarning about raising an error in __array__ when copy=False cannot be honored #60395

Open
wants to merge 13 commits into
base: 2.3.x
Choose a base branch
from

Conversation

KevsterAmp
Copy link
Contributor

@KevsterAmp KevsterAmp commented Nov 22, 2024

@KevsterAmp KevsterAmp changed the base branch from main to 2.3.x November 22, 2024 13:44
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks, looking good!

If you remove the error, you will have to update some tests that are now checking for an error (change that to check for a warning, with tm.assert_produces_warning)

And see the issue for a suggestion to expand the message.

Comment on lines 666 to 668
import warnings

from pandas.util._exceptions import find_stack_level
Copy link
Member

Choose a reason for hiding this comment

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

You can move those warnings to the top of the file

"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
# TODO: By using `zero_copy_only` it may be possible to implement this
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

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

This error should then be removed (we are adding the warning instead of the error, to warn people it will error in the future).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, I initially thought we would raise both an error and a warning.

Comment on lines 1675 to 1677
import warnings

from pandas.util._exceptions import find_stack_level
Copy link
Member

Choose a reason for hiding this comment

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

Same here about the imports (and similar for next cases)

"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

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

And same for the errors

@jorisvandenbossche jorisvandenbossche added this to the 2.3 milestone Nov 22, 2024
@jorisvandenbossche jorisvandenbossche added Compat pandas objects compatability with Numpy or Python functions Deprecate Functionality to remove in pandas labels Nov 23, 2024
@KevsterAmp
Copy link
Contributor Author

Thanks for the review despite this PR being in draft. Working on the tests and changes based on the comments, will mark it as ready once done

@KevsterAmp KevsterAmp marked this pull request as ready for review November 26, 2024 11:12
@KevsterAmp KevsterAmp marked this pull request as draft November 26, 2024 11:35
@KevsterAmp
Copy link
Contributor Author

Re-marking this as draft since I'll be fixing the errors on CI

Comment on lines 50 to 55
with pytest.raises(ValueError, match="Unable to avoid copy while creating"):
msg = "Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
"is not possible, Pandas will follow this behavior starting with "
"version 3.0. This conversion to NumPy requires a copy, but "
"'copy=False' was passed. Consider using 'np.asarray(..)' instead."
with tm.assert_produces_warning(FutureWarning, match=msg):
Copy link
Member

Choose a reason for hiding this comment

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

It's fine to just test a small part of the message, just so we assert the correct message is raised. Something like

msg = "Pandas will follow this behavior starting with version 3.0"
with tm.assert_produces_warning(FutureWarning, match=msg):
    ...

Copy link
Member

Choose a reason for hiding this comment

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

And for the other CI errors, essentially you will have to replace all occurrences of with pytest.raises(ValueError, match="Unable to avoid copy while creating") with this assert_produces_warning

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, will work on this in a couple of days. Been too focused on AoC this December 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is it alright if I match the first part of the message?

    msg = "Starting with NumPy 2.0, the behavior of the 'copy' keyword"
    with tm.assert_produces_warning(FutureWarning, match=msg):
        np.array(arr, copy=False)

Using

msg = "Pandas will follow this behavior starting with version 3.0"

is failing

Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
@jorisvandenbossche
Copy link
Member

@KevsterAmp do you have time to update this?

@KevsterAmp KevsterAmp marked this pull request as ready for review December 19, 2024 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Deprecate Functionality to remove in pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DEPR: deprecate / warn about raising an error in __array__ when copy=False cannot be honore
2 participants