Skip to content

BUG: fix check_array on pandas Series with custom dtype (eg categorical) #12706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v0.20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ Changelog
function to return 0 when two all-zero vectors are compared.
:issue:`12685` by :user:`Thomas Fan <thomasjpfan>`.

:mod:`sklearn.utils`
....................

- |Fix| Calling :func:`utils.check_array` on `pandas.Series` with categorical
data, which raised an error in 0.20.0, now returns the expected output again.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

categorical data -> something like pandas custom dtype? though I'm not able to come up with another application scenario.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I seem to have missed this comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not important :)

:issue:`12699` by `Joris Van den Bossche`_.

.. _changes_0_20_1:

Version 0.20.1
Expand Down
5 changes: 5 additions & 0 deletions sklearn/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,11 @@ def test_check_array_series():
warn_on_dtype=True)
assert_array_equal(res, np.array([1, 2, 3]))

# with categorical dtype (not a numpy dtype) (GH12699)
s = pd.Series(['a', 'b', 'c']).astype('category')
res = check_array(s, dtype=None, ensure_2d=False)
assert_array_equal(res, np.array(['a', 'b', 'c'], dtype=object))


def test_check_dataframe_warns_on_dtype():
# Check that warn_on_dtype also works for DataFrames.
Expand Down
2 changes: 1 addition & 1 deletion sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def check_array(array, accept_sparse=False, accept_large_sparse=True,
# check if the object contains several dtypes (typically a pandas
# DataFrame), and store them. If not, store None.
dtypes_orig = None
if hasattr(array, "dtypes") and len(array.dtypes):
if hasattr(array, "dtypes") and hasattr(array.dtypes, '__array__'):
dtypes_orig = np.array(array.dtypes)

if dtype_numeric:
Expand Down