Skip to content
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

BUG: ufunc on ragged object array emits a warning #15045

Closed
mattip opened this issue Dec 4, 2019 · 5 comments
Closed

BUG: ufunc on ragged object array emits a warning #15045

mattip opened this issue Dec 4, 2019 · 5 comments
Labels

Comments

@mattip
Copy link
Member

mattip commented Dec 4, 2019

After merging gh-14794 this emits a DeprecationWarning

$ python3 -Werror

>>> import numpy as np
>>> arr = np.array(["a", "b"], dtype=object)
>>> objs = [arr, "foo", arr]
>>> np.sum(objs, axis=0)

xref gh-15041

@mattip
Copy link
Member Author

mattip commented Dec 4, 2019

Also, there is a test that is creating a ragged array and emitting a warning, but the warning somehow slips under the radar of pytest, maybe because it is inside an assert_raises

@mattip
Copy link
Member Author

mattip commented Dec 4, 2019

The solution to the first problem is to pro-actively create the array with an object dtype:

>>> import numpy as np
>>> arr = np.array(["a", "b"], dtype=object)
>>> objs = np.array([arr, "foo", arr], dtype=object)  # HERE
>>> np.sum(objs, axis=0)

I am probably reacting to an overly-simplified example since this is a snippet from a more complex code base.

@seberg
Copy link
Member

seberg commented Dec 4, 2019

@jbrockmendel is using np.sum(np.array(objs, dtype=object), axis=0) in some way undesierable for you? (or even going further and using arr = np.empty(len(objs), dtype=object); arr[:] = objs to not rely on numpy guessing the right thing at all)

@jbrockmendel
Copy link
Contributor

@mattip @seberg thanks for the quick feedback. In the np.sum with strs case, the suggested workaround likely works. To get an idea for where else this comes up in the pandas codebase, see pandas-dev/pandas#30035

The change here https://github.com/pandas-dev/pandas/pull/30035/files?file-filters%5B%5D=.py#diff-d5ffc8f19bc8362967ce9ddd23026739R141 was needed to silence warnings caused by calling operator.mod (i dont remember what the inputs were off the top of my head)

@mattip
Copy link
Member Author

mattip commented Dec 9, 2019

Closing the correct fix is to explicitly create the ndarray before calling a ufunc.

@mattip mattip closed this as completed Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants