-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ValueError: invalid __array_struct__ when assigning to object-dtype ndarray #16939
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
Comments
The SciPy builds on TravisCI with |
Probably because I fixed the nightly cython related build failures and triggered new builds yesterday. The nightly builds had stalled out :) |
@seberg Thoughts? |
I am a bit surprised I did not notice additional scipy failures when comparing to master. The SciPy ones are reverse broadcasting, I thought I covered that, but apparently I missed something. The problem is this type of code:
which only works because The pandas code with the dtypes, I am not sure about, may also be coercion related likely. It seems like we used to ignored errors here at some point and now I know that its necessary to support these classes as objects. |
Ah, on the pandas issue, we currently have this, so that explains the change:
they behave identical now, but went towards the error case. |
Was just about to comment the same - this has always been a problem for scalars, it's just we made behavior consistent for lists of them too. |
#8877 is very similar here, our problem is we use |
An easy fix would be to ignore |
True, maybe that is the best solution. We may need to also let |
Perhaps checking for |
Hmmm, I guess so, although it would also be nice not to rely on inspecting slots? The closest thing I could find in python are these from
Interestingly, it seems that |
This was previously allowed for nested cases, i.e.: np.array([np.int64]) but not for the scalar case: np.array(np.int64) The solution is to align these two cases by always interpreting these as not being array-likes (instead of invalid array-likes) if the passed in object is a `type` object and the special attribute has a `__get__` attribute (and is thus probable a property or method). The (arguably better) alterative to this is to move the special attribute lookup to be on the type instead of the instance (which is what python does). This will definitely require some adjustments in our tests to use properties, but is probably fine with respect to most actual code. (The tests more commonly use this to quickly set up an array-like, while it is a fairly strange pattern for typical code.) Address parts of numpygh-16939 Closes nump 8000 ygh-8877
Opted for |
As to the SciPy issues, I am wondering if we could just get away with it, but have not thought about trying to keep supporting them much. Note that this really only affects If we expect this to be extremely rare, I would like it. But, I admit I am worried that the error is very confusing when you look at it for code that previously worked. I am not sure yet, I suppose I can guess that for basically all of our numerical types, I know that it will incidentally work out (and thus a DeprecationWarning would be in order), but otherwise the error needs to be raised immediately. |
Previously, code such as: ``` np.array([0, np.array([0])], dtype=np.int64) ``` worked by discovering the array as having a shape of `(2,)` and then using `int(np.array([0]))` (which currently succeeds). This is also a ragged array, and thus deprecated here earlier on. (As a detail, in the new code the assignment should occur as an array assignment and not using the `__int__`/`__float__` Python protocols.) Two details to note: 1. This still skips the deprecation for sequences which are not array-likes. We assume that sequences will always fail the conversion to a number. 2. The conversion to a number (or string, etc.) may still fail after the DeprecationWarning is given. This is not ideal, but narrowing it down seems tedious, since the actual assignment happens in a later step. I.e. `np.array([0, np.array([None])], dtype=np.int64)` will give the warning, but then fail anyway, since the array cannot be assigned. Closes numpygh-16939
OK, gh-16943 address the SciPy issue. Not by supporting it, but by deprecating it for now. Its not perfect (you can construct things that skip the deprecation in theory, e.g. scipy sparse matrices might), and you may get the warning but then a failure, which is slightly annoying. But I think it would be fairly tricky to get it "right", and it is clear that this type of usage is bad incorrect. I.e. in my opinion it seems good enough to cover almost everything, in the face of covering everything requiring to mix up two distinct steps of the array-coercion process. |
statsmodels with - - pre is also failing with this. |
Keeping issue open as it isn't clear we are done with it. |
This has not been fixed in master, in case it was though that it had:
using |
Closing as the PR is merged now. Please ping if I am missing something. |
Uh oh!
There was an error while loading. Please reload this page.
Hi. Pandas is broken with numpy master :)
Reproducing code example:
Error message:
Numpy/Python version information:
Installed from a wheel with
pip install -i https://pypi.anaconda.org/scipy-wheels-nightly/simple numpy
I don't immediately see what commit could have caused this. #16850 mentions object-dtype scalars, but doesn't look quite right.
The text was updated successfully, but these errors were encountered: