greeners 1.5.3

High-performance econometrics with R/Python formulas. Two-Way Clustering, Marginal Effects (AME/MEM), HC1-4, IV Predictions, Categorical C(var), Polynomial I(x^2), Interactions, Diagnostics. OLS, IV/2SLS, DiD, Logit/Probit, Panel (FE/RE), Time Series (VAR/VECM), Quantile!
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
use chrono::NaiveDateTime;
use indexmap::IndexMap;
use ndarray::Array1;

/// Data type of a column
#[derive(Debug, Clone, PartialEq)]
pub enum DataType {
    Float,
    Categorical,
    Bool,
    Int,
    DateTime,
    String,
}

/// Categorical column with string levels and integer codes
#[derive(Debug, Clone)]
pub struct CategoricalColumn {
    /// Unique category levels (e.g., ["SP", "RJ", "MG"])
    pub levels: Vec<String>,
    /// Integer codes mapping to levels (e.g., [0, 1, 0, 2])
    pub codes: Vec<u32>,
    /// Reverse mapping: level name -> code
    level_to_code: IndexMap<String, u32>,
}

impl CategoricalColumn {
    /// Create a new categorical column from string values
    pub fn from_strings(values: Vec<String>) -> Self {
        let mut levels = Vec::new();
        let mut level_to_code = IndexMap::new();
        let mut codes = Vec::new();

        for value in values {
            let code = if let Some(&existing_code) = level_to_code.get(&value) {
                existing_code
            } else {
                let new_code = levels.len() as u32;
                levels.push(value.clone());
                level_to_code.insert(value.clone(), new_code);
                new_code
            };
            codes.push(code);
        }

        CategoricalColumn {
            levels,
            codes,
            level_to_code,
        }
    }

    /// Create from existing levels and codes (for internal use)
    pub fn from_codes(levels: Vec<String>, codes: Vec<u32>) -> Self {
        let level_to_code = levels
            .iter()
            .enumerate()
            .map(|(i, level)| (level.clone(), i as u32))
            .collect();

        CategoricalColumn {
            levels,
            codes,
            level_to_code,
        }
    }

    /// Get the number of rows
    pub fn len(&self) -> usize {
        self.codes.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.codes.is_empty()
    }

    /// Get level name by code
    pub fn get_level(&self, code: u32) -> Option<&str> {
        self.levels.get(code as usize).map(|s| s.as_str())
    }

    /// Get code by level name
    pub fn get_code(&self, level: &str) -> Option<u32> {
        self.level_to_code.get(level).copied()
    }

    /// Get string value at index
    pub fn get_string(&self, index: usize) -> Option<&str> {
        self.codes.get(index).and_then(|&code| self.get_level(code))
    }

    /// Convert to string vector
    pub fn to_strings(&self) -> Vec<String> {
        self.codes
            .iter()
            .map(|&code| {
                self.get_level(code)
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| "NA".to_string())
            })
            .collect()
    }

    /// Convert to float codes (for numeric operations)
    pub fn to_float_codes(&self) -> Array1<f64> {
        Array1::from(self.codes.iter().map(|&c| c as f64).collect::<Vec<_>>())
    }

    /// Get number of unique levels
    pub fn n_levels(&self) -> usize {
        self.levels.len()
    }

    /// Get value counts
    pub fn value_counts(&self) -> IndexMap<String, usize> {
        let mut counts = IndexMap::new();
        for &code in &self.codes {
            if let Some(level) = self.get_level(code) {
                *counts.entry(level.to_string()).or_insert(0) += 1;
            }
        }
        counts
    }

    /// Filter by indices
    pub fn filter_indices(&self, indices: &[usize]) -> Self {
        let codes = indices.iter().map(|&i| self.codes[i]).collect();
        CategoricalColumn::from_codes(self.levels.clone(), codes)
    }

    /// Create dummy variables (one-hot encoding)
    /// Returns HashMap of column_name -> Array1<f64>
    pub fn get_dummies(&self, prefix: &str, drop_first: bool) -> IndexMap<String, Array1<f64>> {
        let mut dummies = IndexMap::new();
        let start_idx = if drop_first { 1 } else { 0 };

        for (i, level) in self.levels.iter().enumerate().skip(start_idx) {
            let col_name = format!("{}_{}", prefix, level);
            let values: Vec<f64> = self
                .codes
                .iter()
                .map(|&code| if code == i as u32 { 1.0 } else { 0.0 })
                .collect();
            dummies.insert(col_name, Array1::from(values));
        }

        dummies
    }
}

/// Column enum supporting multiple data types
#[derive(Debug, Clone)]
pub enum Column {
    /// Numeric floating-point column
    Float(Array1<f64>),
    /// Categorical column with string levels
    Categorical(CategoricalColumn),
    /// Boolean column
    Bool(Array1<bool>),
    /// Integer column (signed 64-bit)
    Int(Array1<i64>),
    /// DateTime column (without timezone)
    DateTime(Array1<NaiveDateTime>),
    /// String column (free text, not categorical)
    String(Array1<String>),
}

impl Column {
    /// Get the data type of this column
    pub fn dtype(&self) -> DataType {
        match self {
            Column::Float(_) => DataType::Float,
            Column::Categorical(_) => DataType::Categorical,
            Column::Bool(_) => DataType::Bool,
            Column::Int(_) => DataType::Int,
            Column::DateTime(_) => DataType::DateTime,
            Column::String(_) => DataType::String,
        }
    }

    /// Get the number of elements in this column
    pub fn len(&self) -> usize {
        match self {
            Column::Float(arr) => arr.len(),
            Column::Categorical(cat) => cat.len(),
            Column::Bool(arr) => arr.len(),
            Column::Int(arr) => arr.len(),
            Column::DateTime(arr) => arr.len(),
            Column::String(arr) => arr.len(),
        }
    }

    /// Check if the column is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Try to get as float array
    pub fn as_float(&self) -> Option<&Array1<f64>> {
        match self {
            Column::Float(arr) => Some(arr),
            Column::Categorical(_) => None,
            Column::Bool(_) => None,
            Column::Int(_) => None,
            Column::DateTime(_) => None,
            Column::String(_) => None,
        }
    }

    /// Try to get as categorical
    pub fn as_categorical(&self) -> Option<&CategoricalColumn> {
        match self {
            Column::Float(_) => None,
            Column::Categorical(cat) => Some(cat),
            Column::Bool(_) => None,
            Column::Int(_) => None,
            Column::DateTime(_) => None,
            Column::String(_) => None,
        }
    }

    /// Try to get as bool array
    pub fn as_bool(&self) -> Option<&Array1<bool>> {
        match self {
            Column::Float(_) => None,
            Column::Categorical(_) => None,
            Column::Bool(arr) => Some(arr),
            Column::Int(_) => None,
            Column::DateTime(_) => None,
            Column::String(_) => None,
        }
    }

    /// Try to get as int array
    pub fn as_int(&self) -> Option<&Array1<i64>> {
        match self {
            Column::Float(_) => None,
            Column::Categorical(_) => None,
            Column::Bool(_) => None,
            Column::Int(arr) => Some(arr),
            Column::DateTime(_) => None,
            Column::String(_) => None,
        }
    }

    /// Try to get as datetime array
    pub fn as_datetime(&self) -> Option<&Array1<NaiveDateTime>> {
        match self {
            Column::Float(_) => None,
            Column::Categorical(_) => None,
            Column::Bool(_) => None,
            Column::Int(_) => None,
            Column::DateTime(arr) => Some(arr),
            Column::String(_) => None,
        }
    }

    /// Try to get as string array
    pub fn as_string(&self) -> Option<&Array1<String>> {
        match self {
            Column::Float(_) => None,
            Column::Categorical(_) => None,
            Column::Bool(_) => None,
            Column::Int(_) => None,
            Column::DateTime(_) => None,
            Column::String(arr) => Some(arr),
        }
    }

    /// Convert to float array (categorical -> codes as f64, bool -> 1.0/0.0, int -> f64, datetime -> timestamp, string -> NaN)
    pub fn to_float(&self) -> Array1<f64> {
        match self {
            Column::Float(arr) => arr.clone(),
            Column::Categorical(cat) => cat.to_float_codes(),
            Column::Bool(arr) => Array1::from(
                arr.iter()
                    .map(|&b| if b { 1.0 } else { 0.0 })
                    .collect::<Vec<_>>(),
            ),
            Column::Int(arr) => Array1::from(arr.iter().map(|&i| i as f64).collect::<Vec<_>>()),
            Column::DateTime(arr) => Array1::from(
                arr.iter()
                    .map(|dt| dt.and_utc().timestamp() as f64)
                    .collect::<Vec<_>>(),
            ),
            Column::String(arr) => Array1::from(vec![f64::NAN; arr.len()]),
        }
    }

    /// Filter by indices (for DataFrame operations)
    pub fn filter_indices(&self, indices: &[usize]) -> Self {
        match self {
            Column::Float(arr) => {
                let filtered: Vec<f64> = indices.iter().map(|&i| arr[i]).collect();
                Column::Float(Array1::from(filtered))
            }
            Column::Categorical(cat) => Column::Categorical(cat.filter_indices(indices)),
            Column::Bool(arr) => {
                let filtered: Vec<bool> = indices.iter().map(|&i| arr[i]).collect();
                Column::Bool(Array1::from(filtered))
            }
            Column::Int(arr) => {
                let filtered: Vec<i64> = indices.iter().map(|&i| arr[i]).collect();
                Column::Int(Array1::from(filtered))
            }
            Column::DateTime(arr) => {
                let filtered: Vec<NaiveDateTime> = indices.iter().map(|&i| arr[i]).collect();
                Column::DateTime(Array1::from(filtered))
            }
            Column::String(arr) => {
                let filtered: Vec<String> = indices.iter().map(|&i| arr[i].clone()).collect();
                Column::String(Array1::from(filtered))
            }
        }
    }

    /// Create from float array
    pub fn from_float(arr: Array1<f64>) -> Self {
        Column::Float(arr)
    }

    /// Create from string vector
    pub fn from_strings(values: Vec<String>) -> Self {
        Column::Categorical(CategoricalColumn::from_strings(values))
    }

    /// Create from bool array
    pub fn from_bool(arr: Array1<bool>) -> Self {
        Column::Bool(arr)
    }

    /// Create from int array
    pub fn from_int(arr: Array1<i64>) -> Self {
        Column::Int(arr)
    }

    /// Create from datetime array
    pub fn from_datetime(arr: Array1<NaiveDateTime>) -> Self {
        Column::DateTime(arr)
    }

    /// Create from string array (non-categorical free text)
    pub fn from_string_array(arr: Array1<String>) -> Self {
        Column::String(arr)
    }
}

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

    #[test]
    fn test_categorical_from_strings() {
        let values = vec![
            "SP".to_string(),
            "RJ".to_string(),
            "SP".to_string(),
            "MG".to_string(),
        ];
        let cat = CategoricalColumn::from_strings(values);

        assert_eq!(cat.len(), 4);
        assert_eq!(cat.n_levels(), 3);
        assert_eq!(cat.levels, vec!["SP", "RJ", "MG"]);
        assert_eq!(cat.codes, vec![0, 1, 0, 2]);
    }

    #[test]
    fn test_categorical_get_string() {
        let values = vec!["A".to_string(), "B".to_string(), "A".to_string()];
        let cat = CategoricalColumn::from_strings(values);

        assert_eq!(cat.get_string(0), Some("A"));
        assert_eq!(cat.get_string(1), Some("B"));
        assert_eq!(cat.get_string(2), Some("A"));
    }

    #[test]
    fn test_categorical_to_strings() {
        let values = vec!["X".to_string(), "Y".to_string(), "X".to_string()];
        let cat = CategoricalColumn::from_strings(values.clone());

        assert_eq!(cat.to_strings(), values);
    }

    #[test]
    fn test_categorical_to_float_codes() {
        let values = vec!["A".to_string(), "B".to_string(), "A".to_string()];
        let cat = CategoricalColumn::from_strings(values);
        let codes = cat.to_float_codes();

        assert_eq!(codes[0], 0.0);
        assert_eq!(codes[1], 1.0);
        assert_eq!(codes[2], 0.0);
    }

    #[test]
    fn test_categorical_value_counts() {
        let values = vec![
            "SP".to_string(),
            "RJ".to_string(),
            "SP".to_string(),
            "SP".to_string(),
            "MG".to_string(),
        ];
        let cat = CategoricalColumn::from_strings(values);
        let counts = cat.value_counts();

        assert_eq!(counts.get("SP"), Some(&3));
        assert_eq!(counts.get("RJ"), Some(&1));
        assert_eq!(counts.get("MG"), Some(&1));
    }

    #[test]
    fn test_categorical_get_dummies() {
        let values = vec![
            "A".to_string(),
            "B".to_string(),
            "A".to_string(),
            "C".to_string(),
        ];
        let cat = CategoricalColumn::from_strings(values);

        // Without dropping first
        let dummies = cat.get_dummies("cat", false);
        assert_eq!(dummies.len(), 3); // A, B, C

        // With dropping first
        let dummies_drop = cat.get_dummies("cat", true);
        assert_eq!(dummies_drop.len(), 2); // B, C (A dropped)
    }

    #[test]
    fn test_column_dtype() {
        let float_col = Column::Float(Array1::from(vec![1.0, 2.0, 3.0]));
        let cat_col = Column::from_strings(vec!["A".to_string(), "B".to_string()]);

        assert_eq!(float_col.dtype(), DataType::Float);
        assert_eq!(cat_col.dtype(), DataType::Categorical);
    }

    #[test]
    fn test_column_len() {
        let float_col = Column::Float(Array1::from(vec![1.0, 2.0, 3.0]));
        let cat_col = Column::from_strings(vec!["A".to_string(), "B".to_string()]);

        assert_eq!(float_col.len(), 3);
        assert_eq!(cat_col.len(), 2);
    }

    #[test]
    fn test_column_to_float() {
        let float_col = Column::Float(Array1::from(vec![1.0, 2.0, 3.0]));
        let cat_col = Column::from_strings(vec!["A".to_string(), "B".to_string(), "A".to_string()]);

        let float_arr = float_col.to_float();
        assert_eq!(float_arr[0], 1.0);

        let cat_arr = cat_col.to_float();
        assert_eq!(cat_arr[0], 0.0);
        assert_eq!(cat_arr[1], 1.0);
        assert_eq!(cat_arr[2], 0.0);
    }

    #[test]
    fn test_column_filter_indices() {
        let float_col = Column::Float(Array1::from(vec![1.0, 2.0, 3.0, 4.0]));
        let filtered = float_col.filter_indices(&[0, 2, 3]);

        if let Column::Float(arr) = filtered {
            assert_eq!(arr.len(), 3);
            assert_eq!(arr[0], 1.0);
            assert_eq!(arr[1], 3.0);
            assert_eq!(arr[2], 4.0);
        } else {
            panic!("Expected Float column");
        }
    }

    #[test]
    fn test_bool_column_creation() {
        let bool_col = Column::from_bool(Array1::from(vec![true, false, true, false]));

        assert_eq!(bool_col.dtype(), DataType::Bool);
        assert_eq!(bool_col.len(), 4);
        assert!(!bool_col.is_empty());
    }

    #[test]
    fn test_bool_column_as_bool() {
        let bool_col = Column::from_bool(Array1::from(vec![true, false, true]));

        let arr = bool_col.as_bool().unwrap();
        assert_eq!(arr[0], true);
        assert_eq!(arr[1], false);
        assert_eq!(arr[2], true);
    }

    #[test]
    fn test_bool_column_to_float() {
        let bool_col = Column::from_bool(Array1::from(vec![true, false, true, false]));

        let float_arr = bool_col.to_float();
        assert_eq!(float_arr[0], 1.0);
        assert_eq!(float_arr[1], 0.0);
        assert_eq!(float_arr[2], 1.0);
        assert_eq!(float_arr[3], 0.0);
    }

    #[test]
    fn test_bool_column_filter_indices() {
        let bool_col = Column::from_bool(Array1::from(vec![true, false, true, false, true]));
        let filtered = bool_col.filter_indices(&[0, 2, 4]);

        if let Column::Bool(arr) = filtered {
            assert_eq!(arr.len(), 3);
            assert_eq!(arr[0], true);
            assert_eq!(arr[1], true);
            assert_eq!(arr[2], true);
        } else {
            panic!("Expected Bool column");
        }
    }

    #[test]
    fn test_column_type_accessors() {
        let float_col = Column::Float(Array1::from(vec![1.0, 2.0]));
        let cat_col = Column::from_strings(vec!["A".to_string()]);
        let bool_col = Column::from_bool(Array1::from(vec![true, false]));
        let int_col = Column::from_int(Array1::from(vec![1, 2, 3]));
        let str_col = Column::from_string_array(Array1::from(vec!["Hello".to_string()]));

        // Float column
        assert!(float_col.as_float().is_some());
        assert!(float_col.as_categorical().is_none());
        assert!(float_col.as_bool().is_none());
        assert!(float_col.as_int().is_none());
        assert!(float_col.as_string().is_none());

        // Categorical column
        assert!(cat_col.as_float().is_none());
        assert!(cat_col.as_categorical().is_some());
        assert!(cat_col.as_bool().is_none());
        assert!(cat_col.as_int().is_none());
        assert!(cat_col.as_string().is_none());

        // Bool column
        assert!(bool_col.as_float().is_none());
        assert!(bool_col.as_categorical().is_none());
        assert!(bool_col.as_bool().is_some());
        assert!(bool_col.as_int().is_none());
        assert!(bool_col.as_string().is_none());

        // Int column
        assert!(int_col.as_float().is_none());
        assert!(int_col.as_categorical().is_none());
        assert!(int_col.as_bool().is_none());
        assert!(int_col.as_int().is_some());
        assert!(int_col.as_string().is_none());

        // String column
        assert!(str_col.as_float().is_none());
        assert!(str_col.as_categorical().is_none());
        assert!(str_col.as_bool().is_none());
        assert!(str_col.as_int().is_none());
        assert!(str_col.as_string().is_some());
    }

    #[test]
    fn test_int_column_creation() {
        let int_col = Column::from_int(Array1::from(vec![1, 2, 3, 4, 5]));

        assert_eq!(int_col.dtype(), DataType::Int);
        assert_eq!(int_col.len(), 5);
        assert!(!int_col.is_empty());
    }

    #[test]
    fn test_int_column_as_int() {
        let int_col = Column::from_int(Array1::from(vec![10, 20, 30]));

        let arr = int_col.as_int().unwrap();
        assert_eq!(arr[0], 10);
        assert_eq!(arr[1], 20);
        assert_eq!(arr[2], 30);
    }

    #[test]
    fn test_int_column_to_float() {
        let int_col = Column::from_int(Array1::from(vec![100, 200, 300, 400]));

        let float_arr = int_col.to_float();
        assert_eq!(float_arr[0], 100.0);
        assert_eq!(float_arr[1], 200.0);
        assert_eq!(float_arr[2], 300.0);
        assert_eq!(float_arr[3], 400.0);
    }

    #[test]
    fn test_int_column_filter_indices() {
        let int_col = Column::from_int(Array1::from(vec![1, 2, 3, 4, 5]));
        let filtered = int_col.filter_indices(&[0, 2, 4]);

        if let Column::Int(arr) = filtered {
            assert_eq!(arr.len(), 3);
            assert_eq!(arr[0], 1);
            assert_eq!(arr[1], 3);
            assert_eq!(arr[2], 5);
        } else {
            panic!("Expected Int column");
        }
    }

    #[test]
    fn test_int_column_negative_values() {
        let int_col = Column::from_int(Array1::from(vec![-10, -5, 0, 5, 10]));

        let arr = int_col.as_int().unwrap();
        assert_eq!(arr[0], -10);
        assert_eq!(arr[2], 0);
        assert_eq!(arr[4], 10);

        let float_arr = int_col.to_float();
        assert_eq!(float_arr[0], -10.0);
        assert_eq!(float_arr[4], 10.0);
    }

    #[test]
    fn test_datetime_column_creation() {
        use chrono::NaiveDate;

        let dates = vec![
            NaiveDate::from_ymd_opt(2024, 1, 1)
                .unwrap()
                .and_hms_opt(0, 0, 0)
                .unwrap(),
            NaiveDate::from_ymd_opt(2024, 1, 2)
                .unwrap()
                .and_hms_opt(12, 30, 45)
                .unwrap(),
            NaiveDate::from_ymd_opt(2024, 1, 3)
                .unwrap()
                .and_hms_opt(23, 59, 59)
                .unwrap(),
        ];

        let dt_col = Column::from_datetime(Array1::from(dates.clone()));
        assert_eq!(dt_col.dtype(), DataType::DateTime);
        assert_eq!(dt_col.len(), 3);

        let arr = dt_col.as_datetime().unwrap();
        assert_eq!(arr[0], dates[0]);
        assert_eq!(arr[1], dates[1]);
        assert_eq!(arr[2], dates[2]);
    }

    #[test]
    fn test_datetime_column_as_datetime() {
        use chrono::NaiveDate;

        let dt_col =
            Column::from_datetime(Array1::from(vec![NaiveDate::from_ymd_opt(2024, 6, 15)
                .unwrap()
                .and_hms_opt(10, 30, 0)
                .unwrap()]));

        if let Some(arr) = dt_col.as_datetime() {
            assert_eq!(arr.len(), 1);
            assert_eq!(
                arr[0].format("%Y-%m-%d %H:%M:%S").to_string(),
                "2024-06-15 10:30:00"
            );
        } else {
            panic!("Expected DateTime column");
        }
    }

    #[test]
    fn test_datetime_column_to_float() {
        use chrono::NaiveDate;

        let dt = NaiveDate::from_ymd_opt(2024, 1, 1)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap();
        let dt_col = Column::from_datetime(Array1::from(vec![dt]));

        let float_arr = dt_col.to_float();
        assert_eq!(float_arr.len(), 1);
        // Should convert to Unix timestamp
        assert_eq!(float_arr[0], dt.and_utc().timestamp() as f64);
    }

    #[test]
    fn test_datetime_column_filter_indices() {
        use chrono::NaiveDate;

        let dates = vec![
            NaiveDate::from_ymd_opt(2024, 1, 1)
                .unwrap()
                .and_hms_opt(0, 0, 0)
                .unwrap(),
            NaiveDate::from_ymd_opt(2024, 1, 2)
                .unwrap()
                .and_hms_opt(0, 0, 0)
                .unwrap(),
            NaiveDate::from_ymd_opt(2024, 1, 3)
                .unwrap()
                .and_hms_opt(0, 0, 0)
                .unwrap(),
            NaiveDate::from_ymd_opt(2024, 1, 4)
                .unwrap()
                .and_hms_opt(0, 0, 0)
                .unwrap(),
        ];

        let dt_col = Column::from_datetime(Array1::from(dates.clone()));
        let filtered = dt_col.filter_indices(&[0, 2, 3]);

        if let Some(arr) = filtered.as_datetime() {
            assert_eq!(arr.len(), 3);
            assert_eq!(arr[0], dates[0]);
            assert_eq!(arr[1], dates[2]);
            assert_eq!(arr[2], dates[3]);
        } else {
            panic!("Expected DateTime column");
        }
    }

    #[test]
    fn test_datetime_column_timestamp_conversion() {
        use chrono::NaiveDate;

        let dates = vec![
            NaiveDate::from_ymd_opt(2020, 1, 1)
                .unwrap()
                .and_hms_opt(0, 0, 0)
                .unwrap(),
            NaiveDate::from_ymd_opt(2021, 6, 15)
                .unwrap()
                .and_hms_opt(12, 0, 0)
                .unwrap(),
            NaiveDate::from_ymd_opt(2024, 12, 31)
                .unwrap()
                .and_hms_opt(23, 59, 59)
                .unwrap(),
        ];

        let dt_col = Column::from_datetime(Array1::from(dates.clone()));
        let float_arr = dt_col.to_float();

        // Verify each conversion
        for (i, dt) in dates.iter().enumerate() {
            assert_eq!(float_arr[i], dt.and_utc().timestamp() as f64);
        }
    }

    #[test]
    fn test_string_column_creation() {
        let str_col = Column::from_string_array(Array1::from(vec![
            "Alice".to_string(),
            "Bob".to_string(),
            "Charlie".to_string(),
        ]));

        assert_eq!(str_col.dtype(), DataType::String);
        assert_eq!(str_col.len(), 3);
        assert!(!str_col.is_empty());
    }

    #[test]
    fn test_string_column_as_string() {
        let str_col = Column::from_string_array(Array1::from(vec![
            "Hello".to_string(),
            "World".to_string(),
            "Test".to_string(),
        ]));

        let arr = str_col.as_string().unwrap();
        assert_eq!(arr[0], "Hello");
        assert_eq!(arr[1], "World");
        assert_eq!(arr[2], "Test");
    }

    #[test]
    fn test_string_column_to_float() {
        // String columns should convert to NaN when to_float() is called
        let str_col =
            Column::from_string_array(Array1::from(vec!["Alice".to_string(), "Bob".to_string()]));

        let float_arr = str_col.to_float();
        assert_eq!(float_arr.len(), 2);
        assert!(float_arr[0].is_nan());
        assert!(float_arr[1].is_nan());
    }

    #[test]
    fn test_string_column_filter_indices() {
        let str_col = Column::from_string_array(Array1::from(vec![
            "One".to_string(),
            "Two".to_string(),
            "Three".to_string(),
            "Four".to_string(),
            "Five".to_string(),
        ]));

        let filtered = str_col.filter_indices(&[0, 2, 4]);

        if let Column::String(arr) = filtered {
            assert_eq!(arr.len(), 3);
            assert_eq!(arr[0], "One");
            assert_eq!(arr[1], "Three");
            assert_eq!(arr[2], "Five");
        } else {
            panic!("Expected String column");
        }
    }

    #[test]
    fn test_string_column_empty_strings() {
        let str_col = Column::from_string_array(Array1::from(vec![
            "".to_string(),
            "Not empty".to_string(),
            "".to_string(),
        ]));

        let arr = str_col.as_string().unwrap();
        assert_eq!(arr.len(), 3);
        assert_eq!(arr[0], "");
        assert_eq!(arr[1], "Not empty");
        assert_eq!(arr[2], "");
    }

    #[test]
    fn test_string_column_long_text() {
        let long_text = "This is a very long string to test that String columns can handle variable-length text content without issues.".to_string();
        let str_col =
            Column::from_string_array(Array1::from(vec![long_text.clone(), "Short".to_string()]));

        let arr = str_col.as_string().unwrap();
        assert_eq!(arr[0], long_text);
        assert_eq!(arr[1], "Short");
    }

    #[test]
    fn test_string_column_special_characters() {
        let str_col = Column::from_string_array(Array1::from(vec![
            "Test with spaces".to_string(),
            "Test,with,commas".to_string(),
            "Test\"with\"quotes".to_string(),
            "Test\nwith\nnewlines".to_string(),
        ]));

        let arr = str_col.as_string().unwrap();
        assert_eq!(arr.len(), 4);
        assert!(arr[0].contains("spaces"));
        assert!(arr[1].contains("commas"));
        assert!(arr[2].contains("quotes"));
        assert!(arr[3].contains("\n"));
    }
}