Skip to content

Refactor _convert_container - #28681

Open
Charlie-XIAO wants to merge 46 commits into
scikit-learn:mainfrom
Charlie-XIAO:rfc-convert-container
Open

Charlie-XIAO wants to merge 46 commits into
scikit-learn:mainfrom
Charlie-XIAO:rfc-convert-container

Conversation

@Charlie-XIAO

@Charlie-XIAO Charlie-XIAO commented Mar 23, 2024 •

Copy link
Copy Markdown
Contributor

Follow up of #28521, see #28521 (comment).

This PR splits constructor_name into constructor_type and constructor_lib, and adds two parameters sparse_container and sparse_format to reflect dense versus sparse. /cc @glemaitre

Mapping from the original constructor_name to the specifications in this PR

constructor_type constructor_lib sparse_container sparse_format
Default "pandas" None "csr"
"list" "list"
"tuple" "tuple"
"slice" "slice"
"array" "array"
"sparse"/"sparse_csr" "array" "matrix"
"sparse_csc" "array" "matrix" "csc"
"sparse_csr_array" "array" "array"
"sparse_csc_array" "array" "array" "csc"
"dataframe"/"pandas" "dataframe"
"polars" "dataframe" "polars"
"pyarrow" "dataframe" "pyarrow"
"series" "series"
"polars_series" "series" "polars"
"pyarrow_array" "series" "pyarrow"
"index" "index"

Some rules of when each parameter is applicable

  • sparse_container has no effect unless constructor_type="array"
  • sparse_format has no effect unless constructor_type="array" and sparse_container is not None
  • constructor_lib has no effect unless constructor_type is one of "dataframe", "series", and "index"
  • minversion has no effect unless constructor_lib is used
  • column_names (renamed from columns_name) has no effect unless constructor_type="dataframe"
  • categorical_feature_names has no effect unless constructor_type="dataframe"

Constraints on the shape of the input container

  • constructor_type="slice" requires 1-dimensional container with exactly 2 elements
  • constructor_type="dataframe" requires 2-dimensional container (previously it converts (n,) shape to (n, 1) shape)
  • constructor_type="series" requires 1-dimensional container (previously it creates series with each element being a list when given 2-dimensional container)
  • constructor_type="index" requires 1-dimensional container (similar to "series")

@github-actions

github-actions Bot commented Mar 23, 2024 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: a8d0e10. Link to the linter CI: here

@Charlie-XIAO
Charlie-XIAO marked this pull request as ready for review March 24, 2024 04:03

@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.

Thanks for the PR @Charlie-XIAO. Here are some comments. I'm +1 for the refactoring of _convert_container.

Comment thread sklearn/utils/_testing.py Resolved
Comment thread sklearn/utils/_testing.py Resolved
Comment thread sklearn/utils/_testing.py Resolved
Comment thread sklearn/utils/_testing.py Resolved
Comment thread sklearn/utils/tests/test_indexing.py Resolved
@glemaitre

Copy link
Copy Markdown
Member

Once the CIs are green, I'll review ;) (I am buying a bit of time).
But indeed, I really like the refactoring. Thanks for taking care of it @Charlie-XIAO.

@glemaitre
glemaitre self-requested a review March 28, 2024 06:51
@Charlie-XIAO

Copy link
Copy Markdown
Contributor Author

Now I have resolved all suggestions from Jérémie, and CI is green :)

@glemaitre

Copy link
Copy Markdown
Member

I'm going to resolve the conflicts and make a review.

@glemaitre glemaitre 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.

A couple of comments before to look at the tests.

Comment thread sklearn/utils/_testing.py Resolved
Comment thread sklearn/utils/_testing.py Resolved
Comment thread sklearn/utils/_testing.py Resolved
@glemaitre
glemaitre self-requested a review May 21, 2024 16:06
@Charlie-XIAO Charlie-XIAO changed the title RFC _convert_container Refactor _convert_container Jan 2, 2025

@adrinjalali adrinjalali 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.

Nice work, thanks @Charlie-XIAO .

Comment thread sklearn/utils/validation.py Outdated
return isinstance(X, pl.DataFrame)


def _is_arrow_df(X):

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.

do we have a CI with arrow installed? (I think we should)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes I think we do have it in pylatest_conda_forge_mkl_linux-64:

@adrinjalali

Copy link
Copy Markdown
Member

We somehow forgot about this one, would you mind updating the PR @Charlie-XIAO ?

@Charlie-XIAO

Copy link
Copy Markdown
Contributor Author

Will do tomorrow

@Charlie-XIAO
Charlie-XIAO marked this pull request as draft June 20, 2025 13:53
@Charlie-XIAO

Copy link
Copy Markdown
Contributor Author

This has changed too much since I last updated. I need to take some time to check the new pyarrow-related changes to make sure I did not mistakenly drop any test, thus marking as draft now.

@jeremiedbb jeremiedbb modified the milestones: 1.7, 1.8 Jul 1, 2025
@Charlie-XIAO

Copy link
Copy Markdown
Contributor Author

In 11a6186 I added "pyarrow_array" under the "array" type but then realized it is more like a "series" because it can only be one-dimensional, and its primary purpose is to represent a column in a table. I'm now reverting that commit and working towards adding it as a "series" type.

@Charlie-XIAO
Charlie-XIAO marked this pull request as ready for review July 13, 2025 09:02
@Charlie-XIAO

Copy link
Copy Markdown
Contributor Author

@adrinjalali I've added "pyarrow_array" as {"constructor_type": "series", "constructor_lib": "pyarrow" and added (at least) the additional tests in #31040. This PR is ready for review now.

@adrinjalali adrinjalali 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.

This LGTM, and we should probably merge quick not to have more conflicts.

Comment thread sklearn/utils/_testing.py
return sparse_constructor(container, dtype=dtype)


def _convert_container_to_dataframe(

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.

this could be probably trivialised by using narwhals 😅 (@MarcoGorelli )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this just used in tests (so, not user-facing)? if so, it's probably fine to use it?

@adrinjalali

Copy link
Copy Markdown
Member

@jeremiedbb maybe you could have another look?

@lesteve lesteve modified the milestones: 1.8, 1.9 Nov 12, 2025
@jeremiedbb jeremiedbb modified the milestones: 1.9, 1.10 May 10, 2026
@cakedev0
cakedev0 self-requested a review October 28, 2026 17:10
@cakedev0 cakedev0 modified the milestones: 1.10, 1.11 Oct 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

7 participants