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

[FEA] Proxy ndarrays don't pass an instancecheck() for np.ndarray #14537

Closed
shwina opened this issue Nov 30, 2023 · 2 comments · Fixed by #16286 or #16601
Closed

[FEA] Proxy ndarrays don't pass an instancecheck() for np.ndarray #14537

shwina opened this issue Nov 30, 2023 · 2 comments · Fixed by #16286 or #16601
Assignees
Labels
0 - Blocked Cannot progress due to external reasons cudf.pandas Issues specific to cudf.pandas feature request New feature or request Python Affects Python cuDF API.

Comments

@shwina
Copy link
Contributor

shwina commented Nov 30, 2023

There's a lot of code out there that does something like:

if not isinstance(x, np.ndarray):
    raise TypeError("Not a numpy array")

This is a problem for cudf.pandas, because proxy ndarray types, such as those returned by pd.Series.values do not pass the isinstance() check.

Ideally, more projects would avoid doing a hard isinstance() check and instead use something like EAFP:

np.asarray(x)

..but that is not the world we live in today. Too many third-party libraries that people want to use with cudf.pandas use the pattern above, so it's on us to solve the problem right now.

@shwina shwina added feature request New feature or request Needs Triage Need team to review and classify Python Affects Python cuDF API. pandas and removed Needs Triage Need team to review and classify labels Nov 30, 2023
@galipremsagar galipremsagar added the cudf.pandas Issues specific to cudf.pandas label Apr 15, 2024
@galipremsagar galipremsagar added this to the Proxying - cudf.pandas milestone Apr 15, 2024
@vyasr vyasr removed the pandas label May 16, 2024
@Matt711
Copy link
Contributor

Matt711 commented Jun 13, 2024

As far as I can see there isn't a fix for this because we cannot modify np.ndarray's __instancecheck__ which is called when you call isinstance(c, np.ndarray). For example,

import cudf.pandas
cudf.pandas.install()
import pandas as pd
import numpy as np

x = pd.Series([1,2]).to_numpy() # proxy for numpy.ndarray

isinstance(x, np.ndarray) # calls np.ndarray.__instancecheck__(x) which returns false

I thought you might be able to modify np.ndarray.__instancecheck__ but you get an error

TypeError: cannot set '__instancecheck__' attribute of immutable type 'numpy.ndarray'

@Matt711 Matt711 self-assigned this Jun 13, 2024
@shwina
Copy link
Contributor Author

shwina commented Jun 13, 2024

That's correct. I don't think there's a very good solution here, short of proxying numpy in the same way we proxy pandas. But that comes with its own set of challenges.

@Matt711 Matt711 added the 0 - Blocked Cannot progress due to external reasons label Jun 25, 2024
@vyasr vyasr moved this to Todo in cuDF Python Jun 26, 2024
@Matt711 Matt711 moved this from Todo to Blocked in cuDF Python Jun 26, 2024
@GPUtester GPUtester moved this from Blocked to In Progress in cuDF Python Jul 16, 2024
@rapids-bot rapids-bot bot closed this as completed in 1c63e1e Aug 16, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in cuDF Python Aug 16, 2024
@GPUtester GPUtester moved this from Done to In Progress in cuDF Python Aug 19, 2024
@Matt711 Matt711 reopened this Aug 26, 2024
@rapids-bot rapids-bot bot closed this as completed in e1ab1e7 Sep 5, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in cuDF Python Sep 5, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in cuDF Python Sep 5, 2024
rjzamora pushed a commit to rjzamora/cudf that referenced this issue Sep 6, 2024
res-life pushed a commit to res-life/cudf that referenced this issue Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0 - Blocked Cannot progress due to external reasons cudf.pandas Issues specific to cudf.pandas feature request New feature or request Python Affects Python cuDF API.
Projects
Status: Done
4 participants