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

Adding expm1 as expression #12891

Closed
trendelkampschroer opened this issue Dec 4, 2023 · 4 comments
Closed

Adding expm1 as expression #12891

trendelkampschroer opened this issue Dec 4, 2023 · 4 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@trendelkampschroer
Copy link

trendelkampschroer commented Dec 4, 2023

Description

It would be nice to be able to have expm1, cf. https://en.wikipedia.org/wiki/Exponential_function#Computation, available as a polars expression. The inverse log1p is already available as an expression.

I am currently using

 t = (values / 2).tanh()
pl.when(values.abs() < 0.1).then(2.0 * t / (1.0 - t)).otherwise(values.exp() - 1.0)

with an arbitrarily defined threshold of 0.1 to get a 'stable' implementation of expm1 but it's probably sub-optimal, because of the conditional, and a user needs to define a meaningful threshold requiring knowledge of the "region of stability" of the naive exp(x) - 1 implementation.

Would be happy to try implementing this myself, but I have little/zero knowledge of Rust and experience with the complexity/scope of adding this feature to polars - but I am happy to receive guidance and eventually try it out.

Btw, thanks a lot for creating, supporting, and making polars freely available.

@trendelkampschroer trendelkampschroer added the enhancement New feature or an improvement of an existing feature label Dec 4, 2023
@deanm0000
Copy link
Collaborator

deanm0000 commented Dec 6, 2023

The numpy function https://numpy.org/doc/stable/reference/generated/numpy.expm1.html#numpy.expm1

Is a ufunc so you can use it directly such as df.with_columns(np.expm1(pl.col('values')))

You can monkey patch a wrapper to Expr namespace so you can use it like other polars methods

pl.Expr.expm1 = lambda col : (
np.expm1(col)
)

Then you could just do

df.with_columns(pl.col('values').expm1())

@trendelkampschroer
Copy link
Author

trendelkampschroer commented Dec 7, 2023

Awesome, thanks a lot @deanm0000.

@trendelkampschroer
Copy link
Author

I am re-opening this because of #13557, i.e. in a group_by context a polars.Expression cannot simply be replaced by a numpy 'ufunc'.

@deanm0000
Copy link
Collaborator

#14135

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

No branches or pull requests

2 participants