-
Notifications
You must be signed in to change notification settings - Fork 50
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
base: main
Are you sure you want to change the base?
Conversation
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.
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. |
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.
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).
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.
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.
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.
Okay. I've updated the text to hopefully address your concerns regarding outside-the-standard behavior.
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.
...and I needed to relax the output array data type requirement.
This PR
clip
behavior is undefined whenmin
ormax
is outside the bounds ofx
#814 (comment) and Python scalars in elementwise functions #807 and supersedes feat!: allow clip to have int min or max when x is floating-point #811 to indicate that behavior is only defined whenmin
andmax
have the same data type asx
. After reviewing SciPy and scikit-learn usage ofxp.clip
andnp.clip
, the overwhelming usage was using scalarmin
andmax
. For all instances that I found with arraymin
andmax
, type promotion behavior was not expected. Given the various edge cases whenmin
andmax
are not the same type asx
, it seems prudent to limit portable behavior to the scenario which avoids edge cases altogether.clamp
as suggested in docs: clarify thatclip
behavior is undefined whenmin
ormax
is outside the bounds ofx
#814 (comment).min
and/ormax
are scalar values.