Repository: modin-project/modin

Issue:
TEST: test_groupby.py:test_not_str_by seems to unintentionally be testing an empty dataframe
The data's column names don't match the dataframe's column names, so the dataframe is empty: https://github.com/modin-project/modin/blob/3cc33a2be050a2472fd084ccc3b9bbb33986f770/modin/pandas/test/test_groupby.py#L1928-L1929

I don't think this test was meant to test empty dataframes. I happened to find out that the dataframe was empty when I tested execution with a query compiler that had `lazy_execution` as `True`, so the groupby was not defaulting to pandas any more.

Note that when I use data to make a real frame, e.g. with `data = np.arange(5 * 5).reshape(5, 5)`, `BaseOnPython` execution has several failures:

```
FAILED modin/pandas/test/test_groupby.py::test_not_str_by[True-by4] - TypeError: You have to supply one of 'by' and 'level'
FAILED modin/pandas/test/test_groupby.py::test_not_str_by[True-by6] - KeyError: '[nan] not in index'
FAILED modin/pandas/test/test_groupby.py::test_not_str_by[False-0] - TypeError: object of type 'int' has no len()
FAILED modin/pandas/test/test_groupby.py::test_not_str_by[False-1.5] - TypeError: object of type 'float' has no len()
FAILED modin/pandas/test/test_groupby.py::test_not_str_by[False-by3] - TypeError: object of type 'Timestamp' has no len()
FAILED modin/pandas/test/test_groupby.py::test_not_str_by[False-by4] - TypeError: You have to supply one of 'by' and 'level'
FAILED modin/pandas/test/test_groupby.py::test_not_str_by[False-by5] - ValueError: cannot insert 0, already exists
FAILED modin/pandas/test/test_groupby.py::test_not_str_by[False-by6] - KeyError: '[nan] not in index'
```

Some things that I think are going wrong: 
- setting `by` to `by.name` [here](https://github.com/modin-project/modin/blob/f492ba9888fc05ff7c224db8a22faac8c0106a4b/modin/core/dataframe/algebra/default2pandas/groupby.py#L202-L208), so we set `by` to `None`, which is illegal, so we get `You have to supply one of 'by' and 'level'`
- pandas sometimes converts the `None` in the index to `nan` and sometimes not.

cc @dchigarev who is on the git blame for this test case.


Discussion:
Yep, it seems that the test actually tests nothing because of my mistake. I'll assign myself to the issue in order to review failing tests and create according trackers (and probably try to make quick fixes myself)

Resolving patch:
diff --git a/modin/core/dataframe/algebra/default2pandas/groupby.py b/modin/core/dataframe/algebra/default2pandas/groupby.py
--- a/modin/core/dataframe/algebra/default2pandas/groupby.py
+++ b/modin/core/dataframe/algebra/default2pandas/groupby.py
@@ -205,7 +205,7 @@ def fn(
                 and by.name in df
                 and df[by.name].equals(by)
             ):
-                by = by.name
+                by = [by.name]
             if isinstance(by, pandas.DataFrame):
                 df = pandas.concat([df] + [by[[o for o in by if o not in df]]], axis=1)
                 by = list(by.columns)
@@ -217,12 +217,12 @@ def fn(
             grp = df.groupby(by, axis=axis, **groupby_kwargs)
             func = cls.get_func(agg_func, **kwargs)
             result = func(grp, *agg_args, **agg_kwargs)
+            method = kwargs.get("method")
 
             if isinstance(result, pandas.Series):
-                result = result.to_frame()
+                result = result.to_frame(MODIN_UNNAMED_SERIES_LABEL)
 
             if not as_index:
-                method = kwargs.get("method")
                 if isinstance(by, pandas.Series):
                     # 1. If `drop` is True then 'by' Series represents a column from the
                     #    source frame and so the 'by' is internal.
diff --git a/modin/core/storage_formats/base/query_compiler.py b/modin/core/storage_formats/base/query_compiler.py
--- a/modin/core/storage_formats/base/query_compiler.py
+++ b/modin/core/storage_formats/base/query_compiler.py
@@ -2513,7 +2513,7 @@ def groupby_size(
         agg_kwargs,
         drop=False,
     ):
-        return GroupByDefault.register(pandas.core.groupby.DataFrameGroupBy.size)(
+        result = GroupByDefault.register(pandas.core.groupby.DataFrameGroupBy.size)(
             self,
             by=by,
             axis=axis,
@@ -2523,6 +2523,10 @@ def groupby_size(
             drop=drop,
             method="size",
         )
+        if not groupby_kwargs.get("as_index", False):
+            # Renaming 'MODIN_UNNAMED_SERIES_LABEL' to a proper name
+            result.columns = result.columns[:-1].append(pandas.Index(["size"]))
+        return result
 
     @doc_utils.add_refer_to("GroupBy.aggregate")
     def groupby_agg(
diff --git a/modin/pandas/groupby.py b/modin/pandas/groupby.py
--- a/modin/pandas/groupby.py
+++ b/modin/pandas/groupby.py
@@ -664,7 +664,6 @@ def size(self):
         if not isinstance(result, Series):
             result = result.squeeze(axis=1)
         if not self._kwargs.get("as_index") and not isinstance(result, Series):
-            result = result.rename(columns={0: "size"})
             result = (
                 result.rename(columns={MODIN_UNNAMED_SERIES_LABEL: "index"})
                 if MODIN_UNNAMED_SERIES_LABEL in result.columns


Test patch:
diff --git a/modin/pandas/test/test_groupby.py b/modin/pandas/test/test_groupby.py
--- a/modin/pandas/test/test_groupby.py
+++ b/modin/pandas/test/test_groupby.py
@@ -1828,18 +1828,16 @@ def test_multi_column_groupby_different_partitions(
         1.5,
         "str",
         pandas.Timestamp("2020-02-02"),
-        [None],
         [0, "str"],
-        [None, 0],
         [pandas.Timestamp("2020-02-02"), 1.5],
     ],
 )
 @pytest.mark.parametrize("as_index", [True, False])
 def test_not_str_by(by, as_index):
-    data = {f"col{i}": np.arange(5) for i in range(5)}
-    columns = pandas.Index([0, 1.5, "str", pandas.Timestamp("2020-02-02"), None])
+    columns = pandas.Index([0, 1.5, "str", pandas.Timestamp("2020-02-02")])
+    data = {col: np.arange(5) for col in columns}
+    md_df, pd_df = create_test_dfs(data)
 
-    md_df, pd_df = create_test_dfs(data, columns=columns)
     md_grp, pd_grp = (
         md_df.groupby(by, as_index=as_index),
         pd_df.groupby(by, as_index=as_index),