Skip to content

FIX Fixes check_array for pd.NA in a series #25080

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
merged 6 commits into from
Dec 2, 2022

Conversation

thomasjpfan
Copy link
Member

Reference Issues/PRs

Fixes #25078

What does this implement/fix? Explain your changes.

This PR updates check_array to properly raise the correct error and casts pandas Series with pd.NA. Note that check_array already has the correct behavior with DataFrames which is tested here:

def test_check_array_pandas_na_support(pd_dtype, dtype, expected_dtype):

assert_allclose(X_out, [1, 2, np.nan])

X_out = check_array(
X_int64, force_all_finite=False, ensure_2d=False, dtype=np.float32
Copy link
Member

Choose a reason for hiding this comment

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

For my education: what does this test compared to the previous test? I was expecting one of the assert statements after this to check that the dtype of the return value is float32 or some other intricacy related to float32s.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for noticing this! I intended to assert the dtype here too. The PR was updated to include them: 692cfc8 (#25080)

@@ -777,6 +777,13 @@ def check_array(
if all(isinstance(dtype_iter, np.dtype) for dtype_iter in dtypes_orig):
dtype_orig = np.result_type(*dtypes_orig)

elif hasattr(array, "iloc") and hasattr(array, "dtype"):
Copy link
Member

Choose a reason for hiding this comment

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

For my education: why not use isinstance() instead of "guessing" that this is a pd.Series based on the presence of attributes?

Copy link
Member

@jeremiedbb jeremiedbb Dec 1, 2022

Choose a reason for hiding this comment

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

isinstance(..., pd.Series) requires to import pandas which may not be installed since it's not a dependency of scikit-learn

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense, there are a few parts (outside of examples and benchmarks) where the try: import pandas ... except ImportError: pass pattern is used (for example in _param_validation.py). Maybe worth pondering some kind of consolidation, but for a new issue. Or maybe people already pondered it in the past.

Copy link
Member

Choose a reason for hiding this comment

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

For the examples and benchmark, we are happy to depend on pandas (it is indeed documented). The idea is to not depend on pandas in the code itself.

In the parameter validation, we use pd because this is the only way to handle the pd.NA beast. We, therefore, try to import it and if pandas is not installed, we know that pd.NA will not be available in anyway.

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -2,6 +2,21 @@

.. currentmodule:: sklearn

Version 1.2.1
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Version 1.2.1
.. _changes_1_2_1:
Version 1.2.1

Copy link
Member

Choose a reason for hiding this comment

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

What is the reason for releasing the code with a bug in 1.2.0 since this is only a release? We could directly backport for the next release?

Copy link
Member

Choose a reason for hiding this comment

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

Yes let's put that in 1.2

Copy link
Member Author

Choose a reason for hiding this comment

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

The changelog entry has been moved to 1.2.

@thomasjpfan thomasjpfan added the To backport PR merged in master that need a backport to a release branch defined based on the milestone. label Dec 1, 2022
@thomasjpfan thomasjpfan added this to the 1.2 milestone Dec 1, 2022
@jeremiedbb jeremiedbb merged commit 54d91d3 into scikit-learn:main Dec 2, 2022
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Dec 6, 2022
@petr-larin
Copy link

Hi folks, I'm not sure if this is the same bug or different but I'm still getting this both in Colab and in Conda:

File ~\anaconda3\lib\site-packages\sklearn\utils\validation.py:110, in _assert_all_finite(X, allow_nan, msg_dtype, estimator_name, input_name)
108 # for object dtype data, we only check for NaNs (GH-13254)
109 if X.dtype == np.dtype("object") and not allow_nan:
--> 110 if _object_dtype_isnan(X).any():
111 raise ValueError("Input contains NaN")
113 # We need only consider float arrays, hence can early return for all else.

AttributeError: 'bool' object has no attribute 'any'

@petr-larin
Copy link

I have just reinstalled sklearn in Conda to no avail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:utils To backport PR merged in master that need a backport to a release branch defined based on the milestone.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

check_array does not gracefully fail with pd.NA
5 participants