Skip to content

Commit

Permalink
Fix discoverability of submodules inside pd.util (#17215)
Browse files Browse the repository at this point in the history
Fixes: #17166 

This PR fixes the discoverability of the submodules of attributes and modules inside `pd.util`. Somehow `importlib.import_module("pandas.util").__dict__` doesn't display submodules and only root level attributes.

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - Matthew Murray (https://github.com/Matt711)

URL: #17215
  • Loading branch information
galipremsagar authored Nov 4, 2024
1 parent a2001dd commit 1d25d14
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
28 changes: 21 additions & 7 deletions python/cudf/cudf/pandas/_wrappers/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,27 @@ def _pandas_util_dir():
# In pandas 2.0, pandas.util contains public APIs under
# __getattr__ but no __dir__ to find them
# https://github.com/pandas-dev/pandas/blob/2.2.x/pandas/util/__init__.py
return list(importlib.import_module("pandas.util").__dict__.keys()) + [
"hash_array",
"hash_pandas_object",
"Appender",
"Substitution",
"cache_readonly",
]
res = list(
set(
list(importlib.import_module("pandas.util").__dict__.keys())
+ [
"Appender",
"Substitution",
"_exceptions",
"_print_versions",
"cache_readonly",
"hash_array",
"hash_pandas_object",
"version",
"_tester",
"_validators",
"_decorators",
]
)
)
if cudf.core._compat.PANDAS_GE_220:
res.append("capitalize_first_letter")
return res


pd.util.__dir__ = _pandas_util_dir
Expand Down
18 changes: 18 additions & 0 deletions python/cudf/cudf_pandas_tests/test_cudf_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,3 +1759,21 @@ def test_fallback_raises_error(monkeypatch):
monkeycontext.setenv("CUDF_PANDAS_FAIL_ON_FALLBACK", "True")
with pytest.raises(ProxyFallbackError):
pd.Series(range(2)).astype(object)


@pytest.mark.parametrize(
"attrs",
[
"_exceptions",
"version",
"_print_versions",
"capitalize_first_letter",
"_validators",
"_decorators",
],
)
def test_cudf_pandas_util_version(attrs):
if not PANDAS_GE_220 and attrs == "capitalize_first_letter":
assert not hasattr(pd.util, attrs)
else:
assert hasattr(pd.util, attrs)

0 comments on commit 1d25d14

Please sign in to comment.