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: calling np.sqrt on an array containing only floats, but of dtype object, will crash #27870

Open
MarcBresson opened this issue Nov 27, 2024 · 3 comments

Comments

@MarcBresson
Copy link

Describe the issue:

np.sqrt is one of the only operation that crashes on a nd array of dtype object containing only floats.

Furthermore, the error yielded is very unclear, mentioning that floats does not support sqrt (which is not true).

The example speak for itself.

Reproduce the code example:

import numpy as np

feature = np.array([1.2], dtype="object")
print(np.sum(feature))
# 1.2
print(np.sqrt(feature))

Error message:

AttributeError: 'float' object has no attribute 'sqrt'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: loop of ufunc does not support argument 0 of type float which has no callable sqrt method

Python and NumPy Versions:

2.1.3
3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]

Runtime Environment:

[{'numpy_version': '2.1.3',
'python': '3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 '
'(clang-1500.1.0.2.5)]',
'uname': uname_result(system='Darwin', node='Marcos-Macbook-air.local', release='23.4.0', version='Darwin Kernel Version 23.4.0: Fri Mar 15 00:19:22 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8112', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP'],
'not_found': ['ASIMDFHM']}}]

Context for the issue:

No response

@eendebakpt
Copy link
Contributor

There are more ufuncs not working: for example np.sin(feature) gives the same error.

The following example works:

import numpy as np
import math

class A(float):   
    def sqrt(self):
        print('sqrt method called!')
        return math.sqrt(self)

feature = np.array([A(2.2)], dtype=object)
print(np.sum(feature))
print(np.sqrt(feature))

For an array with dtype object it seems np.sqrt is trying to call x.sqrt() for every element x. @MarcBresson I suspect you expected numpy would call np.sqrt on the elements of feature instead?

Not sure this is a bug or a feature, but I agree the error message might be a bit unclear.

@MarcBresson
Copy link
Author

Indeed, that is what I m expecting.

I could be seen as a feature since object arrays can store anything, so this probably maximizes compatibility in some contexts. However, there could be a fallback onto np.sqrt in case the object does not have a sqrt method.

I wonder why sum does not yield that error. Would it be because the + operator is available for nearly all python objects?

@eendebakpt
Copy link
Contributor

The np.sum is not a ufunc, but is implemented using np.add (which is a ufunc). That method looks for __add__, which is present for float objects.

For example:

import numpy as np
import math

class B():   
    def __init__(self, value):
        self.value = value
   
    def __float__(self):
        return float(self.value)
    
    def sqrt(self):
        print('sqrt method called!')
        return math.sqrt(self.value)        
    
    def __add__(self, other):
        print(f'__add__ method {self} {other} called!')
        s = self.value + float(other)
        return s
    
    def add(self, other):
        print('add method called!')
        s = self.value + float(other)
        return s
    
feature = np.array([B(2.2), B(1.2)], dtype=object)
print(np.sum(feature))
print(np.sqrt(feature))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants