TST: Improved test coverage for Styler.bar error conditions
State: closed

- [x]  closes #56283  

- Added two new unit tests `test_bar_color_and_cmap_error_raises()` and `test_bar_invalid_color_type_error_raises()` to improve the coverage of `Styler.bar` where the issue mentioned, ensuring the method raises the appropriate `ValueError`.
- Implemented a solution using `df.min/max` before `np.nanmin/nanmax` to address issues with NumPy's inability to properly handle `pd.NA` under certain conditions, and added `test_styler_bar_with_NA_values()` for testing.

Labels:
name: Styler
description: conditional formatting using DataFrame.style

Comments:
> Do these tests relate to a different issue? #56283 is to test that this doesn't raise for pyarrow types

Hi @mroeschke, this is a simple PR that covers the test for the raise in `bar()` method. 

Since you mentioned, I did a deep dive into this issue, and found that this issue does exist on the main(2.1.3). The `pyarrow` will read the null value as an `NAType` in array, which may not be resolved properly by the `np.nanmin()` or `np.nanmax()` in certain circumstances. You will see this issue as long as you render the result, or call the `to_html()` method, they all give the same errors. 

To address this, I've implemented a method replacing `pd.NA` with `np.nan`, and is applied only before executing `np.nanmin()` or `np.nanmax()`, to resolve the issue.

Thanks for your review, and I look forward to your response!

main is not a released version. As of pandas https://github.com/pandas-dev/pandas/commit/5e4f2b27db3c7363041b2d969921454b63279e26 I get

```python
In [1]: import pandas as pd

In [2]: import io
   ...: data = '''name,age,test1,test2,teacher
   ...: Adam,15,95.0,80,Ashby
   ...: Bob,16,81.0,82,Ashby
   ...: Dave,16,89.0,84,Jones
   ...: Fred,15,,88,Jones'''
   ...: scores = pd.read_csv(io.StringIO(data), dtype_backend='pyarrow',
   ...:                     engine='pyarrow'
   ...:                     )
   ...: 
   ...: (scores
   ...: .style.bar(subset='test1')
   ...: )
Out[2]: <pandas.io.formats.style.Styler at 0x1078cdf90>

In [3]: pd.__version__
Out[3]: '2.2.0.dev0+821.g5e4f2b27db'
```
> main is not a released version. As of pandas [5e4f2b2](https://github.com/pandas-dev/pandas/commit/5e4f2b27db3c7363041b2d969921454b63279e26) I get
> 
> ```python
> In [1]: import pandas as pd
> 
> In [2]: import io
>    ...: data = '''name,age,test1,test2,teacher
>    ...: Adam,15,95.0,80,Ashby
>    ...: Bob,16,81.0,82,Ashby
>    ...: Dave,16,89.0,84,Jones
>    ...: Fred,15,,88,Jones'''
>    ...: scores = pd.read_csv(io.StringIO(data), dtype_backend='pyarrow',
>    ...:                     engine='pyarrow'
>    ...:                     )
>    ...: 
>    ...: (scores
>    ...: .style.bar(subset='test1')
>    ...: )
> Out[2]: <pandas.io.formats.style.Styler at 0x1078cdf90>
> 
> In [3]: pd.__version__
> Out[3]: '2.2.0.dev0+821.g5e4f2b27db'
> ```

Hi @mroeschke , you may see the error with `scores.style.bar(subset='test1').to_html()` both in 2.1.3 and in main not release version. Please have a check.
pre-commit.ci autofix
Hello @mroeschke @attack68 , I've updated my PR and would appreciate it if you could take a review. Please let me know if there are any further changes needed. If everything looks good, could you assist with merging? Thanks for your time and help!
It is possible to create a separate issue for these other identified cases and push two different PRs.
Hi @attack68 , thanks for your valuable feedback. I have tested with `align="mean"` and got a similar error.

This PR was initially meant to provide a test for issue #56283 , but during the code analysis, I discovered that `np.nanmax` and `np.nanmin` can not work on `pd.NA`. If we don't iterate through data to convert `pd.NA` to `np.nan`, using `pd.max` and `pd.min` - which can handle `pd.NA` - seems to be the most clear and concise approach. However, they return `Series` or `DataFrame`, which differ in type from the numpy functions' return values (like `numpy.int64`). Hence, I used nesting to ensure the return types are consistent after modification.

Regarding your concerns about `align` and `cmap`, I am more than willing to address them in separate PRs to avoid overcomplicating this one. We can create a new issue to track it.

Thanks for your help and suggestions!
LGTM.
Please add the new issues before merging this PR. Thx,
@attack68 I have created the issue #56425 . Please help me merge this one. Thank you very much!
Hi @mroeschke I'm writing to gently follow up on my recent PR #56341. When you have a moment, could you please take a look or help me merge it? Appreciate your time and assistance.
Thanks @ccccjone