| 1 | Support nullable pandas dtypes in `unique_labels` |
| 2 | ### Describe the workflow you want to enable |
| 3 | |
| 4 | I would like to be able to pass the nullable pandas dtypes ("Int64", "Float64", "boolean") into sklearn's `unique_labels` function. Because the dtypes become `object` dtype when converted to numpy arrays we get `ValueError: Mix type of y not allowed, got types {'binary', 'unknown'}`: |
| 5 | |
| 6 | Repro with sklearn 1.2.1 |
| 7 | ```py |
| 8 | import pandas as pd |
| 9 | import pytest |
| 10 | from sklearn.utils.multiclass import unique_labels |
| 11 | |
| 12 | for dtype in ["Int64", "Float64", "boolean"]: |
| 13 | y_true = pd.Series([1, 0, 0, 1, 0, 1, 1, 0, 1], dtype=dtype) |
| 14 | y_predicted = pd.Series([0, 0, 1, 1, 0, 1, 1, 1, 1], dtype="int64") |
| 15 | |
| 16 | with pytest.raises(ValueError, match="Mix type of y not allowed, got types"): |
| 17 | unique_labels(y_true, y_predicted) |
| 18 | ``` |
| 19 | |
| 20 | ### Describe your proposed solution |
| 21 | |
| 22 | We should get the same behavior as when `int64`, `float64`, and `bool` dtypes are used, which is no error: |
| 23 | |
| 24 | ```python |
| 25 | import pandas as pd |
| 26 | from sklearn.utils.multiclass import unique_labels |
| 27 | |
| 28 | for dtype in ["int64", "float64", "bool"]: |
| 29 | y_true = pd.Series([1, 0, 0, 1, 0, 1, 1, 0, 1], dtype=dtype) |
| 30 | y_predicted = pd.Series([0, 0, 1, 1, 0, 1, 1, 1, 1], dtype="int64") |
| 31 | |
| 32 | unique_labels(y_true, y_predicted) |
| 33 | ``` |
| 34 | |
| 35 | ### Describe alternatives you've considered, if relevant |
| 36 | |
| 37 | Our current workaround is to convert the data to numpy arrays with the corresponding dtype that works prior to passing it into `unique_labels`. |
| 38 | |
| 39 | ### Additional context |
| 40 | |
| 41 | _No response_ |