feat(base): migrate MiniBatch* class signatures to narwhals-agnostic types - #1989
Open
tiyaagarwal wants to merge 4 commits into
Open
feat(base): migrate MiniBatch* class signatures to narwhals-agnostic types#1989tiyaagarwal wants to merge 4 commits into
tiyaagarwal wants to merge 4 commits into
Conversation
Update MiniBatchClassifier, MiniBatchRegressor, MiniBatchTransformer, and MiniBatchSupervisedTransformer to accept any narwhals-supported eager backend (pandas, polars, PyArrow, etc.) in place of the previously hard-coded pd.DataFrame / pd.Series type hints. - Replace `pd.DataFrame` / `pd.Series` parameter and return annotations with `IntoDataFrame` / `IntoSeries` from narwhals.stable.v2.typing - Rewrite MiniBatchClassifier.predict_many default body with narwhals so it no longer calls pandas-only .empty / .idxmax(axis="columns") - Update all docstrings to note backend-agnostic support Part of online-ml#1919. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Merging this PR will not alter performance
Comparing Footnotes
|
…migration - Remove `from river.utils.dataframe import to_native_series` from the `MiniBatchClassifier.predict_many` body: `river.base -> river.utils` is not an allowed dependency edge (breaks the lint-imports DAG contract). Replace with inline narwhals calls (nw.get_native_namespace / nw.new_series). - Use native-frame columns instead of narwhals `.columns` for label lookup so non-string class labels (e.g. integer class IDs in GaussianNB) are preserved in the output Series without dtype coercion to object. - Guard the empty-frame path against both 0 rows and 0 columns: an untrained classifier returns a DataFrame with N rows but 0 class columns, which caused `arr.argmax(axis=1)` to raise ValueError on an empty sequence. - Remove typing.cast() calls flagged by mypy as redundant. - Guard `compose/union.py` MiniBatchSupervisedTransformer.learn_many call with `y is not None` so mypy does not flag the NativeSeries | None → NativeSeries argument mismatch. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- When predict_proba_many returns an empty frame (no classes seen yet, e.g. untrained model), return proba_native directly instead of building a new empty series. This preserves the caller's backend, index, and the expected DataFrame shape that test_learn_many_not_fit asserts. - Add typing.cast() around the .to_native() call so mypy does not flag [no-any-return]; .to_native() is typed as Any in narwhals stubs. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
Addresses the base-class items from #1919:
base/classifier.py—MiniBatchClassifierbase/regressor.py—MiniBatchRegressorbase/transformer.py—MiniBatchTransformerandMiniBatchSupervisedTransformerChanges
pd.DataFrame/pd.Seriesparameter and return type hints withIntoDataFrame/IntoSeriesfromnarwhals.stable.v2.typingacross all four mini-batch base classes.import pandas as pdguard insideTYPE_CHECKINGblocks; replace with the narwhals typing imports.MiniBatchClassifier.predict_manydefault body so it no longer relies on pandas-only.empty/.idxmax(axis="columns"): it now wraps the output ofpredict_proba_manywith narwhals, extracts a numpy argmax, and rebuilds the result throughto_native_series— matching the backend (and pandas index) of the inputX.Approach
Follows the same narwhals pattern introduced in #1900 and used across
linear_model,preprocessing,anomaly, etc.Testing
All checks passed.
Closes part of #1919 (base class items).
🤖 Generated with Claude Code