Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing Series to Series.__getitem__ #1533

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

devdanzin
Copy link
Contributor

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

If you have comments or can explain your changes, please do so below

This PR allows passing Series to Series.__getitem__ and adds a test. I'm not sure about how/where to document the changes, any suggestions?

@github-actions github-actions bot added the enhancement New feature or request label Dec 7, 2024
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

thanks for your PR!

just left a couple of comments

) -> Any | Self:
if isinstance(item, (int, slice, Sequence)):
return self._from_native_object(self._native_series.__getitem__(item))
return self._from_native_object(self._native_series.__getitem__(item.to_numpy()))
Copy link
Member

@MarcoGorelli MarcoGorelli Dec 7, 2024

Choose a reason for hiding this comment

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

πŸ€” not sure we should be converting to numpy here

I'm not totally sure what we should be doing instead though, sorry, will need to investigate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I had doubts about this too and picked a solution that seemed to work without actually understanding the consequences.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems using item.to_native() instead works, and also solves an issue with missing values becoming NaN with to_numpy(). Do you think it's acceptable? Any other corner cases to cover with tests?

Copy link
Member

Choose a reason for hiding this comment

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

thanks - i'll check, to be careful we get the details right here

Comment on lines +10 to +11
spl = pl.Series([1, 2, 3])
assert spl[spl[0, 1]].equals(pl.Series([2, 3]))
Copy link
Member

Choose a reason for hiding this comment

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

can we check what happens when there's missing values too please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using to_native() instead of to_numpy() allows the following tests to pass, but my complete lack of knowledge on numpy, narwhals, polars and related subjects makes me unsure they are correct and show expected behavior:

    spl = pl.Series([1, None, 3])
    assert spl[spl[0, 1]].equals(pl.Series([None, None]))

    snw = nw.from_native(spl, series_only=True)
    assert snw[snw[0, 1]].to_native().equals(pl.Series([None, None]))

What's your take on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

enh: allow passing Series to Series.__getitem__
2 participants