Skip to main content

write_jsonl_string

Function write_jsonl_string 

Source
pub fn write_jsonl_string(frame: &DataFrame) -> Result<String, IoError>
Expand description

Write a DataFrame to JSONL (JSON Lines) format.

Matches pd.DataFrame.to_json(orient='records', lines=True). Each row is written as a separate JSON object on its own line, with no enclosing array. This format is standard for streaming data pipelines and log processing.

Examples found in repository?
examples/bench_to_jsonl.rs (line 22)
8fn main() {
9    let a: Vec<String> = std::env::args().collect();
10    let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
11    let ints: Vec<i64> = (0..n as i64)
12        .map(|i| (i.wrapping_mul(2_654_435_761)) % 1_000_000)
13        .collect();
14    let floats: Vec<f64> = (0..n).map(|i| (i as f64) * 0.5 - 100.0).collect();
15    let mut cols: BTreeMap<String, Column> = BTreeMap::new();
16    cols.insert("a".to_string(), Column::from_i64_values(ints));
17    cols.insert("b".to_string(), Column::from_f64_values(floats));
18    let frame = DataFrame::new(Index::new_known_unique_int64_unit_range(0, n), cols).unwrap();
19    let mut best = u128::MAX;
20    for _ in 0..6 {
21        let t = std::time::Instant::now();
22        let s = fp_io::write_jsonl_string(&frame).unwrap();
23        std::hint::black_box(s.len());
24        let e = t.elapsed().as_nanos();
25        if e < best {
26            best = e;
27        }
28    }
29    println!("to_jsonl n={n}: best={best}ns ({:.2}ms)", best as f64 / 1e6);
30}