Skip to content

Conversation

@MarcoGorelli
Copy link
Member

@MarcoGorelli MarcoGorelli commented Oct 28, 2024

pretty consistent difference here:

main:

In [2]: %timeit nw.to_py_scalar('foo')
468 ns ± 67.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [3]: %timeit nw.to_py_scalar('foo')
338 ns ± 37.6 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

here:

In [1]: import narwhals as nw

In [2]: %timeit nw.to_py_scalar('foo')
154 ns ± 23.6 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [3]: %timeit nw.to_py_scalar('foo')
131 ns ± 6.37 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

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

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

Related issues

  • Related issue #
  • Closes #

Checklist

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

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

@MarcoGorelli
Copy link
Member Author

@EdAbati fancy taking a look?

@MarcoGorelli MarcoGorelli marked this pull request as ready for review October 28, 2024 18:51
Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

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

Commenting from mobile, just have a tiny question :)

Copy link
Member

@FBruzzesi FBruzzesi 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 the quick adjustment @MarcoGorelli 🙌🏼🚀

DogOmwGIF

Copy link
Collaborator

@EdAbati EdAbati left a comment

Choose a reason for hiding this comment

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

Thanks @MarcoGorelli, I like to see/learn about ways to speed up code

so checking number.Number early on would cause numpy scalars to not be converted to Python scalars

should we also add (np.int64(1), 1) in the tests to capture this? (at the moment that is indirectly tested in the doctests)

and the rest looks great!

Comment on lines +90 to +91
if item in {"to_py_scalar"}:
# We don't overwrite the docstring for these
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just so I know what to do/review in the future, which kind of methods should be added here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think all of them to be honest, it's probably simpler to just use import narwhals as nw in all docstrings and forget about this test 😅 i'll make a separate pr


T = TypeVar("T")

NON_TEMPORAL_SCALAR_TYPES = (
Copy link
Member

Choose a reason for hiding this comment

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

Kind of related, I am running a test, and getting a raise if None enters this function. Should we add it to non temporal scalar types?

Copy link
Member Author

Choose a reason for hiding this comment

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

very good point, thanks!

@MarcoGorelli
Copy link
Member Author

really useful comments here, thanks both for excellent reviews 🙏

@MarcoGorelli MarcoGorelli merged commit 7d07dc1 into narwhals-dev:main Oct 29, 2024
@dangotbanned dangotbanned mentioned this pull request May 6, 2025
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants