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

Rounding to significant figures #11968

Closed
owrior opened this issue Oct 24, 2023 · 5 comments · Fixed by #11959
Closed

Rounding to significant figures #11968

owrior opened this issue Oct 24, 2023 · 5 comments · Fixed by #11959
Labels
accepted Ready for implementation enhancement New feature or an improvement of an existing feature

Comments

@owrior
Copy link
Contributor

owrior commented Oct 24, 2023

Description

Recently I have been working on a project where we needed to round to significant figures and realised there is no way to do this in pandas, numpy or polars. I found this as the best solution.

def signif(x, p):
    x = np.asarray(x)
    x_positive = np.where(np.isfinite(x) & (x != 0), np.abs(x), 10**(p-1))
    mags = 10 ** (p - 1 - np.floor(np.log10(x_positive)))
    return np.round(x * mags) / mags

However it would be nice to have it as part of a package. Would it be possible to include this?

I have implemented this already here #11959 which is about 20% faster than the numpy one here already. However I can imagine there is a better way than using the zip_with maybe?

Thank you!

@owrior owrior added the enhancement New feature or an improvement of an existing feature label Oct 24, 2023
@orlp
Copy link
Collaborator

orlp commented Oct 24, 2023

Perhaps this could be a boolean keyword-only argument for round?

Expr.round(decimals: int = 0, *, significant = False)

If set to True it would round to the specified number of significant digits. Alternatively it could be a dedicated function round_significant. Just bikeshedding.

@owrior
Copy link
Contributor Author

owrior commented Oct 24, 2023

I was thinking that but then thought it might be distinct enough (given the use with integers to) to have in a separate function. Happy to go with what you would prefer though! In the PR I called it round_sf - though maybe that is not clear enough so happy to refactor to round_significant.

@owrior
Copy link
Contributor Author

owrior commented Oct 26, 2023

@orlp should I make the name more expressive or anything?

@orlp
Copy link
Collaborator

orlp commented Oct 27, 2023

@owrior

I had a discussion with @stinodego and I believe we settled on adding this functionality, but as a separate function named round_sig_figs(digits).

@orlp orlp added the accepted Ready for implementation label Oct 27, 2023
@github-project-automation github-project-automation bot moved this to Ready in Backlog Oct 27, 2023
@owrior
Copy link
Contributor Author

owrior commented Oct 31, 2023

@orlp

Thank you very much for this it will be a great help. Can you let me know if my PR is looking good? I have refactored to this function as requested.

@github-project-automation github-project-automation bot moved this from Ready to Done in Backlog Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation enhancement New feature or an improvement of an existing feature
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants