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

docs: clarify clip behavior when arguments have different data types #896

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

kgryte
Copy link
Contributor

@kgryte kgryte commented Feb 6, 2025

This PR

@kgryte kgryte added API change Changes to existing functions or objects in the API. topic: Type Promotion Type promotion. labels Feb 6, 2025
@kgryte kgryte added this to the v2024 milestone Feb 6, 2025
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Thanks @kgryte. Looks good overall, one minor comment about phrasing how Python scalars are handled.

@@ -836,10 +836,12 @@ def clip(
Notes
-----

- This function is conceptually equivalent to ``maximum(minimum(x, max), min)`` when ``x``, ``min``, and ``max`` have the same data type.
- For scalar ``min`` and/or ``max``, the scalar values must be converted to zero-dimensional arrays having the same data type as ``x`` prior to broadcasting.
Copy link
Member

Choose a reason for hiding this comment

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

This sounds a bit like an implementation requirement, rather than only clarifying the intended semantics. It's also not what implementations do for mixed-kind dtypes:

>>> np.clip(np.arange(3, dtype=np.int32), -1, 1.5).dtype
dtype('float64')

I suggest saying instead that for scalars regular type promotion rules have to be followed (and hence mixed-kind is undefined).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, but wouldn't that be functionally the same? According to type promotion rules involving scalars and arrays, the first step is to convert the scalar to the same dtype as the array. When I included the line about conversion, that was what I had in mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. I've updated the text to hopefully address your concerns regarding outside-the-standard behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

...and I needed to relax the output array data type requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change Changes to existing functions or objects in the API. topic: Type Promotion Type promotion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants