pandrs 0.4.1

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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Machine learning pipelines
//!
//! This module provides functionality for creating pipelines of data
//! transformations and machine learning models.

use crate::core::error::{Error, Result};
use crate::dataframe::DataFrame;
use crate::series::Series;
use std::collections::HashMap;

/// Trait for pipeline stages that transform DataFrames
pub trait PipelineTransformer {
    /// Transform DataFrame according to this pipeline stage
    fn transform(&self, df: &DataFrame) -> Result<DataFrame>;

    /// Fit this transformer to the data (if needed)
    fn fit(&mut self, df: &DataFrame) -> Result<()>;

    /// Fit and transform in one step
    fn fit_transform(&mut self, df: &DataFrame) -> Result<DataFrame> {
        self.fit(df)?;
        self.transform(df)
    }
}

/// Enum for different types of pipeline stages
#[derive(Debug)]
pub enum PipelineStage {
    /// Standard scaler
    StandardScaler {
        /// Columns to scale
        columns: Option<Vec<String>>,
        /// Internal storage for fit parameters
        _means: Option<HashMap<String, f64>>,
        _stds: Option<HashMap<String, f64>>,
    },

    /// Min-max scaler
    MinMaxScaler {
        /// Columns to scale
        columns: Option<Vec<String>>,
        /// Feature range (min, max)
        feature_range: (f64, f64),
        /// Internal storage for fit parameters
        _min_values: Option<HashMap<String, f64>>,
        _max_values: Option<HashMap<String, f64>>,
    },

    /// One-hot encoder
    OneHotEncoder {
        /// Columns to encode
        columns: Option<Vec<String>>,
        /// Whether to drop the first category
        drop_first: bool,
        /// Prefix for new column names
        prefix: Option<String>,
        /// Internal storage for fit parameters
        _categories: Option<HashMap<String, Vec<String>>>,
    },

    /// Imputer for missing values
    Imputer {
        /// Columns to impute
        columns: Option<Vec<String>>,
        /// Imputation strategy
        strategy: String,
        /// Constant value for constant strategy
        fill_value: Option<f64>,
        /// Internal storage for fit parameters
        _fill_values: Option<HashMap<String, f64>>,
    },

    /// Feature selector
    FeatureSelector {
        /// Columns to select
        columns: Vec<String>,
    },
    // Custom transformer (commented out since we can't implement Debug for it)
    /*
    Custom {
        // Custom transformation function
        transform_fn: Box<dyn Fn(&DataFrame) -> Result<DataFrame>>,
    },
    */
}

impl PipelineTransformer for PipelineStage {
    fn transform(&self, df: &DataFrame) -> Result<DataFrame> {
        match self {
            PipelineStage::StandardScaler { _means, _stds, .. } => {
                let means = _means.as_ref().ok_or_else(not_fitted("StandardScaler"))?;
                let stds = _stds.as_ref().ok_or_else(not_fitted("StandardScaler"))?;

                let mut result = DataFrame::new();
                for col_name in df.column_names() {
                    if let Some(&mean) = means.get(col_name.as_str()) {
                        let std = stds.get(col_name.as_str()).copied().unwrap_or(1.0);
                        let values = df.get_column_numeric_values(col_name.as_str())?;
                        let scaled: Vec<f64> = values
                            .iter()
                            .map(|&v| {
                                if std > 1e-10 {
                                    (v - mean) / std
                                } else {
                                    v - mean
                                }
                            })
                            .collect();
                        result.add_column(
                            col_name.clone(),
                            Series::new(scaled, Some(col_name.clone()))?,
                        )?;
                    } else {
                        passthrough_column(df, &mut result, col_name.as_str())?;
                    }
                }
                Ok(result)
            }

            PipelineStage::MinMaxScaler {
                feature_range,
                _min_values,
                _max_values,
                ..
            } => {
                let mins = _min_values
                    .as_ref()
                    .ok_or_else(not_fitted("MinMaxScaler"))?;
                let maxs = _max_values
                    .as_ref()
                    .ok_or_else(not_fitted("MinMaxScaler"))?;
                let (out_min, out_max) = *feature_range;

                let mut result = DataFrame::new();
                for col_name in df.column_names() {
                    if let (Some(&min_v), Some(&max_v)) =
                        (mins.get(col_name.as_str()), maxs.get(col_name.as_str()))
                    {
                        let values = df.get_column_numeric_values(col_name.as_str())?;
                        let span = max_v - min_v;
                        let scaled: Vec<f64> = values
                            .iter()
                            .map(|&v| {
                                if span.abs() < f64::EPSILON {
                                    // sklearn's MinMaxScaler special-cases a
                                    // degenerate (constant) column to
                                    // `feature_range[0]`, not the midpoint
                                    // of the range.
                                    out_min
                                } else {
                                    out_min + (out_max - out_min) * (v - min_v) / span
                                }
                            })
                            .collect();
                        result.add_column(
                            col_name.clone(),
                            Series::new(scaled, Some(col_name.clone()))?,
                        )?;
                    } else {
                        passthrough_column(df, &mut result, col_name.as_str())?;
                    }
                }
                Ok(result)
            }

            PipelineStage::OneHotEncoder {
                drop_first,
                prefix,
                _categories,
                ..
            } => {
                let categories = _categories
                    .as_ref()
                    .ok_or_else(not_fitted("OneHotEncoder"))?;

                let mut result = DataFrame::new();
                for col_name in df.column_names() {
                    if let Some(cats) = categories.get(col_name.as_str()) {
                        let values = df.get_column_string_values(col_name.as_str())?;
                        let start = if *drop_first { 1 } else { 0 };
                        let base = prefix.clone().unwrap_or_else(|| col_name.clone());
                        for cat in cats.iter().skip(start) {
                            let dummy: Vec<f64> = values
                                .iter()
                                .map(|v| if v == cat { 1.0 } else { 0.0 })
                                .collect();
                            let new_name = format!("{}_{}", base, cat);
                            result.add_column(
                                new_name.clone(),
                                Series::new(dummy, Some(new_name))?,
                            )?;
                        }
                    } else {
                        passthrough_column(df, &mut result, col_name.as_str())?;
                    }
                }
                Ok(result)
            }

            PipelineStage::Imputer { _fill_values, .. } => {
                let fills = _fill_values.as_ref().ok_or_else(not_fitted("Imputer"))?;

                let mut result = DataFrame::new();
                for col_name in df.column_names() {
                    if let Some(&fill) = fills.get(col_name.as_str()) {
                        let values = df.get_column_numeric_values(col_name.as_str())?;
                        let imputed: Vec<f64> = values
                            .iter()
                            .map(|&v| if v.is_nan() { fill } else { v })
                            .collect();
                        result.add_column(
                            col_name.clone(),
                            Series::new(imputed, Some(col_name.clone()))?,
                        )?;
                    } else {
                        passthrough_column(df, &mut result, col_name.as_str())?;
                    }
                }
                Ok(result)
            }

            PipelineStage::FeatureSelector { columns } => {
                for col_name in columns {
                    if !df.contains_column(col_name) {
                        return Err(Error::InvalidValue(format!(
                            "Column '{}' not found",
                            col_name
                        )));
                    }
                }
                let refs: Vec<&str> = columns.iter().map(|s| s.as_str()).collect();
                df.select_columns(&refs)
            }
        }
    }

    fn fit(&mut self, df: &DataFrame) -> Result<()> {
        match self {
            PipelineStage::StandardScaler {
                columns,
                _means,
                _stds,
            } => {
                let targets = resolve_numeric_targets(df, columns);
                let mut means = HashMap::new();
                let mut stds = HashMap::new();

                for col_name in &targets {
                    let values = df.get_column_numeric_values(col_name)?;
                    if values.is_empty() {
                        continue;
                    }
                    let n = values.len() as f64;
                    let mean = values.iter().sum::<f64>() / n;
                    let variance = values.iter().map(|&v| (v - mean).powi(2)).sum::<f64>() / n;
                    means.insert(col_name.clone(), mean);
                    stds.insert(col_name.clone(), variance.sqrt());
                }

                *_means = Some(means);
                *_stds = Some(stds);
                Ok(())
            }

            PipelineStage::MinMaxScaler {
                columns,
                _min_values,
                _max_values,
                ..
            } => {
                let targets = resolve_numeric_targets(df, columns);
                let mut mins = HashMap::new();
                let mut maxs = HashMap::new();

                for col_name in &targets {
                    let values = df.get_column_numeric_values(col_name)?;
                    if values.is_empty() {
                        continue;
                    }
                    let min_v = values.iter().cloned().fold(f64::INFINITY, f64::min);
                    let max_v = values.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                    mins.insert(col_name.clone(), min_v);
                    maxs.insert(col_name.clone(), max_v);
                }

                *_min_values = Some(mins);
                *_max_values = Some(maxs);
                Ok(())
            }

            PipelineStage::OneHotEncoder {
                columns,
                _categories,
                ..
            } => {
                let targets = resolve_categorical_targets(df, columns);
                let mut categories = HashMap::new();

                for col_name in &targets {
                    let values = df.get_column_string_values(col_name)?;
                    // BTreeSet yields sorted, de-duplicated categories for stable column ordering.
                    let unique: Vec<String> = values
                        .into_iter()
                        .collect::<std::collections::BTreeSet<_>>()
                        .into_iter()
                        .collect();
                    categories.insert(col_name.clone(), unique);
                }

                *_categories = Some(categories);
                Ok(())
            }

            PipelineStage::Imputer {
                columns,
                strategy,
                fill_value,
                _fill_values,
            } => {
                let targets = resolve_numeric_targets(df, columns);
                let mut fills = HashMap::new();

                for col_name in &targets {
                    let values = df.get_column_numeric_values(col_name)?;
                    let present: Vec<f64> =
                        values.iter().cloned().filter(|v| !v.is_nan()).collect();

                    // An all-missing column has no observed values to
                    // derive a mean/median/mode from; fabricating 0.0 (as
                    // this used to for "mean", and as `median`/`mode`'s
                    // own empty-input fallback silently did) would
                    // substitute a made-up number for data that was never
                    // observed, indistinguishable downstream from a
                    // genuine 0.0 reading.
                    if present.is_empty()
                        && matches!(
                            strategy.to_lowercase().as_str(),
                            "mean" | "median" | "most_frequent" | "mode"
                        )
                    {
                        return Err(Error::InvalidValue(format!(
                            "Imputer: column '{}' has no non-missing values; cannot compute \
                             a '{}' fill value from it",
                            col_name, strategy
                        )));
                    }

                    let fill = match strategy.to_lowercase().as_str() {
                        "mean" => present.iter().sum::<f64>() / present.len() as f64,
                        "median" => median(&present),
                        "most_frequent" | "mode" => mode(&present),
                        "constant" => fill_value.ok_or_else(|| {
                            Error::InvalidValue(
                                "Imputer: strategy 'constant' requires fill_value to be set"
                                    .to_string(),
                            )
                        })?,
                        other => {
                            return Err(Error::InvalidValue(format!(
                                "Unknown imputer strategy '{}'",
                                other
                            )))
                        }
                    };
                    fills.insert(col_name.clone(), fill);
                }

                *_fill_values = Some(fills);
                Ok(())
            }

            PipelineStage::FeatureSelector { .. } => Ok(()),
        }
    }
}

/// Build a closure that produces a "not fitted" error for the named stage.
fn not_fitted(stage: &'static str) -> impl Fn() -> Error {
    move || Error::InvalidOperation(format!("{} must be fitted before transform", stage))
}

/// Whether `name` names a column whose CONCRETE storage type is numeric
/// (`f64`, `f32`, `i64`, `i32`, or `bool`), checked via `get_column::<T>`'s
/// exact-type downcast rather than string parseability.
///
/// `get_column_numeric_values` also successfully parses a `Series<String>`
/// column whose text happens to look numeric (e.g. digit-coded category
/// labels like `"1"`, `"2"`), so using it to decide "is this numeric" put
/// such categorical columns through `StandardScaler`/`MinMaxScaler`/
/// `Imputer` as if they were continuous features, while simultaneously
/// excluding them from `resolve_categorical_targets` below.
fn is_concrete_numeric_column(df: &DataFrame, name: &str) -> bool {
    df.get_column::<f64>(name).is_ok()
        || df.get_column::<f32>(name).is_ok()
        || df.get_column::<i64>(name).is_ok()
        || df.get_column::<i32>(name).is_ok()
        || df.get_column::<bool>(name).is_ok()
}

/// Whether `name` names a column whose concrete storage type is `String`.
fn is_concrete_string_column(df: &DataFrame, name: &str) -> bool {
    df.get_column::<String>(name).is_ok()
}

/// Resolve the numeric target columns for a scaler/imputer: the explicit list, or every
/// column whose CONCRETE type is numeric when none was specified.
fn resolve_numeric_targets(df: &DataFrame, columns: &Option<Vec<String>>) -> Vec<String> {
    match columns {
        Some(cols) => cols.clone(),
        None => df
            .column_names()
            .iter()
            .filter(|name| is_concrete_numeric_column(df, name.as_str()))
            .cloned()
            .collect(),
    }
}

/// Resolve categorical target columns for one-hot encoding: the explicit list, or every
/// column whose CONCRETE type is `String` when none was specified.
fn resolve_categorical_targets(df: &DataFrame, columns: &Option<Vec<String>>) -> Vec<String> {
    match columns {
        Some(cols) => cols.clone(),
        None => df
            .column_names()
            .iter()
            .filter(|name| is_concrete_string_column(df, name.as_str()))
            .cloned()
            .collect(),
    }
}

/// Copy a column unchanged into `result`, preserving numeric vs. string representation.
///
/// Routes by the column's CONCRETE storage type, via the same
/// [`is_concrete_string_column`] check `resolve_categorical_targets` uses to decide
/// what is categorical -- not by which read happens to parse successfully. A
/// digit-coded categorical `Series<String>` column (e.g. `"1"`, `"2"`) parses
/// successfully through `get_column_numeric_values` (see the doc comment on
/// `is_concrete_numeric_column`), so trying that read first -- as this previously
/// did -- silently rewrote such a column to `Series<f64>` on every pass-through.
/// That is invisible to whichever stage produced `result`, but breaks any *later*
/// stage in the same `Pipeline` (e.g. `OneHotEncoder`) that also decides "is this
/// categorical?" from the concrete type: it would see an f64 column and skip it,
/// even though `resolve_categorical_targets` correctly identified it as
/// categorical when this same column was still the pipeline's input.
fn passthrough_column(df: &DataFrame, result: &mut DataFrame, name: &str) -> Result<()> {
    if is_concrete_string_column(df, name) {
        let values = df.get_column_string_values(name)?;
        result.add_column(
            name.to_string(),
            Series::new(values, Some(name.to_string()))?,
        )?;
    } else if let Ok(values) = df.get_column_numeric_values(name) {
        result.add_column(
            name.to_string(),
            Series::new(values, Some(name.to_string()))?,
        )?;
    } else if let Ok(values) = df.get_column_string_values(name) {
        result.add_column(
            name.to_string(),
            Series::new(values, Some(name.to_string()))?,
        )?;
    } else {
        return Err(Error::InvalidValue(format!(
            "Unable to determine type of column '{}' for pass-through",
            name
        )));
    }
    Ok(())
}

/// Median of a slice (returns 0.0 for an empty slice).
fn median(values: &[f64]) -> f64 {
    if values.is_empty() {
        return 0.0;
    }
    let mut sorted = values.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    let n = sorted.len();
    if n % 2 == 1 {
        sorted[n / 2]
    } else {
        (sorted[n / 2 - 1] + sorted[n / 2]) / 2.0
    }
}

/// Most frequent value (mode). Ties are broken by the smaller value; empty input yields 0.0.
fn mode(values: &[f64]) -> f64 {
    if values.is_empty() {
        return 0.0;
    }
    let mut counts: HashMap<u64, usize> = HashMap::new();
    for &v in values {
        *counts.entry(v.to_bits()).or_insert(0) += 1;
    }
    let mut best_bits = values[0].to_bits();
    let mut best_count = 0usize;
    for (&bits, &count) in &counts {
        let value = f64::from_bits(bits);
        if count > best_count || (count == best_count && value < f64::from_bits(best_bits)) {
            best_count = count;
            best_bits = bits;
        }
    }
    f64::from_bits(best_bits)
}

/// Pipeline for chaining multiple data transformation steps
#[derive(Debug)]
pub struct Pipeline {
    /// Pipeline stages
    pub stages: Vec<PipelineStage>,
}

impl Pipeline {
    /// Create a new empty pipeline
    pub fn new() -> Self {
        Pipeline { stages: Vec::new() }
    }

    /// Add a stage to the pipeline
    pub fn add_stage(&mut self, stage: PipelineStage) -> &mut Self {
        self.stages.push(stage);
        self
    }

    /// Fit the pipeline to the data
    pub fn fit(&mut self, df: &DataFrame) -> Result<()> {
        let mut current_df = df.clone();

        for stage in &mut self.stages {
            stage.fit(&current_df)?;
            current_df = stage.transform(&current_df)?;
        }

        Ok(())
    }

    /// Transform data using the fitted pipeline
    pub fn transform(&self, df: &DataFrame) -> Result<DataFrame> {
        let mut current_df = df.clone();

        for stage in &self.stages {
            current_df = stage.transform(&current_df)?;
        }

        Ok(current_df)
    }

    /// Fit the pipeline and transform data in one step
    pub fn fit_transform(&mut self, df: &DataFrame) -> Result<DataFrame> {
        let mut current_df = df.clone();

        for stage in &mut self.stages {
            stage.fit(&current_df)?;
            current_df = stage.transform(&current_df)?;
        }

        Ok(current_df)
    }
}

// No need for re-exports here as the types are already defined in this module

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

    fn numeric_df() -> DataFrame {
        let mut df = DataFrame::new();
        df.add_column(
            "a".to_string(),
            Series::new(vec![1.0, 2.0, 3.0, 4.0, 5.0], Some("a".to_string())).unwrap(),
        )
        .unwrap();
        df.add_column(
            "b".to_string(),
            Series::new(vec![10.0, 20.0, 30.0, 40.0, 50.0], Some("b".to_string())).unwrap(),
        )
        .unwrap();
        df
    }

    #[test]
    fn test_standard_scaler_real() {
        let df = numeric_df();
        let mut stage = PipelineStage::StandardScaler {
            columns: None,
            _means: None,
            _stds: None,
        };
        stage.fit(&df).unwrap();
        let out = stage.transform(&df).unwrap();

        let a = out.get_column_numeric_values("a").unwrap();
        let mean: f64 = a.iter().sum::<f64>() / a.len() as f64;
        let var: f64 = a.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / a.len() as f64;
        assert!(
            mean.abs() < 1e-9,
            "standardized mean should be ~0, got {mean}"
        );
        assert!(
            (var - 1.0).abs() < 1e-6,
            "standardized variance should be ~1, got {var}"
        );
    }

    #[test]
    fn test_min_max_scaler_real() {
        let df = numeric_df();
        let mut stage = PipelineStage::MinMaxScaler {
            columns: None,
            feature_range: (0.0, 1.0),
            _min_values: None,
            _max_values: None,
        };
        stage.fit(&df).unwrap();
        let out = stage.transform(&df).unwrap();

        let a = out.get_column_numeric_values("a").unwrap();
        assert!((a[0] - 0.0).abs() < 1e-9);
        assert!((a[4] - 1.0).abs() < 1e-9);
        assert!((a[2] - 0.5).abs() < 1e-9);
    }

    #[test]
    fn test_one_hot_encoder_real() {
        let mut df = DataFrame::new();
        df.add_column(
            "color".to_string(),
            Series::new(
                vec!["red".to_string(), "blue".to_string(), "red".to_string()],
                Some("color".to_string()),
            )
            .unwrap(),
        )
        .unwrap();

        let mut stage = PipelineStage::OneHotEncoder {
            columns: Some(vec!["color".to_string()]),
            drop_first: false,
            prefix: None,
            _categories: None,
        };
        stage.fit(&df).unwrap();
        let out = stage.transform(&df).unwrap();

        // Categories are sorted -> blue, red.
        assert!(out.contains_column("color_blue"));
        assert!(out.contains_column("color_red"));
        assert_eq!(
            out.get_column_numeric_values("color_red").unwrap(),
            vec![1.0, 0.0, 1.0]
        );
        assert_eq!(
            out.get_column_numeric_values("color_blue").unwrap(),
            vec![0.0, 1.0, 0.0]
        );
    }

    #[test]
    fn test_one_hot_encoder_drop_first() {
        let mut df = DataFrame::new();
        df.add_column(
            "color".to_string(),
            Series::new(
                vec!["red".to_string(), "blue".to_string(), "green".to_string()],
                Some("color".to_string()),
            )
            .unwrap(),
        )
        .unwrap();

        let mut stage = PipelineStage::OneHotEncoder {
            columns: Some(vec!["color".to_string()]),
            drop_first: true,
            prefix: None,
            _categories: None,
        };
        stage.fit(&df).unwrap();
        let out = stage.transform(&df).unwrap();

        // Sorted categories blue, green, red -> first ("blue") dropped.
        assert!(!out.contains_column("color_blue"));
        assert!(out.contains_column("color_green"));
        assert!(out.contains_column("color_red"));
    }

    #[test]
    fn test_imputer_mean_real() {
        let mut df = DataFrame::new();
        df.add_column(
            "x".to_string(),
            Series::new(vec![1.0, f64::NAN, 3.0], Some("x".to_string())).unwrap(),
        )
        .unwrap();

        let mut stage = PipelineStage::Imputer {
            columns: Some(vec!["x".to_string()]),
            strategy: "mean".to_string(),
            fill_value: None,
            _fill_values: None,
        };
        stage.fit(&df).unwrap();
        let out = stage.transform(&df).unwrap();

        // Mean of present values {1, 3} = 2.0
        assert_eq!(
            out.get_column_numeric_values("x").unwrap(),
            vec![1.0, 2.0, 3.0]
        );
    }

    #[test]
    fn test_imputer_constant_real() {
        let mut df = DataFrame::new();
        df.add_column(
            "x".to_string(),
            Series::new(vec![f64::NAN, 5.0, f64::NAN], Some("x".to_string())).unwrap(),
        )
        .unwrap();

        let mut stage = PipelineStage::Imputer {
            columns: Some(vec!["x".to_string()]),
            strategy: "constant".to_string(),
            fill_value: Some(-1.0),
            _fill_values: None,
        };
        stage.fit(&df).unwrap();
        let out = stage.transform(&df).unwrap();
        assert_eq!(
            out.get_column_numeric_values("x").unwrap(),
            vec![-1.0, 5.0, -1.0]
        );
    }

    #[test]
    fn test_feature_selector_numeric_columns() {
        // Regression test: the old implementation read columns as Series<String>
        // and errored on real numeric data. Selection must now work on f64 columns.
        let df = numeric_df();
        let stage = PipelineStage::FeatureSelector {
            columns: vec!["a".to_string()],
        };
        let out = stage.transform(&df).unwrap();
        assert_eq!(out.column_names(), vec!["a".to_string()]);
        assert_eq!(
            out.get_column_numeric_values("a").unwrap(),
            vec![1.0, 2.0, 3.0, 4.0, 5.0]
        );
    }

    #[test]
    fn test_unfitted_transform_errors() {
        let df = numeric_df();
        let stage = PipelineStage::StandardScaler {
            columns: None,
            _means: None,
            _stds: None,
        };
        // Transforming before fit must error, not silently pass through.
        assert!(stage.transform(&df).is_err());
    }

    #[test]
    fn test_pipeline_fit_transform_end_to_end() {
        let df = numeric_df();
        let mut pipeline = Pipeline::new();
        pipeline.add_stage(PipelineStage::StandardScaler {
            columns: None,
            _means: None,
            _stds: None,
        });
        let out = pipeline.fit_transform(&df).unwrap();

        for col in ["a", "b"] {
            let values = out.get_column_numeric_values(col).unwrap();
            let mean: f64 = values.iter().sum::<f64>() / values.len() as f64;
            assert!(mean.abs() < 1e-9, "column {col} should be centered");
        }
    }
}