Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: numpy.result_type on non-array objects that have a dtype attribute should always return dtype object #26612

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
lesteve opened this issue Jun 4, 2024 · 4 comments
Labels

Comments

@lesteve
Copy link
Contributor

lesteve commented Jun 4, 2024

Describe the issue:

numpy.result.dtype on non-array objects that have a .dtype attribute will unexpectedly use the .dtype attribute instead of returning dtype object.

Creating an array with the same objects will as expected return an array of dtype object, but sometimes it is convenient to know the dtype without creating the array.

Reproduce the code example:

import numpy as np

class MyClass:
    def __init__(self):
        self.dtype = np.dtype('float64')


print(np.result_type(MyClass(), np.float64))

print(np.array([MyClass(), np.float64]).dtype)

Error message:

float64
object

Python and NumPy Versions:

Tried with 1.26.3 and development Numpy wheel

Runtime Environment:

No response

Context for the issue:

This was noticed in scikit-learn where some estimators like OrdinalEncoder have a dtype parameter and hence a .dtype atribute: scikit-learn/scikit-learn#29157 (comment)

@ngoldbaum ngoldbaum changed the title BUG: numpy.result_dtype on non-array objects that have a dtype attribute should always return dtype object BUG: numpy.result_type on non-array objects that have a dtype attribute should always return dtype object Jun 4, 2024
@seberg
Copy link
Member

seberg commented Jun 5, 2024

Hmmm, trick apparently result_type("float64") also works... Or did this change?

@lesteve
Copy link
Contributor Author

lesteve commented Jun 5, 2024

Not sure about the exact meaning of your question 😅, but indeed it looks like np.result_type('float64') "works" and returns np.dtype('float64'). I guess the underlying issue is that np.dtype(MyClass()) is np.dtype('float64')?

Maybe this is a not such a great idea in scikit-learn to have objects with .dtype attribute ...

@seberg
Copy link
Member

seberg commented Jun 5, 2024

I was wondering if there is a regression. I don't think so. result_type is badly overloaded, note that it converts to arrays anyway internally if you pass a non dtype.

One longer term solution might be to add rsult_type(*args, values=[]) to remove the ambiguity that this might be a dtype (like) object.

@lesteve
Copy link
Contributor Author

lesteve commented Jun 6, 2024

I was wondering if there is a regression. I don't think so

Indeed, I don't think it is a regression either

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

2 participants