Skip to content

MNT simplify check_array for sparse pandas.DataFrame a bit - #33971

Merged
jeremiedbb merged 3 commits into
scikit-learn:mainfrom
lorentzenchr:mnt_iloc_check_array
May 13, 2026
Merged

jeremiedbb merged 3 commits into
scikit-learn:mainfrom
lorentzenchr:mnt_iloc_check_array

Conversation

@lorentzenchr

@lorentzenchr lorentzenchr commented May 10, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Contributes to #33958.

What does this implement/fix? Explain your changes.

Uses narwhals instead of the iloc attribute in check_array and simplify the sparse pandas dataframe logic a bit.

AI usage disclosure

None

Any other comments?

TODO:

@lorentzenchr lorentzenchr added No Changelog Needed Quick Review For PRs that are quick to review labels May 10, 2026
@jeremiedbb

Copy link
Copy Markdown
Member

Interesting, the missing coverage is the line that is being fixed in #33996 :)

@jeremiedbb

Copy link
Copy Markdown
Member

Fix code leftover from #30613, i.e. ValueError can't happen with pandas >= 1.1.

do you intend to do that in this PR ?

@lorentzenchr

lorentzenchr commented May 12, 2026 •

Copy link
Copy Markdown
Member Author

do you intend to do that in this PR ?

Not until Monday.

@lorentzenchr

Copy link
Copy Markdown
Member Author

do you intend to do that in this PR ?

Not until Monday.

Ha, I managed to do the todo. If CI is not green, I can only fix it next week.

Comment on lines -980 to -989
if array.dtype == np.dtype("object"):
unique_dtypes = set([dt.subtype.name for dt in array_orig.dtypes])
if len(unique_dtypes) > 1:
raise ValueError(
"Pandas DataFrame with mixed sparse extension arrays "
"generated a sparse matrix with object dtype which "
"can not be converted to a scipy sparse matrix."
"Sparse extension arrays should all have the same "
"numeric type."
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I haven't been able to trigger this error with not too old pandas. It always fail earlier in to_coo, so the simplification looks good.

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment on lines +886 to +898
# For dataframes, use narwhals
if _nw_into_df_or_series(array):
array_df = nw.from_native(array, allow_series=True)
else:
array_df = None

# pandas.DataFrame may have sparse columns
is_pandas_fully_sparse_df = False
if (
array_df is not None
and array_df.implementation.is_pandas()
and len(array_df.shape) >= 2
):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

alternatively, we could indent once more:

if _nw_into_df_or_series(array):
    array_df = nw.from_native(array, allow_series=True)
    is_pandas_fully_sparse_df = False

        if array_df.implementation.is_pandas() and len(array_df.shape) >= 2:
             ...

but doesn't matter too much

@jeremiedbb
jeremiedbb enabled auto-merge (squash) May 13, 2026 15:45
@jeremiedbb
jeremiedbb merged commit f7db22e into scikit-learn:main May 13, 2026
33 checks passed
@lorentzenchr
lorentzenchr deleted the mnt_iloc_check_array branch May 18, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants