Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
FIX:_safe_first_finite
on all non-finite array#25547
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
I see that a different approach was taken to handle the change in behaviour for |
I think that this approach is quite a bit simpler than the one used in#24149. On the other hand it is a bit more explicit what happens there. My suggestions:
|
Thanks@oscargus your suggestions make sense to me. I’ll put this in draft until I have time to implement them. |
_safe_first_finite
on all nan array_safe_first_finite
on all non-finite arrayexcept StopIteration: | ||
# this means we found no finite element, fall back to first | ||
# element unconditionally | ||
x0 = cbook.safe_first_element(x0) | ||
try: | ||
x = cbook._safe_first_finite(xconv) | ||
except (TypeError, IndexError, KeyError): |
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.
I think there a few other places where StopIteration is try/excepted for _safe_first_element? Did you check all those?
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.
I hadn't, but I have now. I only found these two:
matplotlib/lib/matplotlib/dates.py
Lines 1828 to 1831 in7103779
try: | |
x=cbook._safe_first_finite(x) | |
except (TypeError,StopIteration): | |
pass |
matplotlib/lib/matplotlib/units.py
Lines 182 to 185 in7103779
try:# If cache lookup fails, look up based on first element... | |
first=cbook._safe_first_finite(x) | |
except (TypeError,StopIteration): | |
pass |
These both predate the change that led toStopIteration
being thrown for all-nan arrays. Thecommit that introduced the first says it was for zero length objects, and these will still trigger aStopIteration
when the logic arriveshere. I'm not sure about the second - shouldunits.Registry.get_converter
also be able to handle zero length objects?
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.
I don't really know - just noted that it happens a few other places, and the more logic we can move into a consistent helper function, the better in my opinion.
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.
the more logic we can move into a consistent helper function, the better in my opinion.
I certainly agree with that. I wonder if further consolidation should wait for a separate PR though, given recent clarifications about what should and should not be backported
- my current change fixes a regression from v3.6, so it would be good to get in a patch release
- a change that tidies the code but (hopefully) makes no difference to the user should maybe wait till v3.8
…547-on-v3.7.xBackport PR#25547 on branch v3.7.x (FIX: `_safe_first_finite` on all non-finite array)
Uh oh!
There was an error while loading.Please reload this page.
PR Summary
This function used to be named
_safe_first_non_none
(#23751), and at v3.6.0 we haveSo this PR reinstates previous behaviour. Currently on
main
,_safe_first_finite
raisesStopIteration
when passed an all-nan array.Fixes#18294 andfixes#24818. The examples from both those issues now successfully produce empty plots. The example from#18294 no longer throws the originally reported warning, but the example from#24818 does throw
PR Checklist
Documentation and Tests
pytest
passes)Release Notes
.. versionadded::
directive in the docstring and documented indoc/users/next_whats_new/
.. versionchanged::
directive in the docstring and documented indoc/api/next_api_changes/
next_whats_new/README.rst
ornext_api_changes/README.rst