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

FIX-#1906: fixed incorrect behaviour of 'groupby.__getattr__' #2276

Merged
merged 1 commit into from
Oct 20, 2020
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
2 changes: 1 addition & 1 deletion modin/pandas/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __getattr__(self, key):
return object.__getattribute__(self, key)
except AttributeError as e:
if key in self._columns:
return self._default_to_pandas(lambda df: df.__getitem__(key))
return self.__getitem__(key)
raise e

@property
Expand Down
35 changes: 24 additions & 11 deletions modin/pandas/test/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import numpy as np
import modin.pandas as pd
from modin.utils import try_cast_to_pandas, get_current_backend
from modin.pandas.utils import from_pandas
from modin.pandas.utils import from_pandas, is_scalar
from .utils import (
df_equals,
check_df_columns_have_nans,
Expand Down Expand Up @@ -49,6 +49,17 @@ def eval_aggregation(md_df, pd_df, operation=None, by=None, *args, **kwargs):
)


def build_types_asserter(comparator):
def wrapper(obj1, obj2, *args, **kwargs):
error_str = f"obj1 and obj2 has incorrect types: {type(obj1)} and {type(obj2)}"
assert not (is_scalar(obj1) ^ is_scalar(obj2)), error_str
assert obj1.__module__.split(".")[0] == "modin", error_str
assert obj2.__module__.split(".")[0] == "pandas", error_str
comparator(obj1, obj2, *args, **kwargs)

return wrapper


@pytest.mark.parametrize("as_index", [True, False])
def test_mixed_dtypes_groupby(as_index):
frame_data = np.random.randint(97, 198, size=(2 ** 6, 2 ** 4))
Expand Down Expand Up @@ -1038,16 +1049,18 @@ def eval_quantile(modin_groupby, pandas_groupby):


def eval___getattr__(modin_groupby, pandas_groupby, item):
try:
pandas_groupby = pandas_groupby[item]
pandas_result = pandas_groupby.count()
except Exception as e:
with pytest.raises(type(e)):
modin_groupby[item].count()
else:
modin_groupby = modin_groupby[item]
modin_result = modin_groupby.count()
df_equals(modin_result, pandas_result)
eval_general(
modin_groupby,
pandas_groupby,
lambda grp: grp[item].count(),
comparator=build_types_asserter(df_equals),
)
eval_general(
modin_groupby,
pandas_groupby,
lambda grp: getattr(grp, item).count(),
comparator=build_types_asserter(df_equals),
)


def eval_groups(modin_groupby, pandas_groupby):
Expand Down