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

Add a developer check for proxy objects #15956

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/cudf/source/developer_guide/cudf_pandas.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The "wrapped" types/classes are the Pandas and cuDF specific types that have bee
Wrapped objects and proxy objects are instances of wrapped types and proxy types, respectively.
In the snippet below `s1` and `s2` are wrapped objects and `s3` is a fast-slow proxy object.
Also note that the module `xpd` is a wrapped module and contains cuDF and Pandas modules as attributes.
To check if an object is a proxy type, we can use `cudf.pandas.is_proxy_object`.
```python
import cudf.pandas
cudf.pandas.install()
Expand All @@ -31,6 +32,14 @@ Also note that the module `xpd` is a wrapped module and contains cuDF and Pandas
s1 = cudf.Series([1,2])
s2 = pd.Series([1,2])
s3 = xpd.Series([1,2])

from cudf.pandas import is_proxy_object

is_proxy_object(s1) # returns False

is_proxy_object(s2) # returns False

is_proxy_object(s3) # returns True
```

```{note}
Expand Down
5 changes: 3 additions & 2 deletions python/cudf/cudf/pandas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2023-2024, NVIDIA CORPORATION & AFFILIATES.
# All rights reserved.
# SPDX-License-Identifier: Apache-2.0

from .fast_slow_proxy import is_proxy_object
from .magics import load_ipython_extension
from .profiler import Profiler

__all__ = ["Profiler", "load_ipython_extension", "install"]
__all__ = ["Profiler", "load_ipython_extension", "install", "is_proxy_object"]


LOADED = False
Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/pandas/fast_slow_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,20 @@ def _replace_closurevars(
)


def is_proxy_object(obj: Any) -> bool:
"""Determine if an object is proxy object

Parameters
----------
obj : object
Any python object.

"""
if _FastSlowProxyMeta in type(type(obj)).__mro__:
return True
return False


NUMPY_TYPES: Set[str] = set(np.sctypeDict.values())


Expand Down
16 changes: 15 additions & 1 deletion python/cudf/cudf_pandas_tests/test_cudf_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pytz import utc

from cudf.pandas import LOADED, Profiler
from cudf.pandas.fast_slow_proxy import _Unusable
from cudf.pandas.fast_slow_proxy import _Unusable, is_proxy_object

if not LOADED:
raise ImportError("These tests must be run with cudf.pandas loaded")
Expand Down Expand Up @@ -1488,3 +1488,17 @@ def mock_mean_none(self, *args, **kwargs):

def test_excelwriter_pathlike():
assert isinstance(pd.ExcelWriter("foo.xlsx"), os.PathLike)


def test_is_proxy_object():
np_arr = np.array([1])

s1 = xpd.Series([1])
s2 = pd.Series([1])

np_arr_proxy = s1.to_numpy()

assert not is_proxy_object(np_arr)
Matt711 marked this conversation as resolved.
Show resolved Hide resolved
assert is_proxy_object(np_arr_proxy)
assert is_proxy_object(s1)
assert not is_proxy_object(s2)
Loading