Skip to content

Commit

Permalink
FIX-#7233: Display property name in default_to_pandas error messages (#…
Browse files Browse the repository at this point in the history
…7269)

Signed-off-by: Jonathan Shi <[email protected]>
  • Loading branch information
noloerino authored May 15, 2024
1 parent e74ac5d commit 12f6a3c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
11 changes: 9 additions & 2 deletions modin/core/dataframe/algebra/default2pandas/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ def register(cls, func, obj_type=None, inplace=None, fn_name=None):
Function that takes query compiler, does fallback to pandas and applies `func`
to the casted to pandas frame or its property accesed by ``cls.frame_wrapper``.
"""
fn_name = getattr(func, "__name__", str(func)) if fn_name is None else fn_name

if isinstance(func, str):
if obj_type is None:
obj_type = cls.DEFAULT_OBJECT_TYPE
Expand All @@ -104,7 +102,16 @@ def register(cls, func, obj_type=None, inplace=None, fn_name=None):
fn = func

if type(fn) is property:
if fn_name is None and hasattr(fn, "fget"):
# When `fn` is a property, `str(fn)` will be something like
# "<property object at 0x7f8671e09d10>". We instead check its `fget` method to get
# the name of the property.
# Note that this method is still imperfect because we cannot get the class name
# of the property. For example, we can only get "hour" from `Series.dt.hour`.
fn_name = f"<property fget:{getattr(fn.fget, '__name__', 'noname')}>"
fn = cls.build_property_wrapper(fn)
else:
fn_name = getattr(fn, "__name__", str(fn)) if fn_name is None else fn_name

def applyier(df, *args, **kwargs):
"""
Expand Down
28 changes: 28 additions & 0 deletions modin/tests/core/storage_formats/pandas/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
context,
)
from modin.core.dataframe.algebra import Fold
from modin.core.dataframe.algebra.default2pandas import DataFrameDefault
from modin.core.dataframe.pandas.dataframe.dataframe import PandasDataframe
from modin.core.dataframe.pandas.dataframe.utils import ColumnInfo, ShuffleSortFunctions
from modin.core.dataframe.pandas.metadata import (
Expand Down Expand Up @@ -2757,3 +2758,30 @@ def filter_modin_dataframe2(df):
filtered_df = modin_df.filter_dataframe2()

df_equals(filtered_df, expected_df)


def test_default_property_warning_name():
# Test that when a property defaults to pandas, the raised warning mentions the full name of
# the pandas property rather than a hex address

@property
def _test_default_property(df):
return "suspicious sentinel value"

@property
def qc_test_default_property(qc):
return DataFrameDefault.register(_test_default_property)(qc)

PandasQueryCompiler.qc_test_default_property = qc_test_default_property

@property
def dataframe_test_default_property(df):
return df._query_compiler.qc_test_default_property

pd.DataFrame.dataframe_test_default_property = dataframe_test_default_property

with pytest.warns(
UserWarning,
match="<function DataFrame.<property fget:_test_default_property>> is not currently supported",
):
pd.DataFrame([[1]]).dataframe_test_default_property

0 comments on commit 12f6a3c

Please sign in to comment.