-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX: be more careful about coercing unit-full containers to ndarray #21884
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3282,10 +3282,56 @@ def errorbar(self, x, y, yerr=None, xerr=None, | |
x = np.asarray(x, dtype=object) | ||
if not isinstance(y, np.ndarray): | ||
y = np.asarray(y, dtype=object) | ||
|
||
def _upcast_err(err): | ||
""" | ||
Safely handle tuple of containers that carry units. | ||
|
||
If the units are carried on the values then casting to object | ||
arrays preserves the units, but if the units are on the containers | ||
this will not work. | ||
|
||
This function covers the case where the input to the xerr/yerr is a | ||
length 2 tuple of equal length ndarray-subclasses that carry the | ||
unit information in the container. | ||
|
||
We defer coercing the units to be consistent to the underlying unit | ||
library (and implicitly the broadcasting). | ||
|
||
If we do not have a tuple of nested numpy array (subclasses), | ||
fallback to casting to an object array. | ||
|
||
""" | ||
|
||
# we are here because we the container is not a numpy array, but it | ||
# _is_ iterable (likely a list or a tuple but maybe something more | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But at this point you don't know it is iterable--you check it. If it is not iterable, you still wrap it in an object array. |
||
# exotic) | ||
|
||
if ( | ||
# make sure it is not a scalar | ||
np.iterable(err) and | ||
# and it is not empty | ||
len(err) > 0 and | ||
# and the first element is an array sub-class use | ||
# safe_first_element because getitem is index-first not | ||
# location first on pandas objects so err[0] almost always | ||
# fails. | ||
isinstance(cbook.safe_first_element(err), np.ndarray) | ||
): | ||
# grab the type of the first element, we will try to promote | ||
# the outer container to match the inner container | ||
atype = type(cbook.safe_first_element(err)) | ||
# you can not directly pass data to the init of `np.ndarray` | ||
if atype is np.ndarray: | ||
return np.asarray(err, dtype=object) | ||
# but you can for unyt and astropy uints | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. astropy "units", I presume |
||
return atype(err) | ||
return np.asarray(err, dtype=object) | ||
|
||
if xerr is not None and not isinstance(xerr, np.ndarray): | ||
xerr = np.asarray(xerr, dtype=object) | ||
xerr = _upcast_err(xerr) | ||
if yerr is not None and not isinstance(yerr, np.ndarray): | ||
yerr = np.asarray(yerr, dtype=object) | ||
yerr = _upcast_err(yerr) | ||
x, y = np.atleast_1d(x, y) # Make sure all the args are iterable. | ||
if len(x) != len(y): | ||
raise ValueError("'x' and 'y' must have the same size") | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 find the docstring confusing, mixing the what and the why in a way that I can't sort out.
I suggest simply stating what it does under each case it handles.