Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Improve pandas/xarray/... conversion#22560
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.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1311,9 +1311,8 @@ def _to_unmasked_float_array(x): | ||
def _check_1d(x): | ||
"""Convert scalars to 1D arrays; pass-through arrays as is.""" | ||
# Unpack in case of e.g. Pandas or xarray object | ||
x = _unpack_to_numpy(x) | ||
if not hasattr(x, 'shape') or len(x.shape) < 1: | ||
return np.atleast_1d(x) | ||
else: | ||
@@ -1332,15 +1331,8 @@ def _reshape_2D(X, name): | ||
*name* is used to generate the error message for invalid inputs. | ||
""" | ||
# Unpack in case of e.g. Pandas or xarray object | ||
X = _unpack_to_numpy(X) | ||
# Iterate over columns for ndarrays. | ||
if isinstance(X, np.ndarray): | ||
@@ -2231,3 +2223,20 @@ def _picklable_class_constructor(mixin_class, fmt, attr_name, base_class): | ||
factory = _make_class_factory(mixin_class, fmt, attr_name) | ||
cls = factory(base_class) | ||
return cls.__new__(cls) | ||
def _unpack_to_numpy(x): | ||
"""Internal helper to extract data from e.g. pandas and xarray objects.""" | ||
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. Please document what we intend to support, i.e. everything with .to_numpy() or .values, and what types we expect to catch with it, e.g. values-> older pandas dataframes(?). | ||
if isinstance(x, np.ndarray): | ||
# If numpy, return directly | ||
return x | ||
if hasattr(x, 'to_numpy'): | ||
# Assume that any function to_numpy() do actually return a numpy array | ||
return x.to_numpy() | ||
if hasattr(x, 'values'): | ||
greglucas marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
xtmp = x.values | ||
# For example a dict has a 'values' attribute, but it is not a property | ||
# so in this case we do not want to return a function | ||
if isinstance(xtmp, np.ndarray): | ||
return xtmp | ||
return x |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from matplotlib.testing.conftest import (mpl_test_settings, | ||
pytest_configure, pytest_unconfigure, | ||
pd, xr) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -680,14 +680,37 @@ def test_reshape2d_pandas(pd): | ||
for x, xnew in zip(X.T, Xnew): | ||
np.testing.assert_array_equal(x, xnew) | ||
def test_reshape2d_xarray(xr): | ||
# separate to allow the rest of the tests to run if no xarray... | ||
X = np.arange(30).reshape(10, 3) | ||
x =xr.DataArray(X,dims=["x", "y"]) | ||
Xnew = cbook._reshape_2D(x, 'x') | ||
# Need to check each row because _reshape_2D returns a list of arrays: | ||
for x, xnew in zip(X.T, Xnew): | ||
np.testing.assert_array_equal(x, xnew) | ||
def test_index_of_pandas(pd): | ||
# separate to allow the rest of the tests to run if no pandas... | ||
X = np.arange(30).reshape(10, 3) | ||
x = pd.DataFrame(X, columns=["a", "b", "c"]) | ||
Idx, Xnew = cbook.index_of(x) | ||
np.testing.assert_array_equal(X, Xnew) | ||
IdxRef = np.arange(10) | ||
np.testing.assert_array_equal(Idx, IdxRef) | ||
def test_index_of_xarray(xr): | ||
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. Does xarray get us more coverage here? They have a So, it seems like a pretty heavy dependency to add for just this one test... 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. Not so much for coverage as for actually testing using data of specified formats. With the discussion about which formats we support, it makes sense to test them as well. Right now some of these are tested in the plots, but it can possibly make sense to simply test them here as these are the core function used to get data that can be plotted. If we claim (which we actually don't, maybe we should?) that we can plot xarray, we should probably test it as well. And other types that we may want to claim to support. Or maybe fork off a specific dependency test that is not executed on all platforms/version, including pandas (which is 11.7 MB, xarray is 870 kB). (There is another xarray-test above, so two.) I can of course remove them, but I think we should discuss if we want to support more formats than pandas and numpy (and Python list/tuple), and, if so, have explicit tests for them. 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 agree, we should probably discuss what we want to support/test. To me, this doesn't seem to add a whole lot of value for adding a new dependency. There was also a discussion around removing Scipy as a dependency in the docs:#22120 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 removed the dependencies but kept the tests. Hence, they will run if xarray is available. I also opened#22645 for discussions (probably should be discussed at a dev-call as well). | ||
# separate to allow the rest of the tests to run if no xarray... | ||
X = np.arange(30).reshape(10, 3) | ||
x = xr.DataArray(X, dims=["x", "y"]) | ||
Idx, Xnew = cbook.index_of(x) | ||
np.testing.assert_array_equal(X, Xnew) | ||
IdxRef = np.arange(10) | ||
np.testing.assert_array_equal(Idx, IdxRef) | ||
def test_contiguous_regions(): | ||
a, b, c = 3, 4, 5 | ||
# Starts and ends with True | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -7,3 +7,4 @@ pandas!=0.25.0 | ||
pikepdf | ||
pytz | ||
pywin32; sys.platform == 'win32' | ||
xarray |