Skip to content

feat: Add support for indices and values Series in Series.scatter#3444

Merged
dangotbanned merged 16 commits intomainfrom
feat/scatter-support-series
Feb 20, 2026
Merged

feat: Add support for indices and values Series in Series.scatter#3444
dangotbanned merged 16 commits intomainfrom
feat/scatter-support-series

Conversation

@FBruzzesi
Copy link
Member

@FBruzzesi FBruzzesi commented Feb 2, 2026

Description

@MarcoGorelli this might pop up in sklearn/skrub 😂

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🔧 Optimization
  • 📝 Documentation
  • ✅ Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

@FBruzzesi FBruzzesi added the enhancement New feature or request label Feb 2, 2026
# - Missing return type
pc_replace_with_mask: Incomplete = pc.replace_with_mask
return self._with_native(pc_replace_with_mask(self.native, mask, values_native))
def scatter(self, indices: Self, values: Self) -> Self:
Copy link
Member

@dangotbanned dangotbanned Feb 2, 2026

Choose a reason for hiding this comment

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

This is very satisfying for (#3305) to finally pay off 🙏

Thought it might be worth splitting into a separate PR, since it would be easy to upstream to main and (possibly) avoid a numpy import

@dangotbanned
Copy link
Member

dangotbanned commented Feb 2, 2026

@FBruzzesi would you be open to me porting over the test suite as well?

I'd need to make some tweaks, but I think I could get the diff negative 😄

(no worries if not)

@FBruzzesi
Copy link
Member Author

FBruzzesi commented Feb 2, 2026

Sure @dangotbanned - I will first try to get the CI green again 🥦 🥦


Fixing CI is really the broccoli's to all the currently baking cakes 🍰

Comment on lines +19 to +21
@pytest.mark.filterwarnings(
"ignore:.*all arguments of to_dict except for the argument:FutureWarning"
)
Copy link
Member

Choose a reason for hiding this comment

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

modin triggers this warning because it passes orient=list as positional.

So I copied over the filter from test_to_dict:

@pytest.mark.filterwarnings(
"ignore:.*all arguments of to_dict except for the argument:FutureWarning"
)
def test_to_dict(constructor_eager: ConstructorEager) -> None:

But I think we should either:

  1. Handle this in narwhals
  2. Add it to tool.pytest.ini_options.filterwarnings

assert_equal_data(df, unchanged_indexed)


def test_scatter_pandas_index() -> None:
Copy link
Member

Choose a reason for hiding this comment

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

I renamed this to make it clearer, since indices is a parameter name for scatter.

Although, I'd prefer if we could use a less direct way of testing index preservation.
E.g. if this is important, why only pandas and not all the derivatives?

Comment on lines +457 to +458
compliant_indices = self._extract_native(indices)
compliant_values = self._extract_native(values)
Copy link
Member Author

Choose a reason for hiding this comment

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

PR unrelated: Should we rename _extract_native to _extract_compliant?

Copy link
Member

Choose a reason for hiding this comment

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

Lol, yeah I don't know when that slipped through 😂

Copy link
Member

Choose a reason for hiding this comment

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

@FBruzzesi I thought I remembered discussing this!

#2886 (comment)

Weirdly, the same thing has the wrong name on nw.Series 🤔

narwhals/narwhals/series.py

Lines 376 to 381 in 0e80656

def _extract_native(self, arg: Any) -> Any:
from narwhals.series import Series
if isinstance(arg, Series):
return arg._compliant_series
return arg

Copy link
Member Author

Choose a reason for hiding this comment

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

@dangotbanned which algorithm do you use to index all past comments and conversations in your brain? I am jealous 😂

Copy link
Member

Choose a reason for hiding this comment

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

@dangotbanned which algorithm do you use to index all past comments and conversations in your brain? I am jealous 😂

Deju vu + obsessive thoughts 😭

Copy link
Member

Choose a reason for hiding this comment

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

PR unrelated: Should we rename _extract_native to _extract_compliant?

@MarcoGorelli FYI, I will forget 😂

@FBruzzesi
Copy link
Member Author

FBruzzesi commented Feb 5, 2026

@dangotbanned let me know if you have time and manage to replicate the environment for which the tests are failing.

I am on an Apple Silicon (macOS ARM64), and the old versions of pandas (1.1.3) and numpy (1.19.3) don't have pre-built wheels for this architecture. When pip/uv tries to build them from source, it fails 😭


Update: solved in 6b23fdc

Might be nice to factor out scatter and _scatter_in_place for pandas before merging.

Update 2: b714afb

@dangotbanned dangotbanned self-assigned this Feb 7, 2026
@dangotbanned dangotbanned removed their assignment Feb 7, 2026
Copy link
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

Thanks @FBruzzesi!

I have a few suggestions, but overall just very happy to slowly creep #2572 into main 😉

praise be narwhal

@FBruzzesi
Copy link
Member Author

@dangotbanned if you are happy (or I would say happier since the last approval in #3444 (review)) with this, I will go ahead and merge

Copy link
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

Double approve

@dangotbanned dangotbanned merged commit 349ce10 into main Feb 20, 2026
36 checks passed
@dangotbanned dangotbanned deleted the feat/scatter-support-series branch February 20, 2026 10:43
@MarcoGorelli
Copy link
Member

thanks all, solid!

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.

enh: scatter should accept narwhals.Series for indices

3 participants