pandrs 0.3.2

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! Chart implementations for text-based visualization

use super::{Chart, ChartConfig, ChartStyle};

// ============================================================================
// Histogram
// ============================================================================

/// Configuration for histogram
#[derive(Debug, Clone)]
pub struct HistogramConfig {
    /// Base chart config
    pub base: ChartConfig,
    /// Chart style
    pub style: ChartStyle,
    /// Number of bins
    pub bins: usize,
    /// Show bin counts
    pub show_counts: bool,
}

impl Default for HistogramConfig {
    fn default() -> Self {
        Self {
            base: ChartConfig::default(),
            style: ChartStyle::Unicode,
            bins: 10,
            show_counts: true,
        }
    }
}

/// Histogram chart for distribution visualization
#[derive(Debug, Clone)]
pub struct Histogram {
    /// Bin edges
    bin_edges: Vec<f64>,
    /// Bin counts
    counts: Vec<usize>,
    /// Configuration
    config: HistogramConfig,
}

impl Histogram {
    /// Create a new histogram from data
    pub fn new(data: &[f64], bins: usize) -> Self {
        let config = HistogramConfig {
            bins,
            ..Default::default()
        };
        Self::with_config(data, config)
    }

    /// Create histogram with custom configuration
    pub fn with_config(data: &[f64], config: HistogramConfig) -> Self {
        let (bin_edges, counts) = Self::compute_bins(data, config.bins);
        Self {
            bin_edges,
            counts,
            config,
        }
    }

    fn compute_bins(data: &[f64], bins: usize) -> (Vec<f64>, Vec<usize>) {
        if data.is_empty() || bins == 0 {
            return (vec![], vec![]);
        }

        let min = data.iter().cloned().fold(f64::INFINITY, f64::min);
        let max = data.iter().cloned().fold(f64::NEG_INFINITY, f64::max);

        if (max - min).abs() < f64::EPSILON {
            return (vec![min, max], vec![data.len()]);
        }

        let bin_width = (max - min) / bins as f64;
        let mut edges = Vec::with_capacity(bins + 1);
        let mut counts = vec![0; bins];

        for i in 0..=bins {
            edges.push(min + i as f64 * bin_width);
        }

        for &value in data {
            let bin_idx = ((value - min) / bin_width).floor() as usize;
            let bin_idx = bin_idx.min(bins - 1);
            counts[bin_idx] += 1;
        }

        (edges, counts)
    }

    fn get_bar_char(&self) -> char {
        match self.config.style {
            ChartStyle::Ascii => '#',
            ChartStyle::Unicode | ChartStyle::Braille => '',
        }
    }

    fn get_partial_chars(&self) -> &[char] {
        match self.config.style {
            ChartStyle::Ascii => &['#'],
            ChartStyle::Unicode | ChartStyle::Braille => &['', '', '', '', '', '', '', ''],
        }
    }
}

impl Chart for Histogram {
    fn render(&self) -> String {
        if self.counts.is_empty() {
            return String::from("No data to display");
        }

        let mut output = String::new();
        let max_count = *self.counts.iter().max().unwrap_or(&1);
        let bar_width = self.config.base.width.saturating_sub(15);
        let bar_char = self.get_bar_char();

        // Title
        if let Some(ref title) = self.config.base.title {
            output.push_str(&format!(
                "{:^width$}\n\n",
                title,
                width = self.config.base.width
            ));
        }

        // Render bars
        for (i, &count) in self.counts.iter().enumerate() {
            let bar_len = if max_count > 0 {
                (count as f64 / max_count as f64 * bar_width as f64).round() as usize
            } else {
                0
            };

            let bar: String = std::iter::repeat(bar_char).take(bar_len).collect();
            let edge_start = self.bin_edges[i];
            let edge_end = self.bin_edges[i + 1];

            if self.config.show_counts {
                output.push_str(&format!(
                    "{:>6.1}-{:<6.1} │{:<width$}│ {}\n",
                    edge_start,
                    edge_end,
                    bar,
                    count,
                    width = bar_width
                ));
            } else {
                output.push_str(&format!(
                    "{:>6.1}-{:<6.1} │{:<width$}│\n",
                    edge_start,
                    edge_end,
                    bar,
                    width = bar_width
                ));
            }
        }

        output
    }
}

// ============================================================================
// Bar Chart
// ============================================================================

/// Bar orientation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BarOrientation {
    /// Vertical bars
    Vertical,
    /// Horizontal bars
    Horizontal,
}

impl Default for BarOrientation {
    fn default() -> Self {
        Self::Horizontal
    }
}

/// Configuration for bar chart
#[derive(Debug, Clone)]
pub struct BarChartConfig {
    /// Base chart config
    pub base: ChartConfig,
    /// Chart style
    pub style: ChartStyle,
    /// Bar orientation
    pub orientation: BarOrientation,
    /// Show values on bars
    pub show_values: bool,
    /// Max label width
    pub label_width: usize,
}

impl Default for BarChartConfig {
    fn default() -> Self {
        Self {
            base: ChartConfig::default(),
            style: ChartStyle::Unicode,
            orientation: BarOrientation::Horizontal,
            show_values: true,
            label_width: 12,
        }
    }
}

/// Bar chart for categorical data
#[derive(Debug, Clone)]
pub struct BarChart {
    /// Labels for each bar
    labels: Vec<String>,
    /// Values for each bar
    values: Vec<f64>,
    /// Configuration
    config: BarChartConfig,
}

impl BarChart {
    /// Create a new bar chart
    pub fn new(labels: &[&str], values: &[f64]) -> Self {
        let config = BarChartConfig::default();
        Self::with_config(labels, values, config)
    }

    /// Create a horizontal bar chart
    pub fn horizontal(labels: &[&str], values: &[f64]) -> Self {
        let config = BarChartConfig {
            orientation: BarOrientation::Horizontal,
            ..Default::default()
        };
        Self::with_config(labels, values, config)
    }

    /// Create a vertical bar chart
    pub fn vertical(labels: &[&str], values: &[f64]) -> Self {
        let config = BarChartConfig {
            orientation: BarOrientation::Vertical,
            ..Default::default()
        };
        Self::with_config(labels, values, config)
    }

    /// Create with custom configuration
    pub fn with_config(labels: &[&str], values: &[f64], config: BarChartConfig) -> Self {
        Self {
            labels: labels.iter().map(|s| s.to_string()).collect(),
            values: values.to_vec(),
            config,
        }
    }

    fn get_bar_char(&self) -> char {
        match self.config.style {
            ChartStyle::Ascii => '#',
            ChartStyle::Unicode | ChartStyle::Braille => '',
        }
    }
}

impl Chart for BarChart {
    fn render(&self) -> String {
        if self.values.is_empty() {
            return String::from("No data to display");
        }

        match self.config.orientation {
            BarOrientation::Horizontal => self.render_horizontal(),
            BarOrientation::Vertical => self.render_vertical(),
        }
    }
}

impl BarChart {
    fn render_horizontal(&self) -> String {
        let mut output = String::new();
        let max_val = self
            .values
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max);
        let bar_width = self
            .config
            .base
            .width
            .saturating_sub(self.config.label_width + 10);
        let bar_char = self.get_bar_char();

        // Title
        if let Some(ref title) = self.config.base.title {
            output.push_str(&format!(
                "{:^width$}\n\n",
                title,
                width = self.config.base.width
            ));
        }

        for (label, &value) in self.labels.iter().zip(self.values.iter()) {
            let bar_len = if max_val > 0.0 {
                (value / max_val * bar_width as f64).round() as usize
            } else {
                0
            };

            let bar: String = std::iter::repeat(bar_char).take(bar_len).collect();
            let truncated_label: String = label.chars().take(self.config.label_width).collect();

            if self.config.show_values {
                output.push_str(&format!(
                    "{:>label_width$} │{:<bar_width$}│ {:.2}\n",
                    truncated_label,
                    bar,
                    value,
                    label_width = self.config.label_width,
                    bar_width = bar_width
                ));
            } else {
                output.push_str(&format!(
                    "{:>label_width$} │{:<bar_width$}│\n",
                    truncated_label,
                    bar,
                    label_width = self.config.label_width,
                    bar_width = bar_width
                ));
            }
        }

        output
    }

    fn render_vertical(&self) -> String {
        let mut output = String::new();
        let max_val = self
            .values
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max);
        let height = self.config.base.height.min(20);
        let bar_char = self.get_bar_char();

        // Title
        if let Some(ref title) = self.config.base.title {
            output.push_str(&format!(
                "{:^width$}\n\n",
                title,
                width = self.labels.len() * 4
            ));
        }

        // Normalize values to height
        let normalized: Vec<usize> = self
            .values
            .iter()
            .map(|&v| {
                if max_val > 0.0 {
                    (v / max_val * height as f64).round() as usize
                } else {
                    0
                }
            })
            .collect();

        // Render from top to bottom
        for row in (0..height).rev() {
            for &bar_height in &normalized {
                if bar_height > row {
                    output.push_str(&format!(" {} ", bar_char));
                } else {
                    output.push_str("   ");
                }
            }
            output.push('\n');
        }

        // Baseline
        for _ in 0..self.labels.len() {
            output.push_str("───");
        }
        output.push('\n');

        // Labels (abbreviated)
        for label in &self.labels {
            let abbrev: String = label.chars().take(2).collect();
            output.push_str(&format!(" {} ", abbrev));
        }
        output.push('\n');

        output
    }
}

// ============================================================================
// Line Plot
// ============================================================================

/// Configuration for line plot
#[derive(Debug, Clone)]
pub struct LinePlotConfig {
    /// Base chart config
    pub base: ChartConfig,
    /// Chart style
    pub style: ChartStyle,
    /// Show data points
    pub show_points: bool,
    /// Point character
    pub point_char: char,
}

impl Default for LinePlotConfig {
    fn default() -> Self {
        Self {
            base: ChartConfig {
                height: 10,
                ..Default::default()
            },
            style: ChartStyle::Unicode,
            show_points: true,
            point_char: '',
        }
    }
}

/// Line plot for time series or sequential data
#[derive(Debug, Clone)]
pub struct LinePlot {
    /// Data values
    values: Vec<f64>,
    /// Configuration
    config: LinePlotConfig,
}

impl LinePlot {
    /// Create a new line plot
    pub fn new(values: &[f64]) -> Self {
        Self::with_config(values, LinePlotConfig::default())
    }

    /// Create with custom configuration
    pub fn with_config(values: &[f64], config: LinePlotConfig) -> Self {
        Self {
            values: values.to_vec(),
            config,
        }
    }
}

impl Chart for LinePlot {
    fn render(&self) -> String {
        if self.values.is_empty() {
            return String::from("No data to display");
        }

        let mut output = String::new();
        let height = self.config.base.height;
        let width = self.config.base.width.min(self.values.len());

        let min_val = self.values.iter().cloned().fold(f64::INFINITY, f64::min);
        let max_val = self
            .values
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max);
        let range = if (max_val - min_val).abs() < f64::EPSILON {
            1.0
        } else {
            max_val - min_val
        };

        // Title
        if let Some(ref title) = self.config.base.title {
            output.push_str(&format!("{:^width$}\n\n", title, width = width));
        }

        // Sample data to fit width
        let step = self.values.len() as f64 / width as f64;
        let sampled: Vec<usize> = (0..width)
            .map(|i| {
                let idx = (i as f64 * step).floor() as usize;
                let val = self.values[idx.min(self.values.len() - 1)];
                ((val - min_val) / range * (height - 1) as f64).round() as usize
            })
            .collect();

        // Render from top to bottom
        for row in (0..height).rev() {
            // Y-axis label
            if self.config.base.show_labels {
                let y_val = min_val + (row as f64 / (height - 1) as f64) * range;
                output.push_str(&format!("{:>6.1}", y_val));
            }

            for &y in &sampled {
                if y == row {
                    output.push(self.config.point_char);
                } else {
                    output.push(' ');
                }
            }
            output.push('\n');
        }

        // X-axis
        if self.config.base.show_labels {
            output.push_str("");
            for _ in 0..width {
                output.push('');
            }
            output.push('\n');
        }

        output
    }
}

// ============================================================================
// Scatter Plot
// ============================================================================

/// Configuration for scatter plot
#[derive(Debug, Clone)]
pub struct ScatterPlotConfig {
    /// Base chart config
    pub base: ChartConfig,
    /// Chart style
    pub style: ChartStyle,
    /// Point character
    pub point_char: char,
}

impl Default for ScatterPlotConfig {
    fn default() -> Self {
        Self {
            base: ChartConfig {
                height: 15,
                width: 40,
                ..Default::default()
            },
            style: ChartStyle::Unicode,
            point_char: '',
        }
    }
}

/// Scatter plot for two-dimensional data
#[derive(Debug, Clone)]
pub struct ScatterPlot {
    /// X values
    x: Vec<f64>,
    /// Y values
    y: Vec<f64>,
    /// Configuration
    config: ScatterPlotConfig,
}

impl ScatterPlot {
    /// Create a new scatter plot
    pub fn new(x: &[f64], y: &[f64]) -> Self {
        Self::with_config(x, y, ScatterPlotConfig::default())
    }

    /// Create with custom configuration
    pub fn with_config(x: &[f64], y: &[f64], config: ScatterPlotConfig) -> Self {
        Self {
            x: x.to_vec(),
            y: y.to_vec(),
            config,
        }
    }
}

impl Chart for ScatterPlot {
    fn render(&self) -> String {
        if self.x.is_empty() || self.y.is_empty() {
            return String::from("No data to display");
        }

        let len = self.x.len().min(self.y.len());
        let height = self.config.base.height;
        let width = self.config.base.width;

        let x_min = self.x.iter().cloned().fold(f64::INFINITY, f64::min);
        let x_max = self.x.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        let y_min = self.y.iter().cloned().fold(f64::INFINITY, f64::min);
        let y_max = self.y.iter().cloned().fold(f64::NEG_INFINITY, f64::max);

        let x_range = if (x_max - x_min).abs() < f64::EPSILON {
            1.0
        } else {
            x_max - x_min
        };
        let y_range = if (y_max - y_min).abs() < f64::EPSILON {
            1.0
        } else {
            y_max - y_min
        };

        // Create grid
        let mut grid = vec![vec![' '; width]; height];

        // Plot points
        for i in 0..len {
            let px = ((self.x[i] - x_min) / x_range * (width - 1) as f64).round() as usize;
            let py = ((self.y[i] - y_min) / y_range * (height - 1) as f64).round() as usize;
            let px = px.min(width - 1);
            let py = py.min(height - 1);
            grid[py][px] = self.config.point_char;
        }

        // Render
        let mut output = String::new();

        // Title
        if let Some(ref title) = self.config.base.title {
            output.push_str(&format!("{:^width$}\n\n", title, width = width + 8));
        }

        for row in (0..height).rev() {
            if self.config.base.show_labels {
                let y_val = y_min + (row as f64 / (height - 1) as f64) * y_range;
                output.push_str(&format!("{:>6.1}", y_val));
            }
            for col in 0..width {
                output.push(grid[row][col]);
            }
            output.push('\n');
        }

        // X-axis
        if self.config.base.show_labels {
            output.push_str("");
            for _ in 0..width {
                output.push('');
            }
            output.push('\n');
            output.push_str(&format!(
                "        {:<width$.1}{:>8.1}\n",
                x_min,
                x_max,
                width = width - 8
            ));
        }

        output
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_histogram_creation() {
        let data = vec![1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0, 4.0, 5.0];
        let hist = Histogram::new(&data, 5);
        let output = hist.render();
        assert!(!output.is_empty());
    }

    #[test]
    fn test_histogram_empty() {
        let data: Vec<f64> = vec![];
        let hist = Histogram::new(&data, 5);
        let output = hist.render();
        assert!(output.contains("No data"));
    }

    #[test]
    fn test_bar_chart_horizontal() {
        let labels = vec!["A", "B", "C"];
        let values = vec![10.0, 20.0, 15.0];
        let chart = BarChart::horizontal(&labels, &values);
        let output = chart.render();
        assert!(output.contains("A"));
        assert!(output.contains("B"));
        assert!(output.contains("C"));
    }

    #[test]
    fn test_bar_chart_vertical() {
        let labels = vec!["A", "B", "C"];
        let values = vec![10.0, 20.0, 15.0];
        let chart = BarChart::vertical(&labels, &values);
        let output = chart.render();
        assert!(!output.is_empty());
    }

    #[test]
    fn test_line_plot() {
        let data = vec![1.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0];
        let plot = LinePlot::new(&data);
        let output = plot.render();
        assert!(!output.is_empty());
        assert!(output.contains(''));
    }

    #[test]
    fn test_scatter_plot() {
        let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let y = vec![1.0, 4.0, 2.0, 5.0, 3.0];
        let plot = ScatterPlot::new(&x, &y);
        let output = plot.render();
        assert!(!output.is_empty());
        assert!(output.contains(''));
    }

    #[test]
    fn test_bar_chart_with_title() {
        let labels = vec!["A", "B"];
        let values = vec![10.0, 20.0];
        let config = BarChartConfig {
            base: ChartConfig {
                title: Some("Test Chart".to_string()),
                ..Default::default()
            },
            ..Default::default()
        };
        let chart = BarChart::with_config(&labels, &values, config);
        let output = chart.render();
        assert!(output.contains("Test Chart"));
    }
}