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: Add round_sig_figs expression for rounding to significant figures #11959

Merged
merged 18 commits into from
Nov 9, 2023

Conversation

owrior
Copy link
Contributor

@owrior owrior commented Oct 23, 2023

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? (The implementation here is about 20% faster than the numpy one here already but I imagine there is a better way of implementing it) (Update now about 45% faster).

Thank you!

Closes #11968

@owrior owrior changed the title Significant figures feat(rust, python): Significant figures Oct 23, 2023
@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Oct 23, 2023
@stinodego
Copy link
Member

Hi @owrior, thanks for the PR. Could you please make an issue so we can discuss whether we want to include this, and what the right API for it would be? Thanks!

From a quick initial look, the boilerplate here looks quite good 👌

@stinodego stinodego changed the title feat(rust, python): Significant figures feat: Significant figures Oct 23, 2023
@owrior
Copy link
Contributor Author

owrior commented Oct 28, 2023

Refactored to round_sig_figs @orlp so think this might be ready!

crates/polars-ops/src/series/ops/round.rs Outdated Show resolved Hide resolved
if value == 0.0 {
return value;
}
let magnitiude = 10.0.pow(digits as f64 - 1.0 - ((value.abs()).log10().floor()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you use f64::powi? Also it's spelled magnitude.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Think this is done.

py-polars/polars/expr/expr.py Outdated Show resolved Hide resolved
py-polars/scripts/tes.py Outdated Show resolved Hide resolved
crates/polars-ops/src/series/ops/round.rs Outdated Show resolved Hide resolved
@orlp orlp enabled auto-merge (squash) November 9, 2023 10:47
@orlp orlp disabled auto-merge November 9, 2023 10:51
@stinodego stinodego changed the title feat: Significant figures feat: Add round_sig_figs for rounding to significant figures Nov 9, 2023
@stinodego stinodego changed the title feat: Add round_sig_figs for rounding to significant figures feat: Add round_sig_figs expression for rounding to significant figures Nov 9, 2023
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

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

I will do a small update to the docstrings, will update and merge after.

@stinodego stinodego merged commit a14e727 into pola-rs:main Nov 9, 2023
24 checks passed
@owrior owrior deleted the significant-figures branch November 16, 2023 00:04
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 python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rounding to significant figures
3 participants