Repository navigation
MNT simplify check_array for sparse pandas.DataFrame a bit - #33971
Merged
Merged
Conversation
Member
|
Interesting, the missing coverage is the line that is being fixed in #33996 :) |
Member
do you intend to do that in this PR ? |
Member
Author
Not until Monday. |
Member
Author
Ha, I managed to do the todo. If CI is not green, I can only fix it next week. |
jeremiedbb
reviewed
May 13, 2026
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." | ||
| ) |
Member
There was a problem hiding this comment.
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
reviewed
May 13, 2026
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 | ||
| ): |
Member
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
Contributes to #33958.
What does this implement/fix? Explain your changes.
Uses narwhals instead of the
ilocattribute incheck_arrayand simplify the sparse pandas dataframe logic a bit.AI usage disclosure
None
Any other comments?
TODO: