rustframe 0.0.1-a.20250805

A simple dataframe and math toolkit
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
//! Core data-frame structures such as [`Frame`] and [`RowIndex`].
//!
//! The [`Frame`] type stores column-labelled data with an optional row index
//! and builds upon the [`crate::matrix::Matrix`] type.
//!
//! # Examples
//!
//! ```
//! use rustframe::frame::{Frame, RowIndex};
//! use rustframe::matrix::Matrix;
//!
//! let data = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
//! let frame = Frame::new(data, vec!["L", "R"], Some(RowIndex::Int(vec![10, 20])));
//! assert_eq!(frame.columns(), &["L", "R"]);
//! assert_eq!(frame.index(), &RowIndex::Int(vec![10, 20]));
//! ```
use crate::matrix::Matrix;
use chrono::NaiveDate;
use std::collections::HashMap;
use std::fmt;
use std::ops::{Index, IndexMut, Not, Range};

// Helper enums and structs for indexing support

/// Represents the different types of row indices a Frame can have.
#[derive(Debug, Clone, PartialEq, Eq)] // Derive Eq to enable usage as HashMap keys
pub enum RowIndex {
    /// Integer-based index (e.g., 0, 1, 2, ...). Values must be unique.
    Int(Vec<usize>),
    /// Date-based index. Values must be unique. Order is preserved as given.
    Date(Vec<NaiveDate>),
    /// Default range index (0..num_rows) used when no specific index is provided.
    Range(Range<usize>),
}

impl RowIndex {
    /// Returns the number of elements in the index.
    pub fn len(&self) -> usize {
        match self {
            RowIndex::Int(v) => v.len(),
            RowIndex::Date(v) => v.len(),
            RowIndex::Range(r) => r.end.saturating_sub(r.start),
        }
    }

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

/// Internal helper for fast lookups from index value to physical row position.
#[derive(Debug, Clone, PartialEq, Eq)]
enum RowIndexLookup {
    Int(HashMap<usize, usize>),
    Date(HashMap<NaiveDate, usize>),
    None, // Variant for default range-based indexing
}

// Frame struct definition and associated implementations

/// A data frame - a Matrix with string-identified columns and a typed row index.
///
/// `Frame` extends the concept of a `Matrix` by adding named columns
/// and an index for rows, which can be integers, dates, or a default range.
/// It allows accessing data by column name (using `[]`) and by row index value
/// (using `get_row`, `get_row_mut`, `get_row_date`, `get_row_date_mut` methods).
///
/// Direct row indexing with `frame[row_key]` is not supported due to Rust's
/// lifetime and ownership rules clashing with the `std::ops::Index` trait when
/// returning row views or temporary copies. Use the explicit `get_row*` methods instead.
///
/// The internal data is stored column-major in a `Matrix<T>`.
///
/// # Type Parameters
///
/// * `T`: The data type of the elements within the frame. Must be `Clone + PartialEq`.
///        Numerical/Boolean traits required for specific operations (e.g., `Add`, `BitAnd`).
///
/// # Examples
///
/// ```
/// use rustframe::frame::{Frame, RowIndex};
/// use rustframe::matrix::Matrix; // Assume Matrix is available
/// use chrono::NaiveDate;
///
/// // Helper fn for dates
/// fn d(y: i32, m: u32, d: u32) -> NaiveDate { NaiveDate::from_ymd_opt(y,m,d).unwrap() }
///
/// // --- Example 1: Basic Creation and Access ---
/// let matrix_f64 = Matrix::from_cols(vec![
///     vec![1.0, 2.0, 3.0], // Column "A"
///     vec![4.0, 5.0, 6.0], // Column "B"
/// ]);
/// let mut frame1 = Frame::new(matrix_f64, vec!["A", "B"], None);
///
/// assert_eq!(frame1.columns(), &["A", "B"]);
/// assert_eq!(frame1["A"], vec![1.0, 2.0, 3.0]); // Compare with slice literal
/// assert_eq!(frame1.index(), &RowIndex::Range(0..3));
/// let row0 = frame1.get_row(0);
/// assert_eq!(row0["A"], 1.0);
/// assert_eq!(row0[1], 4.0); // Column "B"
///
/// // --- Example 2: Date Index and Mutation ---
/// let dates = vec![d(2024, 1, 1), d(2024, 1, 2)];
/// let matrix_string = Matrix::from_cols(vec![ vec!["X".to_string(), "Y".to_string()], ]);
/// let mut frame2 = Frame::new(matrix_string, vec!["Label"], Some(RowIndex::Date(dates.clone())));
///
/// assert_eq!(frame2.index(), &RowIndex::Date(dates));
/// assert_eq!(frame2.get_row_date(d(2024, 1, 2))["Label"], "Y");
/// frame2.get_row_date_mut(d(2024, 1, 1)).set_by_index(0, "Z".to_string());
/// assert_eq!(frame2["Label"], vec!["Z", "Y"]);
///
/// // --- Example 3: Element-wise Addition ---
/// let m1 = Matrix::from_cols(vec![ vec![1.0, 2.0], vec![3.0, 4.0] ]);
/// let f1 = Frame::new(m1, vec!["C1", "C2"], None);
/// let m2 = Matrix::from_cols(vec![ vec![0.1, 0.2], vec![0.3, 0.4] ]);
/// let f2 = Frame::new(m2, vec!["C1", "C2"], None);
///
/// let f_sum = &f1 + &f2;
/// assert_eq!(f_sum["C1"], vec![1.1, 2.2]);
/// assert_eq!(f_sum["C2"], vec![3.3, 4.4]);
/// assert_eq!(f_sum.index(), &RowIndex::Range(0..2));
///
/// // --- Example 4: Element-wise Multiplication ---
/// let f_prod = &f1 * &f2;
/// assert!((f_prod["C1"][0] - 0.1_f64).abs() < 1e-9);
/// assert!((f_prod["C1"][1] - 0.4_f64).abs() < 1e-9);
/// assert!((f_prod["C2"][0] - 0.9_f64).abs() < 1e-9);
/// assert!((f_prod["C2"][1] - 1.6_f64).abs() < 1e-9);
///
/// // --- Example 5: Column Manipulation and Sorting ---
/// let mut frame_manip = Frame::new(
///     Matrix::from_cols(vec![ vec![1, 2], vec![3, 4] ]), // Example uses i32
///     vec!["DataC", "DataA"], // Column names (out of order)
///     None
/// );
/// assert_eq!(frame_manip["DataC"], vec![1, 2]);
/// assert_eq!(frame_manip["DataA"], vec![3, 4]);
/// frame_manip.add_column("DataB", vec![5, 6]);
/// assert_eq!(frame_manip.columns(), &["DataC", "DataA", "DataB"]);
/// frame_manip.rename("DataA", "DataX"); // Rename A -> X
/// assert_eq!(frame_manip.columns(), &["DataC", "DataX", "DataB"]);
/// assert_eq!(frame_manip["DataX"], vec![3, 4]); // X has A's original data
/// let deleted_c = frame_manip.delete_column("DataC");
/// assert_eq!(deleted_c, vec![1, 2]);
/// assert_eq!(frame_manip.columns(), &["DataX", "DataB"]);
/// frame_manip.sort_columns();
/// assert_eq!(frame_manip.columns(), &["DataB", "DataX"]);
/// assert_eq!(frame_manip["DataB"], vec![5, 6]);
/// assert_eq!(frame_manip["DataX"], vec![3, 4]);
/// ```

// Custom Debug implementation for Frame
impl<T: Clone + PartialEq + fmt::Debug> fmt::Debug for Frame<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Frame")
            .field("column_names", &self.column_names)
            .field("index", &self.index)
            .field("matrix_dims", &(self.matrix.rows(), self.matrix.cols()))
            .field("col_lookup", &self.col_lookup)
            .field("index_lookup", &self.index_lookup)
            // Matrix data is omitted from Debug output by default
            // .field("matrix", &self.matrix)
            .finish()
    }
}

#[derive(Clone, PartialEq)]
pub struct Frame<T: Clone + PartialEq> {
    /// Vector holding the column names in their current order.
    column_names: Vec<String>,
    /// The underlying column-major matrix storing the data.
    matrix: Matrix<T>,
    /// Maps a column name to its physical column index for **O(1)** lookup.
    col_lookup: HashMap<String, usize>,
    /// The row index values (Int, Date, or Range).
    index: RowIndex,
    /// Internal lookup for mapping index values to physical row positions.
    index_lookup: RowIndexLookup,
}

impl<T: Clone + PartialEq> Frame<T> {
    // Constructors

    /// Creates a new [`Frame`] from a matrix, column names, and an optional row index.
    /// Panics if the underlying `Matrix` requirements are not met (e.g., rows>0, cols>0 for standard constructors),
    /// or if column name/index constraints are violated.
    pub fn new<L: Into<String>>(matrix: Matrix<T>, names: Vec<L>, index: Option<RowIndex>) -> Self {
        // Validate that the number of column names matches the matrix's column count.
        if matrix.cols() != names.len() {
            panic!(
                "Frame::new: column name count mismatch (names: {}, matrix: {})",
                names.len(),
                matrix.cols()
            );
        }
        // Matrix creation already enforces non-zero rows and columns.

        // Build column name list and lookup map, ensuring no duplicates.
        let mut col_lookup = HashMap::with_capacity(names.len());
        let column_names: Vec<String> = names
            .into_iter()
            .enumerate()
            .map(|(i, n)| {
                let s = n.into();
                if col_lookup.insert(s.clone(), i).is_some() {
                    panic!("Frame::new: duplicate column label: {}", s);
                }
                s
            })
            .collect();

        // Validate and construct the row index and its lookup structure.
        let num_rows = matrix.rows();
        let (index_values, index_lookup) = match index {
            Some(RowIndex::Int(vals)) => {
                if vals.len() != num_rows {
                    panic!(
                        "Frame::new: Int index length ({}) mismatch matrix rows ({})",
                        vals.len(),
                        num_rows
                    );
                }
                // Build integer-to-physical-row mapping.
                let mut lookup = HashMap::with_capacity(num_rows);
                for (physical_row, index_val) in vals.iter().enumerate() {
                    if lookup.insert(*index_val, physical_row).is_some() {
                        panic!("Frame::new: duplicate Int index value: {}", index_val);
                    }
                }
                (RowIndex::Int(vals), RowIndexLookup::Int(lookup))
            }
            Some(RowIndex::Date(vals)) => {
                if vals.len() != num_rows {
                    panic!(
                        "Frame::new: Date index length ({}) mismatch matrix rows ({})",
                        vals.len(),
                        num_rows
                    );
                }
                // Build date-to-physical-row mapping.
                let mut lookup = HashMap::with_capacity(num_rows);
                for (physical_row, index_val) in vals.iter().enumerate() {
                    if lookup.insert(*index_val, physical_row).is_some() {
                        panic!("Frame::new: duplicate Date index value: {}", index_val);
                    }
                }
                (RowIndex::Date(vals), RowIndexLookup::Date(lookup))
            }
            Some(RowIndex::Range(ref r)) => {
                // If the length of the range does not match the number of rows, panic.
                if r.end.saturating_sub(r.start) != num_rows {
                    panic!(
                        "Frame::new: Range index length ({}) mismatch matrix rows ({})",
                        r.end.saturating_sub(r.start),
                        num_rows
                    );
                }
                // return the range as is.
                (RowIndex::Range(r.clone()), RowIndexLookup::None)
            }
            None => {
                // Default to a sequential range index.
                (RowIndex::Range(0..num_rows), RowIndexLookup::None)
            }
        };

        Self {
            matrix,
            column_names,
            col_lookup,
            index: index_values,
            index_lookup,
        }
    }

    // Accessors

    /// Returns an immutable reference to the underlying `Matrix`.
    #[inline]
    pub fn matrix(&self) -> &Matrix<T> {
        &self.matrix
    }

    /// Returns a mutable reference to the underlying `Matrix`.
    /// Use with caution: direct matrix edits bypass frame-level name/index consistency checks.
    #[inline]
    pub fn matrix_mut(&mut self) -> &mut Matrix<T> {
        &mut self.matrix
    }

    /// Returns the list of column names in their current order.
    #[inline]
    pub fn columns(&self) -> &[String] {
        &self.column_names
    }

    /// Returns a reference to the frame's row index.
    #[inline]
    pub fn index(&self) -> &RowIndex {
        &self.index
    }

    /// Returns the number of rows in the frame.
    #[inline]
    pub fn rows(&self) -> usize {
        self.matrix.rows()
    }

    /// Returns the number of columns in the frame.
    #[inline]
    pub fn cols(&self) -> usize {
        self.matrix.cols()
    }

    /// Returns the physical column index for the given column name, if present.
    #[inline]
    pub fn column_index(&self, name: &str) -> Option<usize> {
        self.col_lookup.get(name).copied()
    }

    /// Internal helper to compute the physical row index from a logical key.
    fn get_physical_row_index<Idx>(&self, index_key: Idx) -> usize
    where
        Self: RowIndexLookupHelper<Idx>, // Requires `RowIndexLookupHelper` for `Idx`
    {
        <Self as RowIndexLookupHelper<Idx>>::lookup_row_index(
            index_key,
            &self.index,
            &self.index_lookup,
        )
    }

    /// Returns an immutable slice of the specified column's data.
    /// Panics if the column name is not found.
    pub fn column(&self, name: &str) -> &[T] {
        let idx = self
            .column_index(name)
            .unwrap_or_else(|| panic!("Frame::column: unknown column label: '{}'", name));
        self.matrix.column(idx)
    }

    /// Returns a mutable slice of the specified column's data.
    /// Panics if the column name is not found.
    pub fn column_mut(&mut self, name: &str) -> &mut [T] {
        let idx = self
            .column_index(name)
            .unwrap_or_else(|| panic!("Frame::column_mut: unknown column label: '{}'", name));
        self.matrix.column_mut(idx)
    }

    // Row access methods

    /// Returns an immutable view of the row for the given integer key.
    /// Panics if the key is invalid or if the index type is `Date`.
    pub fn get_row(&self, index_key: usize) -> FrameRowView<'_, T> {
        let idx = self.get_physical_row_index(index_key);
        FrameRowView {
            frame: self,
            physical_row_idx: idx,
        }
    }

    /// Returns a mutable view of the row for the given integer key.
    /// Panics if the key is invalid or if the index type is `Date`.
    pub fn get_row_mut(&mut self, index_key: usize) -> FrameRowViewMut<'_, T> {
        let idx = self.get_physical_row_index(index_key);
        FrameRowViewMut {
            frame: self,
            physical_row_idx: idx,
        }
    }

    /// Returns an immutable view of the row for the given date key.
    /// Panics if the key is invalid or if the index type is not `Date`.
    pub fn get_row_date(&self, index_key: NaiveDate) -> FrameRowView<'_, T> {
        let idx = self.get_physical_row_index(index_key);
        FrameRowView {
            frame: self,
            physical_row_idx: idx,
        }
    }

    /// Returns a mutable view of the row for the given date key.
    /// Panics if the key is invalid or if the index type is not `Date`.
    pub fn get_row_date_mut(&mut self, index_key: NaiveDate) -> FrameRowViewMut<'_, T> {
        let idx = self.get_physical_row_index(index_key);
        FrameRowViewMut {
            frame: self,
            physical_row_idx: idx,
        }
    }

    // Column manipulation

    /// Internal helper to swap two columns, updating the matrix, name list, and lookup map.
    /// Not intended for public use; prefer `sort_columns`.
    fn _swap_columns_internal(&mut self, a: &str, b: &str) {
        // Retrieve physical indices for the given column names.
        let ia = self.column_index(a).unwrap_or_else(|| {
            panic!("Frame::_swap_columns_internal: unknown column label: {}", a)
        });
        let ib = self.column_index(b).unwrap_or_else(|| {
            panic!("Frame::_swap_columns_internal: unknown column label: {}", b)
        });

        if ia == ib {
            return; // No action needed if columns are identical
        }

        // Swap the columns in the underlying matrix.
        self.matrix.swap_columns(ia, ib);
        // Swap the names in the ordered list.
        self.column_names.swap(ia, ib);
        // Update lookup entries to reflect the new positions.
        self.col_lookup.insert(a.to_string(), ib);
        self.col_lookup.insert(b.to_string(), ia);
    }

    /// Renames an existing column.
    /// Panics if the source name is missing or if the target name is already in use.
    pub fn rename<L: Into<String>>(&mut self, old: &str, new: L) {
        let new_name = new.into();
        if old == new_name {
            panic!(
                "Frame::rename: new name '{}' cannot be the same as the old name",
                new_name
            );
        }
        let idx = self
            .column_index(old)
            .unwrap_or_else(|| panic!("Frame::rename: unknown column label: '{}'", old));
        if self.col_lookup.contains_key(&new_name) {
            panic!(
                "Frame::rename: new column name '{}' already exists",
                new_name
            );
        }

        // Update lookup and ordered name list.
        self.col_lookup.remove(old);
        self.col_lookup.insert(new_name.clone(), idx);
        self.column_names[idx] = new_name;
    }

    /// Adds a new column at the end of the frame.
    /// Panics if the column name exists or if the data length mismatches the row count.
    pub fn add_column<L: Into<String>>(&mut self, name: L, column_data: Vec<T>) {
        let name_str = name.into();
        if self.col_lookup.contains_key(&name_str) {
            panic!("Frame::add_column: duplicate column label: {}", name_str);
        }
        // Matrix enforces that the new column length matches the frame's row count.
        let new_col_idx = self.matrix.cols();
        self.matrix.add_column(new_col_idx, column_data);

        // Update metadata to include the new column.
        self.column_names.push(name_str.clone());
        self.col_lookup.insert(name_str, new_col_idx);
    }

    /// Deletes a column by name and returns its data.
    /// Panics if the column name is not found.
    pub fn delete_column(&mut self, name: &str) -> Vec<T> {
        let idx = self
            .column_index(name)
            .unwrap_or_else(|| panic!("Frame::delete_column: unknown column label: '{}'", name));

        // Clone out the data before removal.
        let deleted_data = self.matrix.column(idx).to_vec();
        self.matrix.delete_column(idx);

        // Remove from metadata and rebuild lookup for shifted columns.
        self.column_names.remove(idx);
        self.col_lookup.remove(name);
        self.rebuild_col_lookup();

        // If all columns are removed, reset default range index.
        if self.cols() == 0 {
            if let RowIndex::Range(_) = self.index {
                self.index = RowIndex::Range(0..self.rows());
                self.index_lookup = RowIndexLookup::None;
            }
        }

        deleted_data
    }

    /// Returns a new `Matrix` that is the transpose of the current frame's matrix.
    pub fn transpose(&self) -> Matrix<T> {
        self.matrix.transpose()
    }

    /// Sorts columns alphabetically by name, preserving data associations.
    pub fn sort_columns(&mut self) {
        let n = self.column_names.len();
        if n <= 1 {
            return; // Nothing to sort
        }

        // Selection sort on column names.
        for i in 0..n {
            let mut min_idx = i;
            for j in (i + 1)..n {
                if self.column_names[j] < self.column_names[min_idx] {
                    min_idx = j;
                }
            }
            if min_idx != i {
                let col_i_name = self.column_names[i].clone();
                let col_min_name = self.column_names[min_idx].clone();
                self._swap_columns_internal(&col_i_name, &col_min_name);
            }
        }

        // Debug-only consistency check.
        #[cfg(debug_assertions)]
        {
            let mut temp_lookup = HashMap::with_capacity(self.cols());
            for (idx, name) in self.column_names.iter().enumerate() {
                temp_lookup.insert(name.clone(), idx);
            }
            assert_eq!(
                self.col_lookup, temp_lookup,
                "Inconsistent col_lookup after sort_columns"
            );
        }
    }

    pub fn frame_map(&self, f: impl Fn(&T) -> T) -> Frame<T> {
        Frame::new(
            Matrix::from_vec(
                self.matrix.data().iter().map(f).collect(),
                self.matrix.rows(),
                self.matrix.cols(),
            ),
            self.column_names.clone(),
            Some(self.index.clone()),
        )
    }

    pub fn frame_zip(&self, other: &Frame<T>, f: impl Fn(&T, &T) -> T) -> Frame<T> {
        if self.rows() != other.rows() || self.cols() != other.cols() {
            panic!(
                "Frame::frame_zip: incompatible dimensions (self: {}x{}, other: {}x{})",
                self.rows(),
                self.cols(),
                other.rows(),
                other.cols()
            );
        }

        Frame::new(
            Matrix::from_vec(
                self.matrix
                    .data()
                    .iter()
                    .zip(other.matrix.data())
                    .map(|(a, b)| f(a, b))
                    .collect(),
                self.rows(),
                self.cols(),
            ),
            self.column_names.clone(),
            Some(self.index.clone()),
        )
    }

    // Internal helpers

    /// Rebuilds the column lookup map to match the current `column_names` ordering.
    fn rebuild_col_lookup(&mut self) {
        self.col_lookup.clear();
        for (i, name) in self.column_names.iter().enumerate() {
            self.col_lookup.insert(name.clone(), i);
        }
    }
}

// Trait for resolving logical to physical row indices
/// Internal trait to abstract the logic for looking up physical row indices.
trait RowIndexLookupHelper<Idx> {
    fn lookup_row_index(key: Idx, index_values: &RowIndex, index_lookup: &RowIndexLookup) -> usize;
}

impl<T: Clone + PartialEq> RowIndexLookupHelper<usize> for Frame<T> {
    fn lookup_row_index(
        key: usize,
        index_values: &RowIndex,
        index_lookup: &RowIndexLookup,
    ) -> usize {
        match (index_values, index_lookup) {
            (RowIndex::Int(_), RowIndexLookup::Int(lookup)) => {
                // Constant-time lookup using hash map
                *lookup.get(&key).unwrap_or_else(|| {
                    panic!("Frame index: integer key {} not found in Int index", key)
                })
            }
            (RowIndex::Range(range), RowIndexLookup::None) => {
                // Direct-range mapping with boundary check
                if range.contains(&key) {
                    // Since Range always starts at 0, the physical index equals the key
                    key
                } else {
                    panic!(
                        "Frame index: integer key {} out of bounds for Range index {:?}",
                        key, range
                    );
                }
            }
            (RowIndex::Date(_), _) => {
                panic!("Frame index: incompatible key type usize for Date index")
            }
            // Fallback panic on inconsistent internal index state
            #[allow(unreachable_patterns)]
            _ => {
                panic!(
                    "Frame index: inconsistent internal index state (lookup type mismatch for usize key)"
                )
            }
        }
    }
}

impl<T: Clone + PartialEq> RowIndexLookupHelper<NaiveDate> for Frame<T> {
    fn lookup_row_index(
        key: NaiveDate,
        index_values: &RowIndex,
        index_lookup: &RowIndexLookup,
    ) -> usize {
        match (index_values, index_lookup) {
            (RowIndex::Date(_), RowIndexLookup::Date(lookup)) => {
                // Constant-time lookup via hash map
                *lookup.get(&key).unwrap_or_else(|| {
                    panic!("Frame index: date key {} not found in Date index", key)
                })
            }
            (RowIndex::Int(_), _) | (RowIndex::Range(_), _) => {
                panic!("Frame index: incompatible key type NaiveDate for Int or Range index")
            }
            // Fallback panic on inconsistent internal index state
            #[allow(unreachable_patterns)]
            _ => {
                panic!(
                    "Frame index: inconsistent internal index state (lookup type mismatch for NaiveDate key)"
                )
            }
        }
    }
}

// Row view types for frame rows

/// An immutable view of a single row in a `Frame`. Allows access via `[]`.
pub struct FrameRowView<'a, T: Clone + PartialEq> {
    frame: &'a Frame<T>,
    physical_row_idx: usize,
}

impl<'a, T: Clone + PartialEq + fmt::Debug> fmt::Debug for FrameRowView<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Gather row data for debug output
        let row_data: Vec<&T> = (0..self.frame.cols())
            .map(|c| &self.frame.matrix[(self.physical_row_idx, c)])
            .collect();
        f.debug_struct("FrameRowView")
            .field("physical_row_idx", &self.physical_row_idx)
            .field("columns", &self.frame.column_names)
            .field("data", &row_data)
            .finish()
    }
}

impl<'a, T: Clone + PartialEq> FrameRowView<'a, T> {
    /// Returns a reference to the element at the given physical column index.
    /// Panics with a descriptive message if `col_idx` is out of bounds.
    pub fn get_by_index(&self, col_idx: usize) -> &T {
        if col_idx >= self.frame.cols() {
            panic!(
                "FrameRowView::get_by_index: column index {} out of bounds (frame has {} columns)",
                col_idx,
                self.frame.cols()
            );
        }
        &self.frame.matrix[(self.physical_row_idx, col_idx)]
    }

    /// Returns a reference to the element in the column named `col_name`.
    /// Panics if the column name is not found.
    pub fn get(&self, col_name: &str) -> &T {
        let idx = self
            .frame
            .column_index(col_name)
            .unwrap_or_else(|| panic!("FrameRowView::get: unknown column '{}'", col_name));
        self.get_by_index(idx)
    }
}

// Immutable indexing by column name
impl<'a, T: Clone + PartialEq> Index<&str> for FrameRowView<'a, T> {
    type Output = T;
    #[inline]
    fn index(&self, col_name: &str) -> &Self::Output {
        self.get(col_name)
    }
}

// Immutable indexing by physical column index
impl<'a, T: Clone + PartialEq> Index<usize> for FrameRowView<'a, T> {
    type Output = T;
    #[inline]
    fn index(&self, col_idx: usize) -> &Self::Output {
        self.get_by_index(col_idx)
    }
}

/// A mutable view of a single row in a `Frame`.  
/// Supports indexed access and mutation via methods or `[]` operators.
pub struct FrameRowViewMut<'a, T: Clone + PartialEq> {
    frame: &'a mut Frame<T>,
    physical_row_idx: usize,
}

impl<'a, T: Clone + PartialEq + fmt::Debug> fmt::Debug for FrameRowViewMut<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Only display the row index and column names to avoid conflicting borrows
        f.debug_struct("FrameRowViewMut")
            .field("physical_row_idx", &self.physical_row_idx)
            .field("columns", &self.frame.column_names)
            .finish()
    }
}

impl<'a, T: Clone + PartialEq> FrameRowViewMut<'a, T> {
    /// Returns a mutable reference to the element at the given physical column index.
    /// Panics if `col_idx` is out of bounds.
    pub fn get_by_index_mut(&mut self, col_idx: usize) -> &mut T {
        let num_cols = self.frame.cols();
        if col_idx >= num_cols {
            panic!(
                "FrameRowViewMut::get_by_index_mut: column index {} out of bounds (frame has {} columns)",
                col_idx, num_cols
            );
        }
        &mut self.frame.matrix[(self.physical_row_idx, col_idx)]
    }

    /// Returns a mutable reference to the element in the column named `col_name`.
    /// Panics if the column name is not found.
    pub fn get_mut(&mut self, col_name: &str) -> &mut T {
        let idx = self
            .frame
            .column_index(col_name)
            .unwrap_or_else(|| panic!("FrameRowViewMut::get_mut: unknown column '{}'", col_name));
        self.get_by_index_mut(idx)
    }

    /// Sets the element at the given physical column index to `value`.
    /// Panics if `col_idx` is out of bounds.
    pub fn set_by_index(&mut self, col_idx: usize, value: T) {
        *self.get_by_index_mut(col_idx) = value;
    }

    /// Sets the element in the column named `col_name` to `value`.
    /// Panics if the column name is not found.
    pub fn set(&mut self, col_name: &str, value: T) {
        *self.get_mut(col_name) = value;
    }

    // Read-only helpers for Index implementations

    /// Immutable reference to the element at the given physical column index.
    fn get_by_index_ref(&self, col_idx: usize) -> &T {
        if col_idx >= self.frame.cols() {
            panic!(
                "FrameRowViewMut::get_by_index_ref: column index {} out of bounds (frame has {} columns)",
                col_idx,
                self.frame.cols()
            );
        }
        &self.frame.matrix[(self.physical_row_idx, col_idx)]
    }

    /// Immutable reference to the element in the column named `col_name`.
    fn get_ref(&self, col_name: &str) -> &T {
        let idx = self
            .frame
            .column_index(col_name)
            .unwrap_or_else(|| panic!("FrameRowViewMut::get_ref: unknown column '{}'", col_name));
        self.get_by_index_ref(idx)
    }
}

// Immutable indexing by column name
impl<'a, T: Clone + PartialEq> Index<&str> for FrameRowViewMut<'a, T> {
    type Output = T;
    #[inline]
    fn index(&self, col_name: &str) -> &Self::Output {
        self.get_ref(col_name)
    }
}

// Immutable indexing by physical column index
impl<'a, T: Clone + PartialEq> Index<usize> for FrameRowViewMut<'a, T> {
    type Output = T;
    #[inline]
    fn index(&self, col_idx: usize) -> &Self::Output {
        self.get_by_index_ref(col_idx)
    }
}

// Mutable indexing by column name
impl<'a, T: Clone + PartialEq> IndexMut<&str> for FrameRowViewMut<'a, T> {
    #[inline]
    fn index_mut(&mut self, col_name: &str) -> &mut Self::Output {
        self.get_mut(col_name)
    }
}

// Mutable indexing by physical column index
impl<'a, T: Clone + PartialEq> IndexMut<usize> for FrameRowViewMut<'a, T> {
    #[inline]
    fn index_mut(&mut self, col_idx: usize) -> &mut Self::Output {
        self.get_by_index_mut(col_idx)
    }
}

/// Enables immutable access to a column's data via `frame["col_name"]`.
impl<T: Clone + PartialEq> Index<&str> for Frame<T> {
    type Output = [T];
    #[inline]
    fn index(&self, name: &str) -> &Self::Output {
        self.column(name)
    }
}

/// Enables mutable access to a column's data via `frame["col_name"]`.
impl<T: Clone + PartialEq> IndexMut<&str> for Frame<T> {
    #[inline]
    fn index_mut(&mut self, name: &str) -> &mut Self::Output {
        self.column_mut(name)
    }
}

/* ---------- Element-wise Arithmetic Operations ---------- */
/// Generates implementations for element-wise arithmetic (`+`, `-`, `*`, `/`) on `Frame<T>`.
/// Panics if column labels or row indices differ between operands.
macro_rules! impl_elementwise_frame_op {
    ($OpTrait:ident, $method:ident) => {
        // &Frame<T> $OpTrait &Frame<T>
        impl<'a, 'b, T> std::ops::$OpTrait<&'b Frame<T>> for &'a Frame<T>
        where
            T: Clone + PartialEq + std::ops::$OpTrait<Output = T>,
        {
            type Output = Frame<T>;
            fn $method(self, rhs: &'b Frame<T>) -> Frame<T> {
                if self.column_names != rhs.column_names {
                    panic!(
                        "Element-wise {}: column names do not match. Left: {:?}, Right: {:?}",
                        stringify!($method),
                        self.column_names,
                        rhs.column_names
                    );
                }
                if self.index != rhs.index {
                    panic!(
                        "Element-wise {}: row indices do not match. Left: {:?}, Right: {:?}",
                        stringify!($method),
                        self.index,
                        rhs.index
                    );
                }
                let result_matrix = (&self.matrix).$method(&rhs.matrix);
                let new_index = match self.index {
                    RowIndex::Range(_) => None,
                    _ => Some(self.index.clone()),
                };
                Frame::new(result_matrix, self.column_names.clone(), new_index)
            }
        }
        // Frame<T> $OpTrait &Frame<T>
        impl<'b, T> std::ops::$OpTrait<&'b Frame<T>> for Frame<T>
        where
            T: Clone + PartialEq + std::ops::$OpTrait<Output = T>,
        {
            type Output = Frame<T>;
            fn $method(self, rhs: &'b Frame<T>) -> Frame<T> {
                (&self).$method(rhs)
            }
        }
        // &Frame<T> $OpTrait Frame<T>
        impl<'a, T> std::ops::$OpTrait<Frame<T>> for &'a Frame<T>
        where
            T: Clone + PartialEq + std::ops::$OpTrait<Output = T>,
        {
            type Output = Frame<T>;
            fn $method(self, rhs: Frame<T>) -> Frame<T> {
                self.$method(&rhs)
            }
        }
        // Frame<T> $OpTrait Frame<T>
        impl<T> std::ops::$OpTrait<Frame<T>> for Frame<T>
        where
            T: Clone + PartialEq + std::ops::$OpTrait<Output = T>,
        {
            type Output = Frame<T>;
            fn $method(self, rhs: Frame<T>) -> Frame<T> {
                (&self).$method(&rhs)
            }
        }
    };
}

impl_elementwise_frame_op!(Add, add);
impl_elementwise_frame_op!(Sub, sub);
impl_elementwise_frame_op!(Mul, mul);
impl_elementwise_frame_op!(Div, div);

/* ---------- Boolean Bitwise Operations ---------- */
/// Generates implementations for element-wise bitwise operations (`&`, `|`, `^`) on `Frame<bool>`.
/// Panics if column labels or row indices differ between operands.
macro_rules! impl_bitwise_frame_op {
    ($OpTrait:ident, $method:ident) => {
        // &Frame<bool> $OpTrait &Frame<bool>
        impl<'a, 'b> std::ops::$OpTrait<&'b Frame<bool>> for &'a Frame<bool> {
            type Output = Frame<bool>;
            fn $method(self, rhs: &'b Frame<bool>) -> Frame<bool> {
                if self.column_names != rhs.column_names {
                    panic!(
                        "Bitwise {}: column names do not match. Left: {:?}, Right: {:?}",
                        stringify!($method),
                        self.column_names,
                        rhs.column_names
                    );
                }
                if self.index != rhs.index {
                    panic!(
                        "Bitwise {}: row indices do not match. Left: {:?}, Right: {:?}",
                        stringify!($method),
                        self.index,
                        rhs.index
                    );
                }
                let result_matrix = (&self.matrix).$method(&rhs.matrix);
                let new_index = match self.index {
                    RowIndex::Range(_) => None,
                    _ => Some(self.index.clone()),
                };
                Frame::new(result_matrix, self.column_names.clone(), new_index)
            }
        }
        // Frame<bool> $OpTrait &Frame<bool>
        impl<'b> std::ops::$OpTrait<&'b Frame<bool>> for Frame<bool> {
            type Output = Frame<bool>;
            fn $method(self, rhs: &'b Frame<bool>) -> Frame<bool> {
                (&self).$method(rhs)
            }
        }
        // &Frame<bool> $OpTrait Frame<bool>
        impl<'a> std::ops::$OpTrait<Frame<bool>> for &'a Frame<bool> {
            type Output = Frame<bool>;
            fn $method(self, rhs: Frame<bool>) -> Frame<bool> {
                self.$method(&rhs)
            }
        }
        // Frame<bool> $OpTrait Frame<bool>
        impl std::ops::$OpTrait<Frame<bool>> for Frame<bool> {
            type Output = Frame<bool>;
            fn $method(self, rhs: Frame<bool>) -> Frame<bool> {
                (&self).$method(&rhs)
            }
        }
    };
}

impl_bitwise_frame_op!(BitAnd, bitand);
impl_bitwise_frame_op!(BitOr, bitor);
impl_bitwise_frame_op!(BitXor, bitxor);

/* ---------- Logical NOT ---------- */
/// Implements logical NOT (`!`) for `Frame<bool>`, consuming the frame.
impl Not for Frame<bool> {
    type Output = Frame<bool>;

    fn not(self) -> Frame<bool> {
        // Apply NOT to the underlying matrix
        let result_matrix = !self.matrix;

        // Determine index for the result
        let new_index = match self.index {
            RowIndex::Range(_) => None,
            _ => Some(self.index),
        };

        Frame::new(result_matrix, self.column_names, new_index)
    }
}

/// Implements logical NOT (`!`) for `&Frame<bool>`, borrowing the frame.
impl Not for &Frame<bool> {
    type Output = Frame<bool>;

    fn not(self) -> Frame<bool> {
        // Apply NOT to the underlying matrix
        let result_matrix = !&self.matrix;

        // Determine index for the result
        let new_index = match self.index {
            RowIndex::Range(_) => None,
            _ => Some(self.index.clone()),
        };

        Frame::new(result_matrix, self.column_names.clone(), new_index)
    }
}

//  --- Tests ---
#[cfg(test)]
mod tests {
    use super::*;
    // Assume Matrix is available from crate::matrix or similar
    use crate::matrix::{BoolOps, Matrix};
    use chrono::NaiveDate;
    // HashMap needed for direct inspection in tests if required
    use std::collections::HashMap;
    // Use a fixed tolerance for float comparisons
    const FLOAT_TOLERANCE: f64 = 1e-9;

    // --- Test Helpers ---
    fn create_test_matrix_f64() -> Matrix<f64> {
        Matrix::from_cols(vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]]) // 3 rows, 2 cols
    }
    fn create_test_matrix_f64_alt() -> Matrix<f64> {
        Matrix::from_cols(vec![vec![0.1, 0.2, 0.3], vec![0.4, 0.5, 0.6]]) // 3 rows, 2 cols
    }
    fn create_test_matrix_bool() -> Matrix<bool> {
        Matrix::from_cols(vec![vec![true, false], vec![false, true]]) // 2 rows, 2 cols
    }
    fn create_test_matrix_bool_alt() -> Matrix<bool> {
        Matrix::from_cols(vec![vec![true, true], vec![false, false]]) // 2 rows, 2 cols
    }
    fn create_test_matrix_string() -> Matrix<String> {
        Matrix::from_cols(vec![
            vec!["r0c0".to_string(), "r1c0".to_string()], // Col 0
            vec!["r0c1".to_string(), "r1c1".to_string()], // Col 1
        ]) // 2 rows, 2 cols
    }
    fn d(y: i32, m: u32, d: u32) -> NaiveDate {
        NaiveDate::from_ymd_opt(y, m, d).unwrap()
    }
    fn create_test_frame_f64() -> Frame<f64> {
        Frame::new(create_test_matrix_f64(), vec!["A", "B"], None)
    }
    fn create_test_frame_f64_alt() -> Frame<f64> {
        Frame::new(create_test_matrix_f64_alt(), vec!["A", "B"], None)
    }
    fn create_test_frame_bool() -> Frame<bool> {
        Frame::new(create_test_matrix_bool(), vec!["P", "Q"], None)
    }
    fn create_test_frame_bool_alt() -> Frame<bool> {
        Frame::new(create_test_matrix_bool_alt(), vec!["P", "Q"], None)
    }
    fn create_test_frame_int() -> Frame<i32> {
        Frame::new(
            Matrix::from_cols(vec![vec![1, -2], vec![3, -4]]), // 2 rows, 2 cols
            vec!["X", "Y"],
            None,
        )
    }
    fn create_test_frame_int_alt() -> Frame<i32> {
        Frame::new(
            Matrix::from_cols(vec![vec![10, 20], vec![30, 40]]), // 2 rows, 2 cols
            vec!["X", "Y"],
            None,
        )
    }

    // --- Frame::new Tests ---
    #[test]
    fn frame_new_default_index() {
        let frame = create_test_frame_f64(); // 3 rows, 2 cols
        assert_eq!(frame.rows(), 3);
        assert_eq!(frame.cols(), 2);
        assert_eq!(frame.columns(), &["A", "B"]);
        assert_eq!(frame.index(), &RowIndex::Range(0..3));
        assert_eq!(frame.col_lookup.len(), 2);
        assert_eq!(frame.col_lookup["A"], 0);
        assert_eq!(frame.col_lookup["B"], 1);
        assert_eq!(frame.index_lookup, RowIndexLookup::None);
        assert_eq!(frame["A"], vec![1.0, 2.0, 3.0]);
        assert_eq!(frame["B"], vec![4.0, 5.0, 6.0]);
    }
    #[test]
    fn frame_new_int_index() {
        let matrix = create_test_matrix_f64(); // 3 rows
        let index_vec = vec![10, 20, 5];
        let index = RowIndex::Int(index_vec.clone());
        let frame = Frame::new(matrix, vec!["A", "B"], Some(index.clone()));
        assert_eq!(frame.index(), &index);
        assert!(matches!(frame.index_lookup, RowIndexLookup::Int(_)));
        if let RowIndexLookup::Int(lookup) = &frame.index_lookup {
            assert_eq!(lookup.len(), 3);
            assert_eq!(lookup[&10], 0); // value 10 -> physical row 0
            assert_eq!(lookup[&20], 1); // value 20 -> physical row 1
            assert_eq!(lookup[&5], 2); // value 5  -> physical row 2
        }
        assert_eq!(frame.get_row(10)["A"], 1.0); // Access by index value
        assert_eq!(frame.get_row(20)["A"], 2.0);
        assert_eq!(frame.get_row(5)["A"], 3.0);
    }
    #[test]
    fn frame_new_date_index() {
        let matrix = create_test_matrix_string(); // 2 rows
        let dates = vec![d(2024, 1, 10), d(2024, 1, 5)]; // Order preserved
        let index = RowIndex::Date(dates.clone());
        let frame = Frame::new(matrix, vec!["X", "Y"], Some(index.clone()));
        assert_eq!(frame.rows(), 2);
        assert_eq!(frame.cols(), 2);
        assert_eq!(frame.index(), &index);
        assert!(matches!(frame.index_lookup, RowIndexLookup::Date(_)));
        if let RowIndexLookup::Date(lookup) = &frame.index_lookup {
            assert_eq!(lookup.len(), 2);
            assert_eq!(lookup[&d(2024, 1, 10)], 0); // date -> physical row 0
            assert_eq!(lookup[&d(2024, 1, 5)], 1); // date -> physical row 1
        }
        assert_eq!(frame["X"], vec!["r0c0", "r1c0"]);
        assert_eq!(frame.get_row_date(d(2024, 1, 10))["X"], "r0c0");
        assert_eq!(frame.get_row_date(d(2024, 1, 5))["X"], "r1c0");
    }
    #[test]
    fn frame_new_one_by_one() {
        let matrix = Matrix::from_cols(vec![vec![100]]); // 1 row, 1 col
        let frame = Frame::new(matrix, vec!["Single"], None);
        assert_eq!(frame.rows(), 1);
        assert_eq!(frame.cols(), 1);
        assert_eq!(frame.columns(), &["Single"]);
        assert_eq!(frame.index(), &RowIndex::Range(0..1));
        assert_eq!(frame["Single"], vec![100]);
        assert_eq!(frame.get_row(0)[0], 100);
        assert_eq!(frame.get_row(0)["Single"], 100);
    }
    // Removed test frame_new_zero_rows_zero_cols as Matrix constructors prevent it

    // --- Frame::new Panic Tests ---
    #[test]
    #[should_panic(expected = "Frame::new: column name count mismatch (names: 1, matrix: 2)")]
    fn frame_new_panic_col_count() {
        let matrix = create_test_matrix_f64();
        Frame::new(matrix, vec!["A"], None);
    }
    #[test]
    #[should_panic(expected = "duplicate column label: A")]
    fn frame_new_panic_duplicate_col() {
        let matrix = create_test_matrix_f64();
        Frame::new(matrix, vec!["A", "A"], None);
    }
    #[test]
    #[should_panic(expected = "Int index length (2) mismatch matrix rows (3)")]
    fn frame_new_panic_index_len() {
        let matrix = create_test_matrix_f64(); // 3 rows
        let index = RowIndex::Int(vec![10, 20]); // Only 2 index values
        Frame::new(matrix, vec!["A", "B"], Some(index));
    }
    #[test]
    #[should_panic(expected = "Date index length (1) mismatch matrix rows (2)")]
    fn frame_new_panic_date_index_len() {
        let matrix = create_test_matrix_string(); // 2 rows
        let index = RowIndex::Date(vec![d(2024, 1, 1)]); // Only 1 index value
        Frame::new(matrix, vec!["X", "Y"], Some(index));
    }
    #[test]
    #[should_panic(expected = "duplicate Int index value: 10")]
    fn frame_new_panic_duplicate_int_index() {
        let matrix = create_test_matrix_f64(); // 3 rows
        let index = RowIndex::Int(vec![10, 20, 10]); // Duplicate 10
        Frame::new(matrix, vec!["A", "B"], Some(index));
    }
    #[test]
    #[should_panic(expected = "duplicate Date index value: 2024-01-10")]
    fn frame_new_panic_duplicate_date_index() {
        let matrix = create_test_matrix_string(); // 2 rows
        let index = RowIndex::Date(vec![d(2024, 1, 10), d(2024, 1, 10)]); // Duplicate date
        Frame::new(matrix, vec!["X", "Y"], Some(index));
    }
    #[test]
    #[should_panic(expected = "Frame::new: Range index length (4) mismatch matrix rows (3)")]
    fn frame_new_panic_invalid_explicit_range_index() {
        let matrix = create_test_matrix_f64(); // 3 rows
        let index = RowIndex::Range(0..4); // Range 0..4 but only 3 rows
        Frame::new(matrix, vec!["A", "B"], Some(index));
    }

    // --- RowIndex Method Tests ---
    #[test]
    fn test_row_index_methods() {
        let idx_int = RowIndex::Int(vec![10, 20, 5]);
        assert_eq!(idx_int.len(), 3);
        assert!(!idx_int.is_empty());
        let idx_date = RowIndex::Date(vec![d(2024, 1, 1), d(2024, 1, 2)]);
        assert_eq!(idx_date.len(), 2);
        assert!(!idx_date.is_empty());
        let idx_range = RowIndex::Range(0..5);
        assert_eq!(idx_range.len(), 5);
        assert!(!idx_range.is_empty());
        let idx_empty_int = RowIndex::Int(vec![]);
        assert_eq!(idx_empty_int.len(), 0);
        assert!(idx_empty_int.is_empty());
        let idx_empty_date = RowIndex::Date(vec![]);
        assert_eq!(idx_empty_date.len(), 0);
        assert!(idx_empty_date.is_empty());
        let idx_empty_range = RowIndex::Range(3..3);
        assert_eq!(idx_empty_range.len(), 0);
        assert!(idx_empty_range.is_empty());
        let idx_range_zero = RowIndex::Range(0..0);
        assert_eq!(idx_range_zero.len(), 0);
        assert!(idx_range_zero.is_empty());
    }

    // --- Frame Accessor Tests ---
    #[test]
    fn frame_column_access() {
        let mut frame = create_test_frame_f64(); // A=[1,2,3], B=[4,5,6]
        assert_eq!(frame.column("A"), &[1.0, 2.0, 3.0]);
        assert_eq!(frame["B"], vec![4.0, 5.0, 6.0]); // Index trait
        assert_eq!(frame.column_index("A"), Some(0));
        assert_eq!(frame.column_index("B"), Some(1));
        assert_eq!(frame.column_index("C"), None);

        // Mutation
        frame.column_mut("A")[1] = 2.5;
        assert_eq!(frame["A"], vec![1.0, 2.5, 3.0]);
        frame["B"][0] = 4.1; // IndexMut trait
        assert_eq!(frame["B"], vec![4.1, 5.0, 6.0]);
    }
    #[test]
    #[should_panic(expected = "unknown column label: 'C'")]
    fn frame_column_access_panic() {
        let frame = create_test_frame_f64();
        let _ = frame.column("C");
    }
    #[test]
    #[should_panic(expected = "unknown column label: 'C'")]
    fn frame_column_access_mut_panic() {
        let mut frame = create_test_frame_f64();
        let _ = frame.column_mut("C");
    }
    #[test]
    #[should_panic(expected = "unknown column label: 'C'")]
    fn frame_column_index_panic() {
        let frame = create_test_frame_f64();
        let _ = frame["C"]; // Panics when Index calls column internally
    }

    #[test]
    #[should_panic(expected = "unknown column label: 'C'")]
    fn frame_column_index_mut_panic() {
        let mut frame = create_test_frame_f64();
        frame["C"][0] = 0.0; // Panics when IndexMut calls column_mut internally
    }

    #[test]
    fn frame_row_access_default_index() {
        let frame = create_test_frame_f64(); // Index 0..3
        let row1 = frame.get_row(1); // Get row for index value 1 (physical row 1)
        assert_eq!(row1.get("A"), &2.0);
        assert_eq!(row1.get_by_index(1), &5.0); // Access by physical column index
        assert_eq!(row1["A"], 2.0); // Index by name
        assert_eq!(row1[1], 5.0); // Index by physical column index
        assert_eq!(frame.get_row(0)["B"], 4.0); // Index value 0 -> physical row 0
    }
    #[test]
    fn frame_row_access_int_index() {
        let matrix = create_test_matrix_f64(); // 3 rows
        let index = RowIndex::Int(vec![100, 50, 200]);
        let frame = Frame::new(matrix, vec!["A", "B"], Some(index));
        let row50 = frame.get_row(50); // Access by index value 50 (physical row 1)
        assert_eq!(row50["A"], 2.0);
        assert_eq!(row50[1], 5.0); // Column B (physical index 1)
        assert_eq!(frame.get_row(200)["A"], 3.0); // Index value 200 -> physical row 2
    }
    #[test]
    fn frame_row_access_date_index() {
        let matrix = create_test_matrix_string(); // 2 rows
        let index = RowIndex::Date(vec![d(2023, 5, 1), d(2023, 5, 10)]);
        let frame = Frame::new(matrix, vec!["X", "Y"], Some(index));
        let row_may10 = frame.get_row_date(d(2023, 5, 10)); // Access by date (physical row 1)
        assert_eq!(row_may10["X"], "r1c0");
        assert_eq!(row_may10[1], "r1c1"); // Column Y (physical index 1)
        assert_eq!(frame.get_row_date(d(2023, 5, 1))["Y"], "r0c1"); // Date -> physical row 0
    }

    #[test]
    #[should_panic(expected = "integer key 99 not found in Int index")]
    fn frame_row_access_int_index_panic_not_found() {
        let matrix = create_test_matrix_f64();
        let index = RowIndex::Int(vec![100, 50, 200]);
        let frame = Frame::new(matrix, vec!["A", "B"], Some(index));
        frame.get_row(99); // 99 is not in the index values
    }
    #[test]
    #[should_panic(expected = "integer key 99 not found in Int index")]
    fn frame_row_access_int_index_mut_panic_not_found() {
        let matrix = create_test_matrix_f64();
        let index = RowIndex::Int(vec![100, 50, 200]);
        let mut frame = Frame::new(matrix, vec!["A", "B"], Some(index));
        frame.get_row_mut(99); // 99 is not in the index values
    }
    #[test]
    #[should_panic(expected = "integer key 3 out of bounds for Range index 0..3")]
    fn frame_row_access_default_index_panic_out_of_bounds() {
        let frame = create_test_frame_f64(); // Index 0..3
        frame.get_row(3); // 3 is not in the range [0, 3)
    }
    #[test]
    #[should_panic(expected = "integer key 3 out of bounds for Range index 0..3")]
    fn frame_row_access_default_index_mut_panic_out_of_bounds() {
        let mut frame = create_test_frame_f64(); // Index 0..3
        frame.get_row_mut(3); // 3 is not in the range [0, 3)
    }
    #[test]
    #[should_panic(expected = "date key 2023-05-02 not found in Date index")]
    fn frame_row_access_date_index_panic_not_found() {
        let matrix = create_test_matrix_string();
        let index = RowIndex::Date(vec![d(2023, 5, 1), d(2023, 5, 10)]);
        let frame = Frame::new(matrix, vec!["X", "Y"], Some(index));
        frame.get_row_date(d(2023, 5, 2)); // Date not in index
    }
    #[test]
    #[should_panic(expected = "date key 2023-05-02 not found in Date index")]
    fn frame_row_access_date_index_mut_panic_not_found() {
        let matrix = create_test_matrix_string();
        let index = RowIndex::Date(vec![d(2023, 5, 1), d(2023, 5, 10)]);
        let mut frame = Frame::new(matrix, vec!["X", "Y"], Some(index));
        frame.get_row_date_mut(d(2023, 5, 2)); // Date not in index
    }
    #[test]
    #[should_panic(expected = "incompatible key type usize for Date index")]
    fn frame_row_access_type_mismatch_panic_usize_on_date() {
        let matrix = create_test_matrix_string();
        let index = RowIndex::Date(vec![d(2023, 5, 1), d(2023, 5, 10)]);
        let frame = Frame::new(matrix, vec!["X", "Y"], Some(index));
        frame.get_row(0); // Using usize key with Date index
    }
    #[test]
    #[should_panic(expected = "incompatible key type usize for Date index")]
    fn frame_row_access_type_mismatch_mut_panic_usize_on_date() {
        let matrix = create_test_matrix_string();
        let index = RowIndex::Date(vec![d(2023, 5, 1), d(2023, 5, 10)]);
        let mut frame = Frame::new(matrix, vec!["X", "Y"], Some(index));
        frame.get_row_mut(0); // Using usize key with Date index
    }
    #[test]
    #[should_panic(expected = "incompatible key type NaiveDate for Int or Range index")]
    fn frame_row_access_type_mismatch_panic_date_on_int() {
        let matrix = create_test_matrix_f64();
        let index = RowIndex::Int(vec![100, 50, 200]);
        let frame = Frame::new(matrix, vec!["A", "B"], Some(index));
        frame.get_row_date(d(2023, 5, 1)); // Using Date key with Int index
    }
    #[test]
    #[should_panic(expected = "incompatible key type NaiveDate for Int or Range index")]
    fn frame_row_access_type_mismatch_mut_panic_date_on_int() {
        let matrix = create_test_matrix_f64();
        let index = RowIndex::Int(vec![100, 50, 200]);
        let mut frame = Frame::new(matrix, vec!["A", "B"], Some(index));
        frame.get_row_date_mut(d(2023, 5, 1)); // Using Date key with Int index
    }
    #[test]
    #[should_panic(expected = "incompatible key type NaiveDate for Int or Range index")]
    fn frame_row_access_type_mismatch_panic_date_on_range() {
        let frame = create_test_frame_f64(); // Range index
        frame.get_row_date(d(2023, 5, 1)); // Using Date key with Range index
    }
    #[test]
    #[should_panic(expected = "incompatible key type NaiveDate for Int or Range index")]
    fn frame_row_access_type_mismatch_mut_panic_date_on_range() {
        let mut frame = create_test_frame_f64(); // Range index
        frame.get_row_date_mut(d(2023, 5, 1)); // Using Date key with Range index
    }
    #[test]
    #[should_panic(expected = "inconsistent internal index state")]
    fn frame_row_access_inconsistent_state_int_none() {
        // Manually create inconsistent state (Int index, None lookup)
        let frame = Frame::<i32> {
            matrix: Matrix::from_cols(vec![vec![1]]),
            column_names: vec!["A".to_string()],
            col_lookup: HashMap::from([("A".to_string(), 0)]),
            index: RowIndex::Int(vec![10]),
            index_lookup: RowIndexLookup::None, // Inconsistent
        };
        frame.get_row(10); // Should panic due to inconsistency
    }
    #[test]
    #[should_panic(expected = "inconsistent internal index state")]
    fn frame_row_access_inconsistent_state_date_none() {
        // Manually create inconsistent state (Date index, None lookup)
        let frame = Frame::<i32> {
            matrix: Matrix::from_cols(vec![vec![1]]),
            column_names: vec!["A".to_string()],
            col_lookup: HashMap::from([("A".to_string(), 0)]),
            index: RowIndex::Date(vec![d(2024, 1, 1)]),
            index_lookup: RowIndexLookup::None, // Inconsistent
        };
        frame.get_row_date(d(2024, 1, 1)); // Should panic due to inconsistency
    }
    #[test]
    #[should_panic(expected = "inconsistent internal index state")]
    fn frame_row_access_inconsistent_state_range_int() {
        // Manually create inconsistent state (Range index, Int lookup)
        let frame = Frame::<i32> {
            matrix: Matrix::from_cols(vec![vec![1]]),
            column_names: vec!["A".to_string()],
            col_lookup: HashMap::from([("A".to_string(), 0)]),
            index: RowIndex::Range(0..1),
            index_lookup: RowIndexLookup::Int(HashMap::new()), // Inconsistent
        };
        frame.get_row(0); // Should panic due to inconsistency
    }

    // --- Frame Row Mutation Tests ---
    #[test]
    fn frame_row_mutate_default_index() {
        let mut frame = create_test_frame_f64(); // Index 0..3, A=[1,2,3], B=[4,5,6]
                                                 // Mutate using set("col_name", value)
        frame.get_row_mut(1).set("A", 2.9); // Mutate row index 1, col A
        assert_eq!(frame["A"], vec![1.0, 2.9, 3.0]);
        // Mutate using IndexMut by physical column index
        frame.get_row_mut(0)[1] = 4.9; // Mutate row index 0, col B (index 1)
        assert_eq!(frame["B"], vec![4.9, 5.0, 6.0]);
        // Mutate using IndexMut by column name
        frame.get_row_mut(2)["A"] = 3.9; // Mutate row index 2, col A
        assert_eq!(frame["A"], vec![1.0, 2.9, 3.9]);
    }
    #[test]
    fn frame_row_mutate_date_index() {
        let matrix = create_test_matrix_string(); // r0=["r0c0","r0c1"], r1=["r1c0","r1c1"]
        let index = RowIndex::Date(vec![d(2023, 5, 1), d(2023, 5, 10)]); // r0=May1, r1=May10
        let mut frame = Frame::new(matrix, vec!["X", "Y"], Some(index));
        let key_may10 = d(2023, 5, 10);
        let key_may1 = d(2023, 5, 1);

        // Mutate using set_by_index(col_idx, value)
        frame
            .get_row_date_mut(key_may10) // Get row for May 10 (physical row 1)
            .set_by_index(0, "r1c0_mod".to_string()); // Set col X (index 0)
        assert_eq!(frame["X"], vec!["r0c0", "r1c0_mod"]);

        // Mutate using IndexMut by column name
        frame.get_row_date_mut(key_may1)["Y"] = "r0c1_mod".to_string(); // Row May 1, col Y
        assert_eq!(frame["Y"], vec!["r0c1_mod", "r1c1"]);

        // Mutate using IndexMut by physical column index
        frame.get_row_date_mut(key_may10)[1] = "r1c1_mod2".to_string(); // Row May 10, col Y (index 1)
        assert_eq!(frame["Y"], vec!["r0c1_mod", "r1c1_mod2"]);
    }

    // --- FrameRowView / FrameRowViewMut Indexing Tests ---
    #[test]
    fn test_row_view_mut_readonly_index() {
        // Test that read-only indexing still works on a mutable view
        let mut frame = create_test_frame_f64(); // A=[1,2,3], B=[4,5,6]
        let row_mut = frame.get_row_mut(1); // Get mutable view of row index 1
        assert_eq!(row_mut["A"], 2.0); // Read via Index<&str>
        assert_eq!(row_mut[1], 5.0); // Read via Index<usize> (col B)
    }
    #[test]
    #[should_panic(expected = "column index 2 out of bounds")] // Expect more specific message now
    fn test_row_view_index_panic() {
        let frame = create_test_frame_f64(); // 2 cols (0, 1)
        let row_view = frame.get_row(0);
        let _ = row_view[2]; // Access column index 2 (out of bounds)
    }
    #[test]
    #[should_panic(expected = "unknown column 'C'")]
    fn test_row_view_name_panic() {
        let frame = create_test_frame_f64();
        let row_view = frame.get_row(0);
        let _ = row_view["C"]; // Access non-existent column Z
    }
    #[test]
    #[should_panic(expected = "column index 3 out of bounds")] // Check specific message
    fn test_row_view_get_by_index_panic() {
        let frame = create_test_frame_f64(); // 2 cols (0, 1)
        let row_view = frame.get_row(0);
        let _ = row_view.get_by_index(3);
    }
    #[test]
    #[should_panic(expected = "column index 2 out of bounds")] // Expect more specific message now
    fn test_row_view_mut_index_panic() {
        let mut frame = create_test_frame_f64(); // 2 cols (0, 1)
        let mut row_view_mut = frame.get_row_mut(0);
        row_view_mut[2] = 0.0; // Access column index 2 (out of bounds)
    }
    #[test]
    #[should_panic(expected = "unknown column 'C'")]
    fn test_row_view_mut_name_panic() {
        let mut frame = create_test_frame_f64();
        let mut row_view_mut = frame.get_row_mut(0);
        row_view_mut["C"] = 0.0; // Access non-existent column name
    }
    #[test]
    #[should_panic(expected = "column index 3 out of bounds")] // Check specific message
    fn test_row_view_mut_get_by_index_mut_panic() {
        let mut frame = create_test_frame_f64(); // 2 cols (0, 1)
        let mut row_view_mut = frame.get_row_mut(0);
        let _ = row_view_mut.get_by_index_mut(3);
    }
    #[test]
    #[should_panic(expected = "column index 3 out of bounds")] // Check specific message
    fn test_row_view_mut_set_by_index_panic() {
        let mut frame = create_test_frame_f64(); // 2 cols (0, 1)
        let mut row_view_mut = frame.get_row_mut(0);
        row_view_mut.set_by_index(3, 0.0);
    }
    #[test]
    #[should_panic(expected = "unknown column 'C'")] // Panic from view set -> get_mut
    fn test_row_view_mut_set_panic() {
        let mut frame = create_test_frame_f64();
        let mut row_view_mut = frame.get_row_mut(0);
        row_view_mut.set("C", 0.0); // Access non-existent column name
    }

    // --- Frame Column Manipulation & Sorting Tests ---
    #[test]
    fn frame_column_manipulation_and_sort() {
        // Initial: C=[1,2], A=[3,4] (names out of alphabetical order)
        let mut frame = Frame::new(
            Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]), // 2 rows, 2 cols
            vec!["C", "A"],
            None,
        );
        assert_eq!(frame.columns(), &["C", "A"]);
        assert_eq!(frame["C"], vec![1, 2]);
        assert_eq!(frame["A"], vec![3, 4]);
        assert_eq!(frame.column_index("C"), Some(0));
        assert_eq!(frame.column_index("A"), Some(1));
        assert_eq!(frame.col_lookup.len(), 2);

        // Add B=[5,6]: C=[1,2], A=[3,4], B=[5,6]
        frame.add_column("B", vec![5, 6]);
        assert_eq!(frame.columns(), &["C", "A", "B"]);
        assert_eq!(frame["B"], vec![5, 6]);
        assert_eq!(frame.column_index("C"), Some(0));
        assert_eq!(frame.column_index("A"), Some(1));
        assert_eq!(frame.column_index("B"), Some(2)); // B added at the end
        assert_eq!(frame.col_lookup.len(), 3);

        // Rename C -> X: X=[1,2], A=[3,4], B=[5,6]
        frame.rename("C", "X");
        assert_eq!(frame.columns(), &["X", "A", "B"]); // Name 'C' at index 0 replaced by 'X'
        assert_eq!(frame["X"], vec![1, 2]); // Data remains
        assert_eq!(frame.column_index("X"), Some(0));
        assert_eq!(frame.column_index("A"), Some(1));
        assert_eq!(frame.column_index("B"), Some(2));
        assert!(frame.column_index("C").is_none()); // Old name gone
        assert_eq!(frame.col_lookup.len(), 3);

        // Delete A: X=[1,2], B=[5,6]
        let deleted_a = frame.delete_column("A");
        assert_eq!(deleted_a, vec![3, 4]);
        // Deleting col A (at physical index 1) shifts B left
        assert_eq!(frame.columns(), &["X", "B"]); // Remaining columns in physical order
        assert_eq!(frame.rows(), 2);
        assert_eq!(frame.cols(), 2);
        assert_eq!(frame["X"], vec![1, 2]); // X data unchanged
        assert_eq!(frame["B"], vec![5, 6]); // B data unchanged
                                            // Check internal state after delete + rebuild_col_lookup
        assert_eq!(frame.column_index("X"), Some(0)); // X is now physical col 0
        assert_eq!(frame.column_index("B"), Some(1)); // B is now physical col 1
        assert!(frame.column_index("A").is_none());
        assert_eq!(frame.col_lookup.len(), 2);

        // Sort Columns [X, B] -> [B, X]
        frame.sort_columns();
        assert_eq!(frame.columns(), &["B", "X"]); // Alphabetical order of names
                                                  // Verify data remained with the correct logical column after sort
        assert_eq!(frame["B"], vec![5, 6], "Data in B after sort"); // B should still have [5, 6]
        assert_eq!(frame["X"], vec![1, 2], "Data in X after sort"); // X should still have [1, 2]
                                                                    // Verify internal lookup map is correct after sort
        assert_eq!(frame.column_index("B"), Some(0), "Index of B after sort"); // B is now physical col 0
        assert_eq!(frame.column_index("X"), Some(1), "Index of X after sort"); // X is now physical col 1
        assert_eq!(frame.col_lookup.len(), 2);
        assert_eq!(*frame.col_lookup.get("B").unwrap(), 0);
        assert_eq!(*frame.col_lookup.get("X").unwrap(), 1);
    }

    // Tests specific to the old public swap_columns API are removed as it's now internal.
    // Test internal swap via sort_columns edge cases.
    #[test]
    fn test_sort_columns_already_sorted() {
        let mut frame = create_test_frame_f64(); // A, B (already sorted)
        let original_frame = frame.clone();
        frame.sort_columns();
        assert_eq!(frame.columns(), &["A", "B"]);
        assert_eq!(frame["A"], original_frame["A"]);
        assert_eq!(frame["B"], original_frame["B"]);
        assert_eq!(frame.col_lookup, original_frame.col_lookup);
    }
    #[test]
    fn test_sort_columns_reverse_sorted() {
        let mut frame = Frame::new(
            Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]),
            vec!["Z", "A"], // Z, A (reverse sorted)
            None,
        );
        frame.sort_columns();
        assert_eq!(frame.columns(), &["A", "Z"]);
        assert_eq!(frame["A"], vec![3, 4]); // A keeps its original data
        assert_eq!(frame["Z"], vec![1, 2]); // Z keeps its original data
        assert_eq!(frame.column_index("A"), Some(0));
        assert_eq!(frame.column_index("Z"), Some(1));
    }

    #[test]
    #[should_panic(expected = "new column name 'B' already exists")]
    fn test_rename_to_existing() {
        let mut frame = create_test_frame_f64(); // Has cols "A", "B"
        frame.rename("A", "B"); // Try renaming A to B (which exists)
    }
    #[test]
    #[should_panic(expected = "new name 'A' cannot be the same as the old name")]
    fn test_rename_to_self() {
        let mut frame = create_test_frame_f64();
        frame.rename("A", "A");
    }
    #[test]
    #[should_panic(expected = "unknown column label: 'Z'")]
    fn test_rename_panic_unknown() {
        let mut frame = create_test_frame_f64();
        frame.rename("Z", "Y"); // Try renaming non-existent column Z
    }

    #[test]
    #[should_panic(expected = "duplicate column label: A")]
    fn test_add_column_panic_duplicate() {
        let mut frame = create_test_frame_f64(); // Has col "A"
        frame.add_column("A", vec![0.0, 0.0, 0.0]); // Try adding "A" again
    }
    #[test]
    #[should_panic(expected = "column length mismatch")] // Panic comes from Matrix::add_column
    fn test_add_column_panic_len_mismatch() {
        let mut frame = create_test_frame_f64(); // Expects len 3
        frame.add_column("C", vec![0.0, 0.0]); // Provide len 2
    }
    // Removed tests for adding columns to 0-row frames as Matrix constructors prevent 0-row frames.

    #[test]
    fn test_delete_last_column_range_index() {
        // Start with a 1-column frame (since Matrix requires cols >= 1)
        let matrix = Matrix::from_cols(vec![vec![1, 2]]); // 2 rows, 1 col
        let mut frame = Frame::new(matrix, vec!["Single"], None); // Range index 0..2
        assert_eq!(frame.cols(), 1);
        assert_eq!(frame.rows(), 2);

        let deleted_data = frame.delete_column("Single");
        assert_eq!(deleted_data, vec![1, 2]);

        // Assuming Matrix allows 0 columns after deletion, but rows remain
        assert_eq!(frame.cols(), 0);
        assert!(frame.columns().is_empty());
        assert!(frame.col_lookup.is_empty());
        assert_eq!(frame.rows(), 2); // Rows remain
        assert_eq!(frame.index(), &RowIndex::Range(0..2)); // Range index should still reflect rows
    }
    #[test]
    fn test_delete_last_column_int_index() {
        let matrix = Matrix::from_cols(vec![vec![1, 2]]); // 2 rows, 1 col
        let index = RowIndex::Int(vec![10, 20]);
        let mut frame = Frame::new(matrix, vec!["Single"], Some(index.clone()));
        assert_eq!(frame.cols(), 1);
        assert_eq!(frame.rows(), 2);

        let deleted_data = frame.delete_column("Single");
        assert_eq!(deleted_data, vec![1, 2]);

        assert_eq!(frame.cols(), 0);
        assert!(frame.columns().is_empty());
        assert!(frame.col_lookup.is_empty());
        assert_eq!(frame.rows(), 2);
        assert_eq!(frame.index(), &index); // Int index remains unchanged
    }
    #[test]
    #[should_panic(expected = "unknown column label: 'Z'")]
    fn test_delete_column_panic_unknown() {
        let mut frame = create_test_frame_f64();
        frame.delete_column("Z"); // Try deleting non-existent column Z
    }

    #[test]
    fn test_sort_columns_empty_and_single() {
        // Test sorting an empty frame (0 cols) - Create via delete
        let mut frame0 = Frame::new(Matrix::from_cols(vec![vec![1]]), vec!["A"], None);
        frame0.delete_column("A");
        assert_eq!(frame0.cols(), 0);
        assert_eq!(frame0.rows(), 1); // Row remains
        let frame0_clone = frame0.clone();
        frame0.sort_columns(); // Should be a no-op
        assert_eq!(frame0, frame0_clone);
        assert_eq!(frame0.columns(), &[] as &[String]); // Ensure columns are empty

        // Test sorting a frame with a single column
        let mut frame1 = Frame::new(Matrix::from_cols(vec![vec![1.0]]), vec!["Z"], None); // 1x1
        assert_eq!(frame1.cols(), 1);
        let frame1_clone = frame1.clone();
        frame1.sort_columns(); // Should be a no-op
        assert_eq!(frame1, frame1_clone);
        assert_eq!(frame1.columns(), &["Z"]);
    }

    #[test]
    fn test_frame_map() {
        let frame = create_test_frame_f64(); // A=[1,2,3], B=[4,5,6]
        let mapped_frame = frame.frame_map(|x| x * 2.0); // Multiply each value by 2.0
        assert_eq!(mapped_frame.columns(), frame.columns());
        assert_eq!(mapped_frame.index(), frame.index());
        assert!((mapped_frame["A"][0] - 2.0).abs() < FLOAT_TOLERANCE);
        assert!((mapped_frame["A"][1] - 4.0).abs() < FLOAT_TOLERANCE);
        assert!((mapped_frame["A"][2] - 6.0).abs() < FLOAT_TOLERANCE);
        assert!((mapped_frame["B"][0] - 8.0).abs() < FLOAT_TOLERANCE);
        assert!((mapped_frame["B"][1] - 10.0).abs() < FLOAT_TOLERANCE);
        assert!((mapped_frame["B"][2] - 12.0).abs() < FLOAT_TOLERANCE);
    }

    #[test]
    fn test_frame_zip() {
        let f1 = create_test_frame_f64(); // A=[1,2,3], B=[4,5,6]
        let f2 = create_test_frame_f64_alt(); // A=[0.1,0.2,0.3], B=[0.4,0.5,0.6]
        let zipped_frame = f1.frame_zip(&f2, |x, y| x + y); // Element-wise addition
        assert_eq!(zipped_frame.columns(), f1.columns());
        assert_eq!(zipped_frame.index(), f1.index());
        assert!((zipped_frame["A"][0] - 1.1).abs() < FLOAT_TOLERANCE);
        assert!((zipped_frame["A"][1] - 2.2).abs() < FLOAT_TOLERANCE);
        assert!((zipped_frame["A"][2] - 3.3).abs() < FLOAT_TOLERANCE);
        assert!((zipped_frame["B"][0] - 4.4).abs() < FLOAT_TOLERANCE);
        assert!((zipped_frame["B"][1] - 5.5).abs() < FLOAT_TOLERANCE);
        assert!((zipped_frame["B"][2] - 6.6).abs() < FLOAT_TOLERANCE);
    }

    #[test]
    #[should_panic(expected = "Frame::frame_zip: incompatible dimensions (self: 3x1, other: 3x2)")]
    fn test_frame_zip_panic() {
        let mut f1 = create_test_frame_f64();
        let f2 = create_test_frame_f64_alt();
        f1.delete_column("B");

        f1.frame_zip(&f2, |x, y| x + y); // Should panic due to different column counts
    }

    // --- Element-wise Arithmetic Ops Tests ---
    #[test]
    fn test_frame_arithmetic_ops_f64() {
        let f1 = create_test_frame_f64(); // A=[1,2,3], B=[4,5,6]
        let f2 = create_test_frame_f64_alt(); // A=[0.1,0.2,0.3], B=[0.4,0.5,0.6]

        // Addition
        let f_add = &f1 + &f2;
        assert_eq!(f_add.columns(), f1.columns());
        assert_eq!(f_add.index(), f1.index());
        assert!((f_add["A"][0] - 1.1).abs() < FLOAT_TOLERANCE);
        assert!((f_add["A"][1] - 2.2).abs() < FLOAT_TOLERANCE);
        assert!((f_add["A"][2] - 3.3).abs() < FLOAT_TOLERANCE);
        assert!((f_add["B"][0] - 4.4).abs() < FLOAT_TOLERANCE);
        assert!((f_add["B"][1] - 5.5).abs() < FLOAT_TOLERANCE);
        assert!((f_add["B"][2] - 6.6).abs() < FLOAT_TOLERANCE);

        // Subtraction
        let f_sub = &f1 - &f2;
        assert_eq!(f_sub.columns(), f1.columns());
        assert_eq!(f_sub.index(), f1.index());
        assert!((f_sub["A"][0] - 0.9).abs() < FLOAT_TOLERANCE);
        assert!((f_sub["A"][1] - 1.8).abs() < FLOAT_TOLERANCE);
        assert!((f_sub["A"][2] - 2.7).abs() < FLOAT_TOLERANCE);
        assert!((f_sub["B"][0] - 3.6).abs() < FLOAT_TOLERANCE);
        assert!((f_sub["B"][1] - 4.5).abs() < FLOAT_TOLERANCE);
        assert!((f_sub["B"][2] - 5.4).abs() < FLOAT_TOLERANCE);

        // Multiplication
        let f_mul = &f1 * &f2;
        assert_eq!(f_mul.columns(), f1.columns());
        assert_eq!(f_mul.index(), f1.index());
        assert!((f_mul["A"][0] - 0.1).abs() < FLOAT_TOLERANCE); // 1.0 * 0.1
        assert!((f_mul["A"][1] - 0.4).abs() < FLOAT_TOLERANCE); // 2.0 * 0.2
        assert!((f_mul["A"][2] - 0.9).abs() < FLOAT_TOLERANCE); // 3.0 * 0.3
        assert!((f_mul["B"][0] - 1.6).abs() < FLOAT_TOLERANCE); // 4.0 * 0.4
        assert!((f_mul["B"][1] - 2.5).abs() < FLOAT_TOLERANCE); // 5.0 * 0.5
        assert!(
            (f_mul["B"][2] - 3.6).abs() < FLOAT_TOLERANCE,
            "Check B[2] multiplication"
        ); // 6.0 * 0.6

        // Division
        let f_div = &f1 / &f2;
        assert_eq!(f_div.columns(), f1.columns());
        assert_eq!(f_div.index(), f1.index());
        assert!((f_div["A"][0] - 10.0).abs() < FLOAT_TOLERANCE); // 1.0 / 0.1
        assert!((f_div["A"][1] - 10.0).abs() < FLOAT_TOLERANCE); // 2.0 / 0.2
        assert!((f_div["A"][2] - 10.0).abs() < FLOAT_TOLERANCE); // 3.0 / 0.3
        assert!((f_div["B"][0] - 10.0).abs() < FLOAT_TOLERANCE); // 4.0 / 0.4
        assert!((f_div["B"][1] - 10.0).abs() < FLOAT_TOLERANCE); // 5.0 / 0.5
        assert!((f_div["B"][2] - 10.0).abs() < FLOAT_TOLERANCE); // 6.0 / 0.6
    }

    #[test]
    fn test_frame_arithmetic_ops_int() {
        let frame1 = create_test_frame_int(); // X=[1,-2], Y=[3,-4]
        let frame2 = create_test_frame_int_alt(); // X=[10,20], Y=[30,40]

        let frame_add = &frame1 + &frame2; // X=[11,18], Y=[33,36]
        assert_eq!(frame_add.columns(), frame1.columns());
        assert_eq!(frame_add.index(), frame1.index());
        assert_eq!(frame_add["X"], vec![11, 18]);
        assert_eq!(frame_add["Y"], vec![33, 36]);

        let frame_sub = &frame1 - &frame2; // X=[-9,-22], Y=[-27,-44]
        assert_eq!(frame_sub.columns(), frame1.columns());
        assert_eq!(frame_sub.index(), frame1.index());
        assert_eq!(frame_sub["X"], vec![-9, -22]);
        assert_eq!(frame_sub["Y"], vec![-27, -44]);

        let frame_mul = &frame1 * &frame2; // X=[10,-40], Y=[90,-160]
        assert_eq!(frame_mul.columns(), frame1.columns());
        assert_eq!(frame_mul.index(), frame1.index());
        assert_eq!(frame_mul["X"], vec![10, -40]);
        assert_eq!(frame_mul["Y"], vec![90, -160]);

        // Integer division (truncates)
        let frame_div = &frame2 / &frame1; // X=[10/1, 20/-2]=[10,-10], Y=[30/3, 40/-4]=[10,-10]
        assert_eq!(frame_div.columns(), frame1.columns());
        assert_eq!(frame_div.index(), frame1.index());
        assert_eq!(frame_div["X"], vec![10, -10]);
        assert_eq!(frame_div["Y"], vec![10, -10]);
    }

    #[test]
    fn tests_for_frame_arithmetic_ops() {
        let ops: Vec<(
            &str,
            fn(&Frame<f64>, &Frame<f64>) -> Frame<f64>,
            fn(&Frame<f64>, &Frame<f64>) -> Frame<f64>,
        )> = vec![
            ("addition", |a, b| a + b, |a, b| (&*a) + (&*b)),
            ("subtraction", |a, b| a - b, |a, b| (&*a) - (&*b)),
            ("multiplication", |a, b| a * b, |a, b| (&*a) * (&*b)),
            ("division", |a, b| a / b, |a, b| (&*a) / (&*b)),
        ];

        for (op_name, owned_op, ref_op) in ops {
            let f1 = create_test_frame_f64();
            let f2 = create_test_frame_f64_alt();
            let result_owned = owned_op(&f1, &f2);
            let expected = ref_op(&f1, &f2);

            assert_eq!(
                result_owned.columns(),
                f1.columns(),
                "Column mismatch for {}",
                op_name
            );
            assert_eq!(
                result_owned.index(),
                f1.index(),
                "Index mismatch for {}",
                op_name
            );

            let bool_mat = result_owned.matrix().eq_elem(expected.matrix().clone());
            assert!(bool_mat.all(), "Element-wise {} failed", op_name);
        }
    }

    // test not , and or on frame
    #[test]
    fn tests_for_frame_bool_ops() {
        let ops: Vec<(
            &str,
            fn(&Frame<bool>, &Frame<bool>) -> Frame<bool>,
            fn(&Frame<bool>, &Frame<bool>) -> Frame<bool>,
        )> = vec![
            ("and", |a, b| a & b, |a, b| (&*a) & (&*b)),
            ("or", |a, b| a | b, |a, b| (&*a) | (&*b)),
            ("xor", |a, b| a ^ b, |a, b| (&*a) ^ (&*b)),
        ];
        for (op_name, owned_op, ref_op) in ops {
            let f1 = create_test_frame_bool();
            let f2 = create_test_frame_bool_alt();
            let result_owned = owned_op(&f1, &f2);
            let expected = ref_op(&f1, &f2);

            assert_eq!(
                result_owned.columns(),
                f1.columns(),
                "Column mismatch for {}",
                op_name
            );
            assert_eq!(
                result_owned.index(),
                f1.index(),
                "Index mismatch for {}",
                op_name
            );

            let bool_mat = result_owned.matrix().eq_elem(expected.matrix().clone());
            assert!(bool_mat.all(), "Element-wise {} failed", op_name);
        }
    }

    #[test]
    fn test_frame_arithmetic_ops_date_index() {
        let dates = vec![d(2024, 1, 1), d(2024, 1, 2)];
        let index = Some(RowIndex::Date(dates));
        let m1 = Matrix::from_cols(vec![vec![1, 2], vec![10, 20]]);
        let m2 = Matrix::from_cols(vec![vec![3, 4], vec![30, 40]]);
        let f1 = Frame::new(m1, vec!["A", "B"], index.clone());
        let f2 = Frame::new(m2, vec!["A", "B"], index.clone());

        let f_add = &f1 + &f2;
        assert_eq!(f_add.columns(), f1.columns());
        assert_eq!(f_add.index(), f1.index());
        assert_eq!(f_add["A"], vec![4, 6]);
        assert_eq!(f_add["B"], vec![40, 60]);
        assert_eq!(f_add.get_row_date(d(2024, 1, 1))["A"], 4);
        assert_eq!(f_add.get_row_date(d(2024, 1, 2))["B"], 60);
    }

    #[test]
    fn test_bitwise_ops_and_not() {
        let frame_a = create_test_frame_bool(); // P=[T,F], Q=[F,T]
        let frame_b = create_test_frame_bool_alt(); // P=[T,T], Q=[F,F]

        // Bitwise AND
        let frame_and = &frame_a & &frame_b; // P=[T&T, F&T]->[T,F], Q=[F&F, T&F]->[F,F]
        assert_eq!(frame_and.columns(), frame_a.columns());
        assert_eq!(frame_and.index(), frame_a.index());
        assert_eq!(frame_and["P"], vec![true, false]);
        assert_eq!(frame_and["Q"], vec![false, false]);

        // Logical NOT (takes ownership)
        let frame_a_clone = frame_a.clone(); // Clone frame_a as Not consumes it
        let frame_not = !frame_a_clone;
        assert_eq!(frame_not.columns(), frame_a.columns());
        assert_eq!(frame_not.index(), frame_a.index());
        assert_eq!(frame_not["P"], vec![false, true]);
        assert_eq!(frame_not["Q"], vec![true, false]);

        // Check original frame_a is unchanged
        assert_eq!(frame_a["P"], vec![true, false]);
    }
    #[test]
    fn test_bitwise_ops_or_xor() {
        let frame_a = create_test_frame_bool(); // P=[T,F], Q=[F,T]
        let frame_b = create_test_frame_bool_alt(); // P=[T,T], Q=[F,F]

        // Bitwise OR
        let frame_or = &frame_a | &frame_b; // P=[T|T, F|T]->[T,T], Q=[F|F, T|F]->[F,T]
        assert_eq!(frame_or.columns(), frame_a.columns());
        assert_eq!(frame_or.index(), frame_a.index());
        assert_eq!(frame_or["P"], vec![true, true]);
        assert_eq!(frame_or["Q"], vec![false, true]);

        // Bitwise XOR
        let frame_xor = &frame_a ^ &frame_b; // P=[T^T, F^T]->[F,T], Q=[F^F, T^F]->[F,T]
        assert_eq!(frame_xor.columns(), frame_a.columns());
        assert_eq!(frame_xor.index(), frame_a.index());
        assert_eq!(frame_xor["P"], vec![false, true]);
        assert_eq!(frame_xor["Q"], vec![false, true]);
    }

    #[test]
    #[should_panic(expected = "row indices do not match")]
    fn frame_elementwise_ops_panic_index() {
        let frame1 = create_test_frame_f64(); // Range index 0..3
        let matrix2 = create_test_matrix_f64_alt(); // 3 rows
        let index2 = RowIndex::Int(vec![10, 20, 30]); // Different index type/values
        let frame2 = Frame::new(matrix2, vec!["A", "B"], Some(index2));
        let _ = &frame1 + &frame2; // Should panic due to index mismatch
    }
    #[test]
    #[should_panic(expected = "column names do not match")]
    fn frame_elementwise_ops_panic_cols() {
        let frame1 = create_test_frame_f64(); // Columns ["A", "B"]
        let matrix2 = create_test_matrix_f64_alt();
        let frame2 = Frame::new(matrix2, vec!["X", "Y"], None); // Different column names
        let _ = &frame1 + &frame2; // Should panic due to column name mismatch
    }
    #[test]
    #[should_panic(expected = "row indices do not match")]
    fn frame_bitwise_ops_panic_index() {
        let frame1 = create_test_frame_bool(); // Range index 0..2
        let matrix2 = create_test_matrix_bool_alt(); // 2 rows
        let index2 = RowIndex::Int(vec![10, 20]); // Different index
        let frame2 = Frame::new(matrix2, vec!["P", "Q"], Some(index2));
        let _ = &frame1 & &frame2;
    }
    #[test]
    #[should_panic(expected = "column names do not match")]
    fn frame_bitwise_ops_panic_cols() {
        let frame1 = create_test_frame_bool(); // Cols P, Q
        let matrix2 = create_test_matrix_bool_alt();
        let frame2 = Frame::new(matrix2, vec!["X", "Y"], None); // Cols X, Y
        let _ = &frame1 | &frame2;
    }

    // --- Debug Format Tests ---
    #[test]
    fn test_frame_debug_format() {
        let frame = create_test_frame_f64();
        let debug_str = format!("{:?}", frame);
        println!("Frame Debug: {}", debug_str); // Print for manual inspection
        assert!(debug_str.starts_with("Frame"));
        assert!(debug_str.contains("column_names: [\"A\", \"B\"]"));
        assert!(debug_str.contains("index: Range(0..3)"));
        assert!(debug_str.contains("matrix_dims: (3, 2)"));
        // Check for key-value pairs independently of order
        assert!(debug_str.contains("\"A\": 0"));
        assert!(debug_str.contains("\"B\": 1"));
        assert!(debug_str.contains("index_lookup: None"));
    }
    #[test]
    fn test_frame_debug_format_date_index() {
        let matrix = create_test_matrix_string();
        let index = RowIndex::Date(vec![d(2023, 5, 1), d(2023, 5, 10)]);
        let frame = Frame::new(matrix, vec!["X", "Y"], Some(index));
        let debug_str = format!("{:?}", frame);
        println!("Frame Debug Date Index: {}", debug_str);
        assert!(debug_str.starts_with("Frame"));
        assert!(debug_str.contains("column_names: [\"X\", \"Y\"]"));
        assert!(debug_str.contains("index: Date([2023-05-01, 2023-05-10])"));
        assert!(debug_str.contains("matrix_dims: (2, 2)"));
        assert!(debug_str.contains("\"X\": 0"));
        assert!(debug_str.contains("\"Y\": 1"));
        assert!(
            debug_str.contains("index_lookup: Date({2023-05-10: 1, 2023-05-01: 0})")
                || debug_str.contains("index_lookup: Date({2023-05-01: 0, 2023-05-10: 1})")
        );
    }
    #[test]
    fn test_row_view_debug_format() {
        let frame = create_test_frame_f64();
        let row_view = frame.get_row(1); // Physical row 1
        let debug_str = format!("{:?}", row_view);
        println!("RowView Debug: {}", debug_str); // Print for manual inspection
        assert!(debug_str.starts_with("FrameRowView"));
        assert!(debug_str.contains("physical_row_idx: 1"));
        assert!(debug_str.contains("columns: [\"A\", \"B\"]"));
        assert!(debug_str.contains("data: [2.0, 5.0]")); // Data from row 1
    }
    #[test]
    fn test_row_view_mut_debug_format() {
        let mut frame = create_test_frame_f64();
        let row_view_mut = frame.get_row_mut(0); // Physical row 0
        let debug_str = format!("{:?}", row_view_mut);
        println!("RowViewMut Debug: {}", debug_str); // Print for manual inspection
        assert!(debug_str.starts_with("FrameRowViewMut"));
        assert!(debug_str.contains("physical_row_idx: 0"));
        assert!(debug_str.contains("columns: [\"A\", \"B\"]"));
        // Debug format doesn't show data for Mut view to avoid borrow issues
    }

    // --- Miscellaneous Tests ---
    #[test]
    fn test_simple_accessors() {
        let matrix = create_test_matrix_f64(); // 3 rows, 2 cols
        let matrix_dims = (matrix.rows(), matrix.cols());
        let mut frame = Frame::new(matrix.clone(), vec!["A", "B"], None);
        assert_eq!(frame.rows(), 3);
        assert_eq!(frame.cols(), 2);
        assert_eq!(frame.columns(), &["A", "B"]);
        assert_eq!(frame.index(), &RowIndex::Range(0..3));
        // Check matrix accessors
        assert_eq!((frame.matrix().rows(), frame.matrix().cols()), matrix_dims);
        assert_eq!(frame.matrix().get(0, 0), matrix.get(0, 0)); // Get (0,0) via matrix ref
        assert_eq!(frame.matrix().get(0, 0), &1.0);
        // Check mutable matrix accessor (use with caution)
        assert_eq!(
            (frame.matrix_mut().rows(), frame.matrix_mut().cols()),
            matrix_dims
        );
        *frame.matrix_mut().get_mut(0, 0) = 99.0; // Mutate matrix directly
        assert_eq!(frame.matrix().get(0, 0), &99.0); // Verify change via matrix ref
        assert_eq!(frame["A"][0], 99.0); // Verify change via Frame column access
    }
}