Skip to content

Commit

Permalink
preserve old behavior by default
Browse files Browse the repository at this point in the history
  • Loading branch information
magarick committed Aug 4, 2023
1 parent af40ee2 commit 95512aa
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
21 changes: 11 additions & 10 deletions crates/polars-ops/src/series/ops/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,26 @@ fn find_label_precision(sorted_breaks: &[f64], precision: usize, scientific: boo
fn make_labels(
sorted_breaks: &[f64],
left_closed: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> Vec<String> {
let precision = find_label_precision(sorted_breaks, precision, scientific);
let precision = precision.map(|p| find_label_precision(sorted_breaks, p, scientific));

// This seems to be the most straightforward way to get rid of trailing zeros.
let (re, replacement) = match scientific {
true => (Regex::new(r"(\.?0+)e").unwrap(), "e"),
false => (Regex::new(r"(\.?0+)$").unwrap(), ""),
};
// Rust doesn't like returning these closures as part of the tuple above.
let format_fn = match scientific {
true => |v, p| format!("{0:.1$e}", v, p),
false => |v, p| format!("{0:.1$}", v, p),
};

let mut out = Vec::with_capacity(sorted_breaks.len() + 2);
let mut last_break_str = format!("{}", f64::NEG_INFINITY);
for v in sorted_breaks.iter().chain(once(&f64::INFINITY)) {
let raw_break_str = format_fn(v, precision);
let raw_break_str = match (precision, scientific) {
(Some(p), true) => format!("{0:.1$e}", v, p),
(Some(p), false) => format!("{0:.1$}", v, p),
(None, true) => format!("{0:e}", v),
(None, false) => format!("{0}", v),
};
let break_str = re.replace(&raw_break_str, replacement).to_string();
if left_closed {
out.push(format!("[{}, {})", last_break_str, break_str));
Expand All @@ -128,7 +129,7 @@ pub fn cut(
labels: Option<Vec<String>>,
left_closed: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> PolarsResult<Series> {
polars_ensure!(breaks.iter().all(|x| x.is_finite()), ComputeError: "Don't include NaN, Inf, or -Inf in breaks");
Expand Down Expand Up @@ -157,7 +158,7 @@ pub fn qcut(
left_closed: bool,
allow_duplicates: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> PolarsResult<Series> {
let s = s.cast(&DataType::Float64)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-plan/src/dsl/function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub enum FunctionExpr {
labels: Option<Vec<String>>,
left_closed: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
},
#[cfg(feature = "cutqcut")]
Expand All @@ -221,7 +221,7 @@ pub enum FunctionExpr {
left_closed: bool,
allow_duplicates: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
},
#[cfg(feature = "rle")]
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-plan/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ impl Expr {
labels: Option<Vec<String>>,
left_closed: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> Expr {
self.apply_private(FunctionExpr::Cut {
Expand All @@ -1483,7 +1483,7 @@ impl Expr {
left_closed: bool,
allow_duplicates: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> Expr {
self.apply_private(FunctionExpr::QCut {
Expand All @@ -1506,7 +1506,7 @@ impl Expr {
left_closed: bool,
allow_duplicates: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> Expr {
let probs = (1..n_bins).map(|b| b as f64 / n_bins as f64).collect();
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3304,7 +3304,7 @@ def cut(
labels: list[str] | None = None,
left_closed: bool = False,
include_breaks: bool = False,
precision: int = 3,
precision: int | None = None,
scientific: bool = False,
) -> Self:
"""
Expand Down Expand Up @@ -3384,7 +3384,7 @@ def qcut(
left_closed: bool = False,
allow_duplicates: bool = False,
include_breaks: bool = False,
precision: int = 3,
precision: int | None = None,
scientific: bool = False,
) -> Self:
"""
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ def cut(
series: bool = True,
left_closed: bool = False,
include_breaks: bool = False,
precision: int = 3,
precision: int | None = None,
scientific: bool = False,
) -> DataFrame | Series:
"""
Expand Down Expand Up @@ -1769,7 +1769,7 @@ def qcut(
left_closed: bool = False,
allow_duplicates: bool = False,
include_breaks: bool = False,
precision: int = 3,
precision: int | None = None,
scientific: bool = False,
) -> DataFrame | Series:
"""
Expand Down
6 changes: 3 additions & 3 deletions py-polars/src/expr/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl PyExpr {
labels: Option<Vec<String>>,
left_closed: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> Self {
self.inner
Expand All @@ -211,7 +211,7 @@ impl PyExpr {
left_closed: bool,
allow_duplicates: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> Self {
self.inner
Expand All @@ -237,7 +237,7 @@ impl PyExpr {
left_closed: bool,
allow_duplicates: bool,
include_breaks: bool,
precision: usize,
precision: Option<usize>,
scientific: bool,
) -> Self {
self.inner
Expand Down

0 comments on commit 95512aa

Please sign in to comment.