-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix check 1d #22141
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
Fix check 1d #22141
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1300,47 +1300,13 @@ def _to_unmasked_float_array(x): | |
|
||
def _check_1d(x): | ||
"""Convert scalars to 1D arrays; pass-through arrays as is.""" | ||
if hasattr(x, 'to_numpy'): | ||
# if we are given an object that creates a numpy, we should use it... | ||
x = x.to_numpy() | ||
if not hasattr(x, 'shape') or len(x.shape) < 1: | ||
return np.atleast_1d(x) | ||
else: | ||
try: | ||
# work around | ||
# https://github.com/pandas-dev/pandas/issues/27775 which | ||
# means the shape of multi-dimensional slicing is not as | ||
# expected. That this ever worked was an unintentional | ||
# quirk of pandas and will raise an exception in the | ||
# future. This slicing warns in pandas >= 1.0rc0 via | ||
# https://github.com/pandas-dev/pandas/pull/30588 | ||
# | ||
# < 1.0rc0 : x[:, None].ndim == 1, no warning, custom type | ||
# >= 1.0rc1 : x[:, None].ndim == 2, warns, numpy array | ||
# future : x[:, None] -> raises | ||
# | ||
# This code should correctly identify and coerce to a | ||
# numpy array all pandas versions. | ||
with warnings.catch_warnings(record=True) as w: | ||
warnings.filterwarnings( | ||
"always", | ||
category=Warning, | ||
message='Support for multi-dimensional indexing') | ||
|
||
ndim = x[:, None].ndim | ||
# we have definitely hit a pandas index or series object | ||
# cast to a numpy array. | ||
if len(w) > 0: | ||
return np.asanyarray(x) | ||
# We have likely hit a pandas object, or at least | ||
# something where 2D slicing does not result in a 2D | ||
# object. | ||
if ndim < 2: | ||
return np.atleast_1d(x) | ||
return x | ||
# In pandas 1.1.0, multidimensional indexing leads to an | ||
# AssertionError for some Series objects, but should be | ||
# IndexError as described in | ||
# https://github.com/pandas-dev/pandas/issues/35527 | ||
except (AssertionError, IndexError, TypeError): | ||
return np.atleast_1d(x) | ||
return x | ||
|
||
|
||
def _reshape_2D(X, name): | ||
|
@@ -1649,7 +1615,7 @@ def index_of(y): | |
The x and y values to plot. | ||
""" | ||
try: | ||
return y.index.values, y.values | ||
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. Do we need both this change and the additional exception handling above? 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. I don't know, but I think we should be in the habit of using 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.
I think this change may be un-related (or fix this by chance) but I do not think it will avoid needing the other one and does no harm. |
||
return y.index.to_numpy(), y.to_numpy() | ||
except AttributeError: | ||
pass | ||
try: | ||
|
Uh oh!
There was an error while loading. Please reload this page.