Repository: unionai-oss/pandera

Issue:
Confusing message on coercion failure due to chained exceptions (Could not coerce <..> data_container into type ..)
When coercion fails, e.g. in the following script:

```python
import pandas as pd
import pandera as pa
df = pd.DataFrame({'col': [1, '']})
schema = pa.DataFrameSchema(columns={"col": pa.Column("float64", nullable=True)}, coerce=True)
df = schema.validate(df)
df
```

...the user gets a huge traceback (below), which consists of three "chained" exceptions:
* A useful, if vague `ValueError: could not convert string to float: ''`
* followed by a meaningless `pandera.errors.ParserError: Could not coerce <class 'pandas.core.series.Series'> data_container into type float64`
* followed by `pandera.errors.SchemaError: Error while coercing 'col' to type float64: Could not coerce <class 'pandas.core.series.Series'> data_container into type float64`

This confused me greatly, as a new user of pandera, because:
* My instinct was not to read through the traceback carefully, but instead scroll right to the end.
* I assumed that "nullable=True" would convert an empty string to a NULL/NaN
* Since I knew there were no other odd values in my column, the message about trying to convert a `Series` to `float` led me to believe that I messed the schema definition somehow (surely it should be trying to convert _string_ to float, not a _series_, I thought)
* ... and the error message was not on Google

Are there any changes that could be made to improve the developer experience in this case or should I just be more careful?

I was thinking along the lines of removing the exception chaining and including the root error message in the message instead (based on `exc.failure_cases`?)

Was `''`->NaN coercion already discussed or should I open a separate issue about it?

---
The complete traceback from the example above:
```
Traceback (most recent call last):
  File "...\lib\site-packages\pandera\engines\numpy_engine.py", line 58, in try_coerce
    return self.coerce(data_container)
  File "...\lib\site-packages\pandera\engines\numpy_engine.py", line 48, in coerce
    coerced = data_container.astype(self.type)
  File "...\lib\site-packages\pandas\core\generic.py", line 5815, in astype
    new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
  File "...\lib\site-packages\pandas\core\internals\managers.py", line 418, in astype
    return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
  File "...\lib\site-packages\pandas\core\internals\managers.py", line 327, in apply
    applied = getattr(b, f)(**kwargs)
  File "...\lib\site-packages\pandas\core\internals\blocks.py", line 591, in astype
    new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
  File "...\lib\site-packages\pandas\core\dtypes\cast.py", line 1309, in astype_array_safe
    new_values = astype_array(values, dtype, copy=copy)
  File "...\lib\site-packages\pandas\core\dtypes\cast.py", line 1257, in astype_array
    values = astype_nansafe(values, dtype, copy=copy)
  File "...\lib\site-packages\pandas\core\dtypes\cast.py", line 1201, in astype_nansafe
    return arr.astype(dtype, copy=True)
ValueError: could not convert string to float: ''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "...\lib\site-packages\pandera\schemas.py", line 1765, in coerce_dtype
    return self.dtype.try_coerce(obj)
  File "...\lib\site-packages\pandera\engines\numpy_engine.py", line 60, in try_coerce
    raise errors.ParserError(
pandera.errors.ParserError: Could not coerce <class 'pandas.core.series.Series'> data_container into type float64

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Nickolay.Ponomarev\Documents\work\INHOUSE\_tasks\centihede\pandera_test.py", line 6, in <module>
    df = schema.validate(df)
  File "...\lib\site-packages\pandera\schemas.py", line 485, in validate
    return self._validate(
  File "...\lib\site-packages\pandera\schemas.py", line 623, in _validate
    raise schema_error_dict["error"]
  File "...\lib\site-packages\pandera\schemas.py", line 354, in _try_coercion
    return coerce_fn(obj)
  File "...\lib\site-packages\pandera\schema_components.py", line 151, in coerce_dtype
    return super(Column, self).coerce_dtype(obj)
  File "...\lib\site-packages\pandera\schemas.py", line 1771, in coerce_dtype
    raise errors.SchemaError(
pandera.errors.SchemaError: Error while coercing 'col' to type float64: Could not coerce <class 'pandas.core.series.Series'> data_container into type float64
```


Discussion:

Resolving patch:
diff --git a/pandera/schemas.py b/pandera/schemas.py
--- a/pandera/schemas.py
+++ b/pandera/schemas.py
@@ -1784,7 +1784,7 @@ def coerce_dtype(self, obj: Union[pd.Series, pd.Index]) -> pd.Series:
         except errors.ParserError as exc:
             msg = (
                 f"Error while coercing '{self.name}' to type "
-                f"{self.dtype}: {exc}"
+                f"{self.dtype}: {exc}:\n{exc.failure_cases}"
             )
             raise errors.SchemaError(
                 self,


Test patch: