Repository: modin-project/modin

Issue:
Pandas count(level=...) sorts index while Modin doesn't do this
### System information
- **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**: Linux Ubuntu 18.04
- **Modin version** (`modin.__version__`): 0.7.3+281.g4d80bd8
- **Python version**: 3.8.3
- **Code we can use to reproduce**:
```
import pandas
import modin.pandas as pd
import numpy as np

from modin.pandas.test.utils import test_data

df_modin, df_pandas = pd.DataFrame(test_data["dense_nan_data"]), pandas.DataFrame(test_data["dense_nan_data"])
rows_number = len(df_modin.index)
level_0 = np.random.choice([x for x in range(10)], rows_number)
level_1 = np.random.choice([x for x in range(10)], rows_number)
index = pandas.MultiIndex.from_arrays([level_0, level_1])

df_modin.index = index
df_pandas.index = index

print("df_modin.count(level=0).index", df_modin.count(level=0).index) # Int64Index([8, 2, 4, 0, 5, 6, 3, 7, 9, 1], dtype='int64')
print("df_pandas.count(level=0).index", df_pandas.count(level=0).index) # Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')
```
<!--
You can obtain the Modin version with

python -c "import modin; print(modin.__version__)"
-->

### Describe the problem
<!-- Describe the problem clearly here. -->

### Source code / logs
<!-- Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem. -->



Discussion:

Resolving patch:
diff --git a/modin/pandas/base.py b/modin/pandas/base.py
--- a/modin/pandas/base.py
+++ b/modin/pandas/base.py
@@ -119,14 +119,14 @@ def _update_inplace(self, new_query_compiler):
             sib._query_compiler = new_query_compiler
         old_query_compiler.free()
 
-    def _handle_level_agg(self, axis, level, op, **kwargs):
+    def _handle_level_agg(self, axis, level, op, sort=False, **kwargs):
         """Helper method to perform error checking for aggregation functions with a level parameter.
         Args:
             axis: The axis to apply the operation on
             level: The level of the axis to apply the operation on
             op: String representation of the operation to be performed on the level
         """
-        return getattr(self.groupby(level=level, axis=axis, sort=False), op)(**kwargs)
+        return getattr(self.groupby(level=level, axis=axis, sort=sort), op)(**kwargs)
 
     def _validate_other(
         self,
@@ -751,7 +751,7 @@ def count(self, axis=0, level=None, numeric_only=False):
                 # error thrown by pandas
                 raise TypeError("Can only count levels on hierarchical columns.")
 
-            return self._handle_level_agg(axis, level, "count")
+            return self._handle_level_agg(axis=axis, level=level, op="count", sort=True)
 
         return self._reduce_dimension(
             self._query_compiler.count(


Test patch:
diff --git a/modin/pandas/test/test_dataframe.py b/modin/pandas/test/test_dataframe.py
--- a/modin/pandas/test/test_dataframe.py
+++ b/modin/pandas/test/test_dataframe.py
@@ -31,6 +31,7 @@
     df_is_empty,
     arg_keys,
     name_contains,
+    test_data,
     test_data_values,
     test_data_keys,
     test_data_with_duplicates_values,
@@ -5364,6 +5365,26 @@ def test___len__(self, data):
 
         assert len(modin_df) == len(pandas_df)
 
+    def test_index_order(self):
+        # see #1708 and #1869 for details
+        df_modin, df_pandas = (
+            pd.DataFrame(test_data["dense_nan_data"]),
+            pandas.DataFrame(test_data["dense_nan_data"]),
+        )
+        rows_number = len(df_modin.index)
+        level_0 = np.random.choice([x for x in range(10)], rows_number)
+        level_1 = np.random.choice([x for x in range(10)], rows_number)
+        index = pandas.MultiIndex.from_arrays([level_0, level_1])
+
+        df_modin.index = index
+        df_pandas.index = index
+
+        for func in ["all", "any", "mad", "count"]:
+            df_equals(
+                getattr(df_modin, func)(level=0).index,
+                getattr(df_pandas, func)(level=0).index,
+            )
+
 
 class TestDataFrameIter:
     @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
diff --git a/modin/pandas/test/test_series.py b/modin/pandas/test/test_series.py
--- a/modin/pandas/test/test_series.py
+++ b/modin/pandas/test/test_series.py
@@ -1813,7 +1813,7 @@ def test_last():
 
 
 def test_index_order():
-    # see #1708 for details
+    # see #1708 and #1869 for details
     s_modin, s_pandas = create_test_series(test_data["dense_nan_data"])
     rows_number = len(s_modin.index)
     level_0 = np.random.choice([x for x in range(10)], rows_number)
@@ -1823,7 +1823,7 @@ def test_index_order():
     s_modin.index = index
     s_pandas.index = index
 
-    for func in ["all", "any", "mad"]:
+    for func in ["all", "any", "mad", "count"]:
         df_equals(
             getattr(s_modin, func)(level=0).index,
             getattr(s_pandas, func)(level=0).index,