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] Allow capturing cudf specific exceptions in cudf.pandas by using ExceptionGroup in Python 3.11 #14379

Open
mroeschke opened this issue Nov 8, 2023 · 2 comments
Labels
cudf.pandas Issues specific to cudf.pandas feature request New feature or request Python Affects Python cuDF API.

Comments

@mroeschke
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently when using cudf.pandas, there's no way to capture or act on the cudf exception thrown whether the operation succeeds or fails on during the pandas path.

In [1]: %load_ext cudf.pandas

In [2]: import pandas as pd

In [3]: try:
   ...:    pd.to_datetime("2020-01-01", utc=True)
   ...: except NotImplementedError:
   ...:     print("hello")

In [4]: import cudf

In [5]: cudf.to_datetime("2020-01-01", utc=True)
NotImplementedError: utc is not yet implemented

Describe the solution you'd like

If _fast_slow_function_call was structured like:

In [8]: def f():
   ...:     try:
   ...:         raise NotImplementedError("fast doesn't work")
   ...:     except Exception as err_fast:
   ...:         try:
   ...:             raise ValueError("Slow doesn't work")
   ...:         except Exception as err_slow:
   ...:             raise ExceptionGroup("Fast and slow did't work", [err_fast, err_slow])
   ...: 

In [9]: f()
Traceback (most recent call last):
  File "<ipython-input-8-f0c78d1a1137>", line 6, in f
    raise ValueError("Slow doesn't work")
ValueError: Slow doesn't work

During handling of the above exception, another exception occurred:

  + Exception Group Traceback (most recent call last):
  |   File "/opt/miniconda3/envs/pandas-dev/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3548, in run_code
  |     exec(code_obj, self.user_global_ns, self.user_ns)
  |   File "<ipython-input-9-c43e34e6d405>", line 1, in <module>
  |     f()
  |   File "<ipython-input-8-f0c78d1a1137>", line 8, in f
  |     raise ExceptionGroup("Fast and slow did't work", [err_fast, err_slow])
  | ExceptionGroup: Fast and slow did't work (2 sub-exceptions)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "<ipython-input-8-f0c78d1a1137>", line 3, in f
    |     raise NotImplementedError("fast doesn't work")
    | NotImplementedError: fast doesn't work
    +---------------- 2 ----------------
    | Traceback (most recent call last):
    |   File "<ipython-input-8-f0c78d1a1137>", line 6, in f
    |     raise ValueError("Slow doesn't work")
    | ValueError: Slow doesn't work
    +------------------------------------

In [10]: try:
    ...:     f()
    ...: except* NotImplementedError:
    ...:     print("Try doing something else")
    ...: 
Try doing something else
  + Exception Group Traceback (most recent call last):
  |   File "/opt/miniconda3/envs/pandas-dev/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3548, in run_code
  |     exec(code_obj, self.user_global_ns, self.user_ns)
  |   File "<ipython-input-10-e0e980fb6b7c>", line 2, in <module>
  |     f()
  |   File "<ipython-input-8-f0c78d1a1137>", line 8, in f
  |     raise ExceptionGroup("Fast and slow did't work", [err_fast, err_slow])
  | ExceptionGroup: Fast and slow did't work (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "<ipython-input-8-f0c78d1a1137>", line 3, in f
    |     raise NotImplementedError("fast doesn't work")
    | NotImplementedError: fast doesn't work
    | 
    | During handling of the above exception, another exception occurred:
    | 
    | Traceback (most recent call last):
    |   File "<ipython-input-8-f0c78d1a1137>", line 6, in f
    |     raise ValueError("Slow doesn't work")
    | ValueError: Slow doesn't work
    +------------------------------------

Additional context

Admittedly I don't have a definitive use case where capturing the cudf exception is necessary, and using ExceptionGroup might break "drop in replacement" potential of cudf.pandas, but noting that this is a limitation in the current design

@mroeschke mroeschke added feature request New feature or request Python Affects Python cuDF API. labels Nov 8, 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
@Matt711
Copy link
Contributor

Matt711 commented Jun 13, 2024

After #15910, if you set the fallback environment variable you get a warning.

In [1]: %load_ext cudf.pandas

In [2]: import pandas as pd

In [3]: import os

In [4]: os.environ["CUDF_PANDAS_FALLBACK_DEBUGGING"]="True"

In [5]: pd.to_datetime("2020-01-01", utc=True)
/cudf/python/cudf/cudf/pandas/fast_slow_proxy.py:995: CudfPandasDebugTypeErrorWarning: TypeError. Falling back to the slow path. The exception was Cannot interpret 'datetime64[ns, UTC]' as a data type.
  warnings.warn(
Out[5]: /cudf/python/cudf/cudf/pandas/fast_slow_proxy.py:983: CudfPandasDebugNotImplementedErrorWarning: NotImplementedError. Falling back to the slow path. The exception was Fast implementation not available. Falling back to the slow implementation.
  warnings.warn(
Timestamp('2020-01-01 00:00:00+0000', tz='UTC')

@mroeschke
Copy link
Contributor Author

Thanks for checking @Matt711. This is a good intermediate state towards this issue, still would be nice to use ExceptionGroups in the future so exceptions can be explicitly caught. But noting here that as of now this is a low priority

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cudf.pandas Issues specific to cudf.pandas feature request New feature or request Python Affects Python cuDF API.
Projects
Status: In Progress
Development

No branches or pull requests

3 participants