pola-rs / polars

Dataframes powered by a multithreaded, vectorized query engine, written in Rust
https://docs.pola.rs
Other
30.57k stars 1.98k forks source link

feat(python): Add support for python builtin `round` #19941

Closed kl31tz closed 4 days ago

kl31tz commented 4 days ago

Hello polars maintainers,

This is a PR to add support for builtin round by implementing __round__ in Expr and Series; It is already possible to call abs(e) and e.abs() on series and expressions; this change enables calling round(e) or round(e, 3) in addition to e.round() or e.round(3)

Let me know if you'd like to support this feature and whether I can contribute further by implementing it in other areas

resolves https://github.com/pola-rs/polars/issues/19942

Thank you !

codecov[bot] commented 4 days ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 79.45%. Comparing base (8facee9) to head (a9fb5df). Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #19941 +/- ## ======================================= Coverage 79.45% 79.45% ======================================= Files 1555 1555 Lines 216168 216172 +4 Branches 2456 2456 ======================================= + Hits 171760 171764 +4 Misses 43850 43850 Partials 558 558 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.


🚨 Try these New Features:

eitsupi commented 4 days ago

Maybe related to #17798

kl31tz commented 4 days ago

right ! I did not notice np.ndarray did not define __round__ either Strange considering its round() method/functions all do banker rounding, and therefore are all consistent consistent w/ python

>>> import numpy as np
>>> np.round(np.arange(6) + .5)
array([0., 2., 2., 4., 4., 6.])
>>> [round(i + .5) for i in range(6)]
[0, 2, 2, 4, 4, 6]
>>> round(np.arange(6) + .5)
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    round(np.arange(6) + .5)
    ~~~~~^^^^^^^^^^^^^^^^^^^
TypeError: type numpy.ndarray doesn't define __round__ method

For polars it is trickier if Series.round and python round differ

>>> import polars as pl
>>> pl.int_range(6, eager=True).round()
shape: (6,)
Series: 'literal' [i64]
[
        0
        1
        2
        3
        4
        5
]
ritchie46 commented 4 days ago

I don't think we should do this. I don't think we should implement all python dunders. I don't see much benefit as it adds different ways of doing stuff. Another reasons is that our rounding behavior is not equal to python's.

kl31tz commented 4 days ago

understood, I'll close the issue thank you for taking a look !