| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
| if hasattr(X, 'values'): # support pandas.Series | ||
| X = X.values | ||
| if hasattr(X, 'to_numpy'): # support pandas.Series | ||
| X = X.to_numpy() |
There was a problem hiding this comment.
Are there any libraries we need to worry about that implement X.values, but not X.to_numpy()? Maybe we also want to leave an elif with the X.values block as a fallback just in case...?
Sorry, something went wrong.
|
Perhaps should be coordinated with #22141? |
Sorry, something went wrong.
That's where I got the idea. As seen, I have not touched cbook. Related to the question from @greglucas, I do not know. But I saw this code in cbook (which I didn't touch): matplotlib/lib/matplotlib/cbook/__init__.py Lines 1369 to 1377 in 3a994d2 This is a bit more complex as it uses a try-except approach (not sure how much that affects things though), has a fallback on values and check that values actually returns an nparray. One can of course use a similar approach here (and in #22141). Possibly slightly improved as I do not know if there are cases if values actually is non-trivial, so no need to run it twice. Edit: I have now touched this and use a similar approach, but with hasattr instead of try-except see https://stackoverflow.com/questions/903130/hasattr-vs-try-except-block-to-deal-with-non-existent-attributes as the assumption is that most often we do not pass pandas or xarray (or whatever) data. |
Sorry, something went wrong.
|
I guess you could write a cbook._unpack_pandas(x) helper to use that in all of these areas for consistency? I would say it is safer to keep the .values fallback, and if you do want to get rid of it eventually, you could put in a deprecation warning if that branch is hit. |
Sorry, something went wrong.
|
Yes, I was also thinking about a helper function (but didn't know where to place it, so great info that cbook is the place). I do not have any strong opinions as such, more that I read the link and it seemed like the right thing to do. |
Sorry, something went wrong.
|
Seems like the easiest way is to wait for #22141, add those conversions here as well, and then discuss the correct name for the function. |
Sorry, something went wrong.
|
I'm thinking that maybe _unpack_to_numpy is a general enough name? |
Sorry, something went wrong.
|
This is now updated:
|
Sorry, something went wrong.
|
Please update the lines touched by #22141 |
Sorry, something went wrong.
|
There is also this function where it doesn't work to simply replace to_numpy with _unpack_to_numpy. No idea why. (One reason may be that there is no attribute error for to_numpy anymore, so some types of input data is handled well by _check_1d, has an index method, but no to_numpy method... Seems like plain Python lists can be a culprit here then...) matplotlib/lib/matplotlib/cbook/__init__.py Lines 1607 to 1639 in 0359832 |
Sorry, something went wrong.
| np.testing.assert_array_equal(Idx, IdxRef) | ||
|
|
||
|
|
||
| def test_index_of_xarray(xr): |
There was a problem hiding this comment.
Does xarray get us more coverage here? They have a to_numpy() method the same as pandas I believe.
https://xarray.pydata.org/en/stable/generated/xarray.DataArray.to_numpy.html
So, it seems like a pretty heavy dependency to add for just this one test...
Sorry, something went wrong.
There was a problem hiding this comment.
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.
Sorry, something went wrong.
There was a problem hiding this comment.
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
Sorry, something went wrong.
There was a problem hiding this comment.
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).
Sorry, something went wrong.
|
I made an executive decision to install xarray on CI. We already have all of its dependencies installed and it is a pure-python package. |
Sorry, something went wrong.
|
|
||
|
|
||
| def _unpack_to_numpy(x): | ||
| """Internal helper to extract data from e.g. pandas and xarray objects.""" |
There was a problem hiding this comment.
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(?).
Sorry, something went wrong.
|
We discussed this on the call, I will write up an issues shortly about the documentation of the intention etc. |
Sorry, something went wrong.
|
meeseeksdev backport to v3.5.x |
Sorry, something went wrong.
…560-on-v3.5.x Backport PR #22560 on branch v3.5.x (Improve pandas/xarray/... conversion)
see matplotlib/matplotlib#22973, matplotlib/matplotlib#22879, and matplotlib/matplotlib#22560 It is not clear to me as to which is the standard interface for unit handling (for eg, hist still doesn't handle unit by default)
| Back | FazBrowse Home | New Git URL |
PR Summary
See https://stackoverflow.com/questions/13187778/convert-pandas-dataframe-to-numpy-array/54508052#54508052 for motivation.
Related to #16402
PR Checklist
Tests and Styling
Documentation