Repository: unionai-oss/pandera

Issue:
Better error message when validating pandas.MultiIndex with a pandera.Index schema
**Describe the bug**
Trying to validate a MultiIndex data frame with a schema that Index has been set to single Index.

- [x] I have checked that this issue has not already been reported.
- [x] I have confirmed this bug exists on the latest version of pandera.
- [x] (optional) I have confirmed this bug exists on the master branch of pandera.


#### Code Sample, a copy-pastable example

```python
ind = pd.MultiIndex.from_tuples([('a', 'b'), ('c', 'd'), ('e', 'f')], names=(u'one', u'two'))
df = pd.DataFrame(columns=['alpha', 'beta'], index=ind)
schema = DataFrameSchema(index=Index(name='key'))
schema.validate(df)
```
Output:
```python
Error
Traceback (most recent call last):
  File "/home/baran/PycharmProjects/anofox/packages/component_default/anofox/utils/schema_decorators_test.py", line 136, in test_prod2
    schema.validate(df)
  File "/home/baran/PycharmProjects/anofox/.tox/component_default/lib/python3.8/site-packages/pandera/schemas.py", line 593, in validate
    result = schema_component(
  File "/home/baran/PycharmProjects/anofox/.tox/component_default/lib/python3.8/site-packages/pandera/schemas.py", line 1896, in __call__
    return self.validate(
  File "/home/baran/PycharmProjects/anofox/.tox/component_default/lib/python3.8/site-packages/pandera/schema_components.py", line 369, in validate
    obj_to_validate = pd.Series(
  File "/home/baran/PycharmProjects/anofox/.tox/component_default/lib/python3.8/site-packages/pandas/core/series.py", line 289, in __init__
    raise NotImplementedError(
NotImplementedError: initializing a Series from a MultiIndex is not supported

```

#### Expected behavior
If the index type does not match, it should raise a `SchemaError`. Alternatively, you can provide a way to validate a part of the multi-index.




Discussion:
you need to use a `pandera.MultiIndex` object if you want to validate a `pandas.MultiIndex`. This error reflects intended behavior, as it doesn't make sense to validate a multiindex index with a single index schema specification, though the error message can be better.

Gonna rename this issue and add the help wanted tag. A PR would be welcome!
Let me take a look at this issue.
- error is happening here: https://github.com/pandera-dev/pandera/blob/master/pandera/schema_components.py#L365-L381
- schema component tests: https://github.com/pandera-dev/pandera/blob/master/tests/core/test_schema_components.py
hey @jekwatt just checking in, let me know if you have any questions or need help with anything!
Hi @cosmicBboy I made my local branch and did my local update but have not pushed commits yet.  I am comfortable creating a PR and will create a PR for this issue when ready.  Thank you for all the help with getting started on pandera.  I love how you did data validation checks using hypothesis testing.

Resolving patch:
diff --git a/pandera/schema_components.py b/pandera/schema_components.py
--- a/pandera/schema_components.py
+++ b/pandera/schema_components.py
@@ -354,6 +354,11 @@ def validate(
             otherwise creates a copy of the data.
         :returns: validated DataFrame or Series.
         """
+        if isinstance(check_obj.index, pd.MultiIndex):
+            raise errors.SchemaError(
+                self, check_obj, "Attempting to validate mismatch index"
+            )
+
         if self.coerce:
             check_obj.index = self.coerce_dtype(check_obj.index)
             # handles case where pandas native string type is not supported


Test patch:
diff --git a/tests/core/test_schema_components.py b/tests/core/test_schema_components.py
--- a/tests/core/test_schema_components.py
+++ b/tests/core/test_schema_components.py
@@ -179,6 +179,21 @@ def test_multi_index_index():
         schema.validate(df_fail)
 
 
+def test_single_index_multi_index_mismatch():
+    """Tests the failure case that attempting to validate a MultiIndex DataFrame
+    against a single index schema raises a SchemaError with a constructive error
+    message."""
+    ind = pd.MultiIndex.from_tuples(
+        [("a", "b"), ("c", "d"), ("e", "f")],
+        names=("one", "two"),
+    )
+    df_fail = pd.DataFrame(index=ind)
+    schema = DataFrameSchema(index=Index(name="key"))
+
+    with pytest.raises(errors.SchemaError):
+        schema.validate(df_fail)
+
+
 def test_multi_index_schema_coerce():
     """Test that multi index can be type-coerced."""
     indexes = [