Skip to content

Commit f949dee

Browse files
Move isinstance_cudf_pandas to fast_slow_proxy (#17875)
https://github.com/rapidsai/cudf/pull/17629/files#diff-8731197057aec7c2ece5535ff5fb740a7d2109b213bb859ccd19290d40b7b703R11 broke number of cuml pytests. This was because `pandas_compatible` mode was being set in `._wrappers.pandas` and the import introduced in the above pr was the reason for it. This pr fixes it by moving the `isinstance_cudf_pandas` to `fast_slow_proxy` module. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Vyas Ramasubramani (https://github.com/vyasr) URL: #17875
1 parent ca06c39 commit f949dee

File tree

6 files changed

+34
-31
lines changed

6 files changed

+34
-31
lines changed

docs/cudf/source/cudf_pandas/faq.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,24 @@ provides a similar configuration-based plugin for Spark.
144144

145145
## How do I know if an object is a `cudf.pandas` proxy object?
146146

147-
To determine if an object is a `cudf.pandas` proxy object, you can use the `isinstance_cudf_pandas` API. This function checks if the given object is a proxy object that wraps either a `cudf` or `pandas` object. Here is an example of how to use this API:
147+
To determine if an object is a `cudf.pandas` proxy object, you can use the `is_proxy_instance` API. This function checks if the given object is a proxy object that wraps either a `cudf` or `pandas` object. Here is an example of how to use this API:
148148

149149
```python
150-
from cudf.pandas import isinstance_cudf_pandas
150+
from cudf.pandas import is_proxy_instance
151151

152152
obj = ... # Your object here
153-
if isinstance_cudf_pandas(obj, pd.Series):
153+
if is_proxy_instance(obj, pd.Series):
154154
print("The object is a cudf.pandas proxy Series object.")
155155
else:
156156
print("The object is not a cudf.pandas proxy Series object.")
157157
```
158158

159159
To detect `Series`, `DataFrame`, `Index`, and `ndarray` objects separately, you can pass the type names as the second parameter:
160160

161-
* `isinstance_cudf_pandas(obj, pd.Series)`: Detects if the object is a `cudf.pandas` proxy `Series`.
162-
* `isinstance_cudf_pandas(obj, pd.DataFrame)`: Detects if the object is a `cudf.pandas` proxy `DataFrame`.
163-
* `isinstance_cudf_pandas(obj, pd.Index)`: Detects if the object is a `cudf.pandas` proxy `Index`.
164-
* `isinstance_cudf_pandas(obj, np.ndarray)`: Detects if the object is a `cudf.pandas` proxy `ndarray`.
161+
* `is_proxy_instance(obj, pd.Series)`: Detects if the object is a `cudf.pandas` proxy `Series`.
162+
* `is_proxy_instance(obj, pd.DataFrame)`: Detects if the object is a `cudf.pandas` proxy `DataFrame`.
163+
* `is_proxy_instance(obj, pd.Index)`: Detects if the object is a `cudf.pandas` proxy `Index`.
164+
* `is_proxy_instance(obj, np.ndarray)`: Detects if the object is a `cudf.pandas` proxy `ndarray`.
165165

166166
## How can I access the underlying GPU or CPU objects?
167167

python/cudf/cudf/pandas/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
import pylibcudf
99
import rmm.mr
1010

11-
from ._wrappers.pandas import isinstance_cudf_pandas
12-
from .fast_slow_proxy import is_proxy_object
11+
from .fast_slow_proxy import is_proxy_instance, is_proxy_object
1312
from .magics import load_ipython_extension
1413
from .profiler import Profiler
1514

16-
__all__ = ["Profiler", "install", "is_proxy_object", "load_ipython_extension"]
15+
__all__ = [
16+
"Profiler",
17+
"install",
18+
"is_proxy_instance",
19+
"is_proxy_object",
20+
"load_ipython_extension",
21+
]
1722

1823

1924
LOADED = False

python/cudf/cudf/pandas/_wrappers/pandas.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
_FunctionProxy,
3838
_maybe_wrap_result,
3939
_Unusable,
40-
is_proxy_object,
4140
make_final_proxy_type as _make_final_proxy_type,
4241
make_intermediate_proxy_type as _make_intermediate_proxy_type,
4342
register_proxy_func,
@@ -70,8 +69,6 @@
7069
except ImportError:
7170
ipython_shell = None
7271

73-
cudf.set_option("mode.pandas_compatible", True)
74-
7572

7673
def _pandas_util_dir():
7774
# In pandas 2.0, pandas.util contains public APIs under
@@ -1713,10 +1710,6 @@ def holiday_calendar_factory_wrapper(*args, **kwargs):
17131710
)
17141711

17151712

1716-
def isinstance_cudf_pandas(obj, type):
1717-
return is_proxy_object(obj) and obj.__class__.__name__ == type.__name__
1718-
1719-
17201713
# timestamps and timedeltas are not proxied, but non-proxied
17211714
# pandas types are currently not picklable. Thus, we define
17221715
# custom reducer/unpicker functions for these types:

python/cudf/cudf/pandas/fast_slow_proxy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,10 @@ def _get_proxy_base_class(cls):
13351335
return object
13361336

13371337

1338+
def is_proxy_instance(obj, type):
1339+
return is_proxy_object(obj) and obj.__class__.__name__ == type.__name__
1340+
1341+
13381342
PROXY_BASE_CLASSES: set[type] = {
13391343
ProxyNDarrayBase,
13401344
}

python/cudf/cudf/pandas/module_accelerator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,10 @@ def install(
595595
)
596596
mode = deduce_cudf_pandas_mode(slow_lib, fast_lib)
597597
if mode.use_fast_lib:
598-
importlib.import_module(
598+
pandas_wrappers = importlib.import_module(
599599
f".._wrappers.{mode.slow_lib}", __name__
600600
)
601+
pandas_wrappers.cudf.set_option("mode.pandas_compatible", True)
601602
try:
602603
(self,) = (
603604
p

python/cudf/cudf_pandas_tests/test_cudf_pandas.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
)
6767

6868
from cudf.pandas import (
69-
isinstance_cudf_pandas,
69+
is_proxy_instance,
7070
)
7171

7272
# Accelerated pandas has the real pandas and cudf modules as attributes
@@ -1902,23 +1902,23 @@ def test_is_cudf_pandas():
19021902
df = xpd.DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]})
19031903
index = xpd.Index([1, 2, 3])
19041904

1905-
assert isinstance_cudf_pandas(s, pd.Series)
1906-
assert isinstance_cudf_pandas(df, pd.DataFrame)
1907-
assert isinstance_cudf_pandas(index, pd.Index)
1908-
assert isinstance_cudf_pandas(index.values, np.ndarray)
1905+
assert is_proxy_instance(s, pd.Series)
1906+
assert is_proxy_instance(df, pd.DataFrame)
1907+
assert is_proxy_instance(index, pd.Index)
1908+
assert is_proxy_instance(index.values, np.ndarray)
19091909

19101910
for obj in [s, df, index, index.values]:
1911-
assert not isinstance_cudf_pandas(obj._fsproxy_slow, pd.Series)
1912-
assert not isinstance_cudf_pandas(obj._fsproxy_fast, pd.Series)
1911+
assert not is_proxy_instance(obj._fsproxy_slow, pd.Series)
1912+
assert not is_proxy_instance(obj._fsproxy_fast, pd.Series)
19131913

1914-
assert not isinstance_cudf_pandas(obj._fsproxy_slow, pd.DataFrame)
1915-
assert not isinstance_cudf_pandas(obj._fsproxy_fast, pd.DataFrame)
1914+
assert not is_proxy_instance(obj._fsproxy_slow, pd.DataFrame)
1915+
assert not is_proxy_instance(obj._fsproxy_fast, pd.DataFrame)
19161916

1917-
assert not isinstance_cudf_pandas(obj._fsproxy_slow, pd.Index)
1918-
assert not isinstance_cudf_pandas(obj._fsproxy_fast, pd.Index)
1917+
assert not is_proxy_instance(obj._fsproxy_slow, pd.Index)
1918+
assert not is_proxy_instance(obj._fsproxy_fast, pd.Index)
19191919

1920-
assert not isinstance_cudf_pandas(obj._fsproxy_slow, np.ndarray)
1921-
assert not isinstance_cudf_pandas(obj._fsproxy_fast, np.ndarray)
1920+
assert not is_proxy_instance(obj._fsproxy_slow, np.ndarray)
1921+
assert not is_proxy_instance(obj._fsproxy_fast, np.ndarray)
19221922

19231923

19241924
def test_series_dtype_property():

0 commit comments

Comments
 (0)