0

In rust polars, float number larger than 1e10 will be converted to secientific notation.

EXAMPLE:

use polars::prelude::*;
use polars::df;
use std::fs;

fn main() {
    let mut df = df!{
        "name" => ["a", "b", "c", "d", "e", "f"],
        "b" => [1, 1, 1, 1, 1, 1],
        "c" => [2, 2, 2, 2, 2, 2],
        "d" => [3, 3, 3, 3, 3, 3],
        "e" => [4.11, 4.12, 4.3, 4.4, 4.5, 4.6],
        "f" => [10000000005.1, 10000000005.2, 10000000005.3, 10000000005.4, 10000000005.5, 10000000005.6]
    }.unwrap();
    dbg!(&df);
    let file = fs::File::create("test.csv").expect("failed");
    CsvWriter::new(&file)
        .has_header(true)
        .with_delimiter(b',')
        .finish(&mut df)
        .unwrap();
}

Here is output :

[src/main.rs:14] &df = shape: (6, 6)
┌──────┬─────┬─────┬─────┬──────┬───────────┐
│ name ┆ b   ┆ c   ┆ d   ┆ e    ┆ f         │
│ ---  ┆ --- ┆ --- ┆ --- ┆ ---  ┆ ---       │
│ str  ┆ i32 ┆ i32 ┆ i32 ┆ f64  ┆ f64       │
╞══════╪═════╪═════╪═════╪══════╪═══════════╡
│ a    ┆ 1   ┆ 2   ┆ 3   ┆ 4.11 ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ b    ┆ 1   ┆ 2   ┆ 3   ┆ 4.12 ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ c    ┆ 1   ┆ 2   ┆ 3   ┆ 4.3  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ d    ┆ 1   ┆ 2   ┆ 3   ┆ 4.4  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ e    ┆ 1   ┆ 2   ┆ 3   ┆ 4.5  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ f    ┆ 1   ┆ 2   ┆ 3   ┆ 4.6  ┆ 1.0000e10 │
└──────┴─────┴─────┴─────┴──────┴───────────┘

And here is the csv file:

name,b,c,d,e,f
a,1,2,3,4.11,1.00000000051e10
b,1,2,3,4.12,1.00000000052e10
c,1,2,3,4.3,1.00000000053e10
d,1,2,3,4.4,1.00000000054e10
e,1,2,3,4.5,1.00000000055e10
f,1,2,3,4.6,1.00000000056e10

For those values larger than 1e10, they formated and displayed as secientific notation. If I use with_float_precision() method in CsvWriter, things will like this.

    ...
    CsvWriter::new(&file)
        .has_header(true)
        .with_delimiter(b',')
        .with_float_precision(Some(1))
        .finish(&mut df)
        .unwrap();
}
Termianl:
┌──────┬─────┬─────┬─────┬──────┬───────────┐
│ name ┆ b   ┆ c   ┆ d   ┆ e    ┆ f         │
│ ---  ┆ --- ┆ --- ┆ --- ┆ ---  ┆ ---       │
│ str  ┆ i32 ┆ i32 ┆ i32 ┆ f64  ┆ f64       │
╞══════╪═════╪═════╪═════╪══════╪═══════════╡
│ a    ┆ 1   ┆ 2   ┆ 3   ┆ 4.11 ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ b    ┆ 1   ┆ 2   ┆ 3   ┆ 4.12 ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ c    ┆ 1   ┆ 2   ┆ 3   ┆ 4.3  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ d    ┆ 1   ┆ 2   ┆ 3   ┆ 4.4  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ e    ┆ 1   ┆ 2   ┆ 3   ┆ 4.5  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ f    ┆ 1   ┆ 2   ┆ 3   ┆ 4.6  ┆ 1.0000e10 │
└──────┴─────┴─────┴─────┴──────┴───────────┘

CSV File:
name,b,c,d,e,f
a,1,2,3,4.1,10000000005.1
b,1,2,3,4.1,10000000005.2
c,1,2,3,4.3,10000000005.3
d,1,2,3,4.4,10000000005.4
e,1,2,3,4.5,10000000005.5
f,1,2,3,4.6,10000000005.6

OR:

    ...
    CsvWriter::new(&file)
        .has_header(true)
        .with_delimiter(b',')
        .with_float_precision(Some(2))
        .finish(&mut df)
        .unwrap();
}
Terminal:
┌──────┬─────┬─────┬─────┬──────┬───────────┐
│ name ┆ b   ┆ c   ┆ d   ┆ e    ┆ f         │
│ ---  ┆ --- ┆ --- ┆ --- ┆ ---  ┆ ---       │
│ str  ┆ i32 ┆ i32 ┆ i32 ┆ f64  ┆ f64       │
╞══════╪═════╪═════╪═════╪══════╪═══════════╡
│ a    ┆ 1   ┆ 2   ┆ 3   ┆ 4.11 ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ b    ┆ 1   ┆ 2   ┆ 3   ┆ 4.12 ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ c    ┆ 1   ┆ 2   ┆ 3   ┆ 4.3  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ d    ┆ 1   ┆ 2   ┆ 3   ┆ 4.4  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ e    ┆ 1   ┆ 2   ┆ 3   ┆ 4.5  ┆ 1.0000e10 │
├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ f    ┆ 1   ┆ 2   ┆ 3   ┆ 4.6  ┆ 1.0000e10 │
└──────┴─────┴─────┴─────┴──────┴───────────┘

CSV File:
name,b,c,d,e,f
a,1,2,3,4.11,10000000005.10
b,1,2,3,4.12,10000000005.20
c,1,2,3,4.30,10000000005.30
d,1,2,3,4.40,10000000005.40
e,1,2,3,4.50,10000000005.50
f,1,2,3,4.60,10000000005.60

This method can only disable scientific notation when writing csv, but it will also cause unnecessary precision or pricision loss.

How to disable secientific notation globally without increase or loss of precision?

Haoan
  • 71
  • 8
  • There is a `no_exponent_notation` option in `polars::lexical::NumberFormatBuilder` which may make sense. But I can't find a function to apply it. – Haoan Dec 12 '22 at 07:20

0 Answers0