FIX check_array with narwhals.DataFrame input - #34051
Conversation
| if _nw_into_df_or_series(array): | ||
| array_df = nw.from_native(array, allow_series=True) | ||
| else: | ||
| array_df = None |
There was a problem hiding this comment.
maybe we shoudl already define df_pandas here, instead of asking and array_df.implementation.is_pandas() afterwards ?
There was a problem hiding this comment.
I like relying on nw for which backend is used.
There was a problem hiding this comment.
yeah but the flow is weird here. We only use array_df if it comes from pandas.
if _nw_into_df_or_series(array):
array_df = nw.from_native(array, allow_series=True)
else:
array_df = None
if array_df is not None and array_df.implentation.is_pandas():
df_pandas = array_df.to_native()
# and then only use df_pandas in this branchI think the following would be more readable:
if _nw_into_df_or_series(array):
array_df = nw.from_native(array, allow_series=True)
array_df = array_df.to_native() if array_df.implentation.is_pandas() else None
else:
array_df = None
if array_df is not None:
# do everything with array_dfThere was a problem hiding this comment.
This part is not touched by this PR. Also, I anticipate that array_df will be used more within this function in the future.
FrancoisPgm
left a comment
There was a problem hiding this comment.
Thank you @lorentzenchr , I have just a couple of questions and nitpicks but otherwise from what I can tell this looks good.
| assert not np.isnan(M).any() | ||
|
|
||
|
|
||
| @pytest.mark.filterwarnings("ignore::scipy.sparse.SparseEfficiencyWarning") |
There was a problem hiding this comment.
Does the warning result from the changes in check_array ?
There was a problem hiding this comment.
No. a8d8983 is not related to the bugfix, but removes all warnings in the tests such that I could run pytest -x -We sklearn/utils/tests/tests_validation.py successfully.
There was a problem hiding this comment.
Haha if that's your goal, you have a loooong path in front of you 😄 (#29516)
There was a problem hiding this comment.
I‘ve already been quite successful in linear models (the ones I touched).
| X = retype(np.arange(4).reshape(2, 2).astype(float)) | ||
| X = np.array(np.arange(4).reshape(2, 2).astype(float)) | ||
| X[0, 0] = value | ||
| X = retype(X) |
There was a problem hiding this comment.
I believe this change is here to avoid the SparseEfficiencyWarning, is that correct ?
| convert_to_nw = False | ||
| if constructor_name == "narwhals": | ||
| convert_to_nw = True | ||
| # Search for backend. | ||
| for lib_name in ["pandas", "polars"]: | ||
| try: | ||
| sys.modules[lib_name] | ||
| constructor_name = lib_name | ||
| break | ||
| except KeyError: | ||
| continue | ||
| else: | ||
| pytest.skip("no dataframe backend for narwhals is installed") | ||
|
|
There was a problem hiding this comment.
feels like maybe constructor_name is not the best place for it. To me we should be able to use _convert_container to create a narwhals dataframe backed by a pandas or polars dataframe without having to create a dataframe first. Something like
X = np.array([[1, 2], [3, 4]])
df = _convert_container(X, "pandas", convert_to_narwhals=True)
There was a problem hiding this comment.
I see it different. Either nw.df is an option in constructor name, or we don’t deal with nw in _convert_container.
We could also simplify and require in the tests that, e.g., pandas is available to construct nw.df (and rely on ne testing that the backend does not matter).
There was a problem hiding this comment.
Still it's weird that you can't chose the backend and that the backend that one gets is based on an hardcoded ordering. For instance, in the test that check sparse you added:
sdf = pd.DataFrame.sparse.from_spmatrix(sp_mat)
if convert_to_narwhals:
sdf = _convert_container(sdf, constructor_name="narwhals")If the order was different, it would not do what's expected. That's fragile.
(in particular there's no way, with this ordering, to get a narwhals dataframe with a polars backend if pandas is installed)
Maybe here it would just be more appropriate and robust to do
sdf = pd.DataFrame.sparse.from_spmatrix(sp_mat)
if convert_to_narwhals:
sdf = nw.from_native(sdf)
jeremiedbb
left a comment
There was a problem hiding this comment.
I removed the changes to _convert_container since it was not really used anymore. Later I think we'll prefer to think narwhals inclusion jointly with #28681.
LGTM
|
While well intended, 916bbd7 effectively removes the test that this PR fixes!!! |
|
I'm not so sure. Was check_array already expected to work on narwhals dataframes ? (also, not all tests: the sparse test is still there) |
Co-authored-by: Jérémie du Boisberranger <[email protected]>
Co-authored-by: Jérémie du Boisberranger <[email protected]>
Reference Issues/PRs
Fixes a bug in
check_arrayintroduced in #33971, popped up in #33959.What does this implement/fix? Explain your changes.
Input in
check_arraymay already be anarwhals.DataFrame. This errors on main and is fixed here with a test for it.AI usage disclosure
None
Any other comments?