Skip to content

Commit

Permalink
FIX-#2305: fix handling of renaming aggregation (#2732)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Chigarev <[email protected]>
  • Loading branch information
dchigarev authored Feb 15, 2021
1 parent f14a3c1 commit 04cd912
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 8 additions & 8 deletions modin/pandas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,22 +517,22 @@ def aggregate(self, func=None, axis=0, *args, **kwargs):

agg = aggregate

def _aggregate(self, arg, *args, **kwargs):
def _aggregate(self, func, *args, **kwargs):
_axis = kwargs.pop("_axis", 0)
kwargs.pop("_level", None)

if isinstance(arg, str):
if isinstance(func, str):
kwargs.pop("is_transform", None)
return self._string_function(arg, *args, **kwargs)
return self._string_function(func, *args, **kwargs)

# Dictionaries have complex behavior because they can be renamed here.
elif isinstance(arg, dict):
return self._default_to_pandas("agg", arg, *args, **kwargs)
elif is_list_like(arg) or callable(arg):
elif func is None or isinstance(func, dict):
return self._default_to_pandas("agg", func, *args, **kwargs)
elif is_list_like(func) or callable(func):
kwargs.pop("is_transform", None)
return self.apply(arg, axis=_axis, args=args, **kwargs)
return self.apply(func, axis=_axis, args=args, **kwargs)
else:
raise TypeError("type {} is not callable".format(type(arg)))
raise TypeError("type {} is not callable".format(type(func)))

def _string_function(self, func, *args, **kwargs):
assert isinstance(func, str)
Expand Down
12 changes: 12 additions & 0 deletions modin/pandas/test/dataframe/test_udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
matplotlib.use("Agg")


def test_agg_dict():
md_df, pd_df = create_test_dfs(test_data_values[0])
agg_dict = {pd_df.columns[0]: "sum", pd_df.columns[-1]: ("sum", "count")}
eval_general(md_df, pd_df, lambda df: df.agg(agg_dict), raising_exceptions=True)

agg_dict = {
"new_col1": (pd_df.columns[0], "sum"),
"new_col2": (pd_df.columns[-1], "count"),
}
eval_general(md_df, pd_df, lambda df: df.agg(**agg_dict), raising_exceptions=True)


@pytest.mark.parametrize("axis", [0, 1])
@pytest.mark.parametrize(
"func",
Expand Down

0 comments on commit 04cd912

Please sign in to comment.