Content-Length: 263408 | pFad | https://github.com/numpy/numpy/issues/18832

6C Wrong error message for "+" (add) operation about "use np.concontenate() instead" for scalar with array priority · Issue #18832 · numpy/numpy · GitHub
Skip to content

Wrong error message for "+" (add) operation about "use np.concontenate() instead" for scalar with array priority #18832

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

Open
jorisvandenbossche opened this issue Apr 22, 2021 · 4 comments
Labels

Comments

@jorisvandenbossche
Copy link
Contributor

Reproducing code example:

import numpy as np
import pandas as pd
import datetime

>>> np.array([1.0, 2.0, 3.0]) + datetime.datetime.now()
...
TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'

>>> np.array([1.0, 2.0, 3.0]) + pd.Timestamp("2021-01-01")
...
TypeError: Concatenation operation is not implemented for NumPy arrays, use np.concatenate() instead. Please do not rely on this error; it may not be given on all Python implementations.

So for datetime.datetime object, this gives the expected TypeError. But for pd.Timestamp it gives a confusing error message.

I suppose the difference could stem from the fact that pd.Timestamp defines an __array_priority__. However, in this case it just returns NotImplemented when passed a float ndarray, so I would still expect to get the normal "unsupported operand type" TypeError.

(opening the issue here in numpy because the error message comes from numpy; of course it might also be that we need to change something in Timestamp.__add__, although as said above, it's returning NotImplemented in this case)

NumPy/Python version information:

In [6]: import sys, numpy; print(numpy.__version__, sys.version)
1.20.1 3.8.6 | packaged by conda-forge | (default, Nov 27 2020, 19:31:52) 
[GCC 9.3.0]
@seberg
Copy link
Member

seberg commented Apr 22, 2021

Interesting, Python allows this to drop through to concatenation even though both operators define normal addition (even though both operators define normal addition). I am not sure that is quite correct on the Python side, but I guess that doesn't really matter.

I can't really think of a solution right now. We may just have to delete that whole code path again, if it is accessible through the normal + operator.

@jorisvandenbossche
Copy link
Contributor Author

We may just have to delete that whole code path again

Do you mean what was added in #16570?

@seberg
Copy link
Member

seberg commented Apr 22, 2021

Bah, this is all super awkward... Yes, I mean that PR, in the end I am not invested in keeping that PR around. I think that concat is weird, since returning NotImplemented should also guarantee Python's "incompatible operands TypeError", and that is something only we can fix.

But... At the same time pandas is sligthly breaking the "assymetry of binary operands" contract (which is very arcane!).

Basically __add__ and __radd__ have an inherent assymetry in that __radd__ has a bit of an obligation not to defer back (unless it is to a superclass).
Since you indicate to NumPy that it must defer from __add__ (using __array_priority__) you also get into the position that you are obliged to handle the operation! You are allowed to defer from your __add__, but not from __radd__ (although I am not sure how that distinction plays in cython).

To proof my point, try this:

arr = np.array([pd.Timedelta("10 days")])
pd.Timestamp("2021-01-01") + arr
# works: array([Timestamp('2021-01-11 00:00:00')], dtype=object)
arr + pd.Timestamp("2021-01-01")
# TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'Timestamp'

In the first case it is OK that you defer to NumPy. NumPy realize its __radd__ is used and wraps the timestamp into an object array. In the second case, NumPy has no way of doing this (this is impossible in the way binary operators are designed in Python – I don't think it even depends on the C-API).

(Long story short, I won't complain about undoing the PR to get the old TypeError back, but I am not quite sure its actually the best outcome for either project.)

@ikamensh
Copy link

ikamensh commented May 5, 2025

This happened to me because I forgot to convert a torch tensor to numpy. Basically in this scenario:

import torch
import numpy as np

a = np.array([1, 2,])
b = torch.tensor([1, 2,])
c = a + b

Error:

E           TypeError: Concatenation operation is not implemented for NumPy arrays, use np.concatenate() instead. Please do not rely on this error; it may not be given on all Python implementations.

This was quite confusing to debug because I used assignment to a slice where I've combined scatter on one dimension and bool mask on other dimension and LLMs would tell me the problem is with my indexing...

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








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/numpy/numpy/issues/18832

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy