Skip to content

DataFrame

pydantable.DataFrame

Bases: _DataFrameForGroupBy, Generic[SchemaT]

Strongly typed lazy table: schema at construction, transforms, then collect.

Construct with DataFrame[SchemaSubclass](data). Pass engine= to use a custom :class:~pydantable.engine.protocols.ExecutionEngine instance; the default is :func:~pydantable.engine.get_default_engine (native Polars/Rust).

Column types come from the schema model; expressions are built with :class:~pydantable.expressions.Expr or attribute access (df.colname). The native engine validates operators and lowers plans to Polars for execution.

Source code in python/pydantable/dataframe/_impl.py
  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
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
class DataFrame(_DataFrameForGroupBy, Generic[SchemaT]):
    """Strongly typed lazy table: schema at construction, transforms, then ``collect``.

    Construct with ``DataFrame[SchemaSubclass](data)``. Pass ``engine=`` to use a
    custom :class:`~pydantable.engine.protocols.ExecutionEngine` instance; the
    default is :func:`~pydantable.engine.get_default_engine` (native Polars/Rust).

    Column types come from the schema model; expressions are built with
    :class:`~pydantable.expressions.Expr` or attribute access (``df.colname``). The
    native engine validates operators and lowers plans to Polars for execution.
    """

    _schema_type: type[BaseModel] | None = None

    def __class_getitem__(cls, schema_type: Any) -> type[DataFrame[Any]]:
        if not isinstance(schema_type, type) or not issubclass(schema_type, BaseModel):
            raise TypeError("DataFrame[Schema] expects a Pydantic BaseModel type.")

        name = f"{cls.__name__}[{schema_type.__name__}]"
        # Important: avoid referencing `DataFrame[Any]` in a runtime `cast(...)`
        # because it triggers `__class_getitem__` again.
        return type(name, (cls,), {"_schema_type": schema_type})

    def __init__(
        self,
        data: Mapping[str, Sequence[Any]] | Any,
        *,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        column_strictness_default: Literal[
            "inherit", "coerce", "strict", "off"
        ] = "coerce",
        nested_strictness_default: Literal[
            "inherit", "coerce", "strict", "off"
        ] = "inherit",
        engine: Any | None = None,
    ) -> None:
        if self._schema_type is None:
            raise TypeError(
                "Use DataFrame[SchemaType](data) to construct a typed DataFrame."
            )

        root_data = validate_columns_strict(
            data,
            self._schema_type,
            validate_elements=None,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            column_strictness_default=column_strictness_default,
            nested_strictness_default=nested_strictness_default,
        )
        self._root_data: Any = root_data
        self._root_schema_type: type[BaseModel] = self._schema_type
        self._current_schema_type: type[BaseModel] = self._schema_type
        self._current_field_types = schema_field_types(self._current_schema_type)
        # Execution engine owns logical planning and materialization
        # (native Polars by default). Pass ``engine=`` to use a custom backend.
        self._engine = engine if engine is not None else get_default_engine()
        self._rust_plan = self._engine.make_plan(
            field_types_for_rust(self.schema_fields())
        )
        # Optional validation options attached to lazy scan roots. These are applied
        # at materialization time (after the engine produces columns), because
        # scan roots are lazy and cannot validate rows up front.
        self._io_validation_enabled: bool = False
        self._io_validation_trusted_mode: (
            Literal["off", "shape_only", "strict"] | None
        ) = None
        self._io_validation_fill_missing_optional: bool = True
        self._io_validation_ignore_errors: bool = False
        self._io_validation_on_validation_errors: (
            Callable[[list[dict[str, Any]]], None] | None
        ) = None
        # Optional default for Polars streaming collect on this object.
        self._engine_streaming_default: bool | None = None

    @classmethod
    def _from_scan_root(
        cls,
        root: Any,
        *,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    ) -> DataFrame[Any]:
        """Build a lazy frame from :class:`pydantable_native._core.ScanFileRoot`.

        Column validation is not applied yet.
        """
        from ._impl_lazy_sources import _from_scan_root as _fsr

        return _fsr(
            cls,
            root,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
        )

    @classmethod
    def read_parquet(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        """Lazy Parquet read (local file path)."""
        from ._impl_lazy_sources import read_parquet as _read_parquet

        return _read_parquet(
            cls,
            path,
            columns=columns,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    async def aread_parquet(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        executor: Executor | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        """Async lazy Parquet read (local path).

        Returns a typed lazy frame backed by a native `ScanFileRoot`. Ingest
        validation options are applied when you materialize (`to_dict()` /
        `collect()` / `to_arrow()` / `to_polars()`), not at scan time.
        """
        from ._impl_lazy_sources import aread_parquet as _aread_parquet

        return await _aread_parquet(
            cls,
            path,
            columns=columns,
            executor=executor,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    def read_parquet_url(
        cls,
        url: str,
        *,
        experimental: bool = True,
        columns: list[str] | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **kwargs: Any,
    ) -> DataFrame[Any]:
        """Lazy Parquet read after HTTP(S) download to a temp file.

        See the DATA_IO_SOURCES guide (project docs).
        """
        from ._impl_lazy_sources import read_parquet_url as _read_parquet_url

        return _read_parquet_url(
            cls,
            url,
            experimental=experimental,
            columns=columns,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **kwargs,
        )

    @classmethod
    async def aread_parquet_url(
        cls,
        url: str,
        *,
        experimental: bool = True,
        columns: list[str] | None = None,
        executor: Executor | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **kwargs: Any,
    ) -> DataFrame[Any]:
        """Async lazy Parquet over HTTP(S) (downloads to a temp file).

        Prefer `DataFrameModel.aread_parquet_url_ctx` /
        `pydantable.io.aread_parquet_url_ctx` for automatic temp-file cleanup.
        Validation options apply on materialization.
        """
        from ._impl_lazy_sources import aread_parquet_url as _aread_parquet_url

        return await _aread_parquet_url(
            cls,
            url,
            experimental=experimental,
            columns=columns,
            executor=executor,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **kwargs,
        )

    @classmethod
    def read_csv(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        from ._impl_lazy_sources import read_csv as _read_csv

        return _read_csv(
            cls,
            path,
            columns=columns,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    async def aread_csv(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        executor: Executor | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        """Async lazy CSV read (local path).

        Validation options apply on materialization (see `aread_parquet`).
        """
        from ._impl_lazy_sources import aread_csv as _aread_csv

        return await _aread_csv(
            cls,
            path,
            columns=columns,
            executor=executor,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    def read_ndjson(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        from ._impl_lazy_sources import read_ndjson as _read_ndjson

        return _read_ndjson(
            cls,
            path,
            columns=columns,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    async def aread_ndjson(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        executor: Executor | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        """Async lazy NDJSON read (local path).

        Validation options apply on materialization (see `aread_parquet`).
        """
        from ._impl_lazy_sources import aread_ndjson as _aread_ndjson

        return await _aread_ndjson(
            cls,
            path,
            columns=columns,
            executor=executor,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    def read_json(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        """Lazy JSON Lines (same as :meth:`read_ndjson`)."""
        from ._impl_lazy_sources import read_json as _read_json

        return _read_json(
            cls,
            path,
            columns=columns,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    async def aread_json(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        executor: Executor | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        """Async lazy JSON Lines read (local path; alias of NDJSON engine).

        Validation options apply on materialization (see `aread_parquet`).
        """
        from ._impl_lazy_sources import aread_json as _aread_json

        return await _aread_json(
            cls,
            path,
            columns=columns,
            executor=executor,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    def read_ipc(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        from ._impl_lazy_sources import read_ipc as _read_ipc

        return _read_ipc(
            cls,
            path,
            columns=columns,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    async def aread_ipc(
        cls,
        path: str | Any,
        *,
        columns: list[str] | None = None,
        executor: Executor | None = None,
        engine_streaming: bool | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
        **scan_kwargs: Any,
    ) -> DataFrame[Any]:
        """Async lazy Arrow IPC file read (local path).

        Validation options apply on materialization (see `aread_parquet`).
        """
        from ._impl_lazy_sources import aread_ipc as _aread_ipc

        return await _aread_ipc(
            cls,
            path,
            columns=columns,
            executor=executor,
            engine_streaming=engine_streaming,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
            **scan_kwargs,
        )

    @classmethod
    def iter_parquet(
        cls,
        path: str | Any,
        *,
        batch_size: int = 65_536,
        columns: list[str] | None = None,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    ):
        """Stream Parquet as batches of in-memory typed frames (PyArrow-backed)."""
        from ._impl_lazy_sources import iter_parquet as _iter_parquet

        yield from _iter_parquet(
            cls,
            path,
            batch_size=batch_size,
            columns=columns,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
        )

    @classmethod
    def iter_ipc(
        cls,
        source: str | Any,
        *,
        batch_size: int = 65_536,
        as_stream: bool = False,
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    ):
        """Stream IPC as batches of in-memory typed frames (PyArrow-backed)."""
        from ._impl_lazy_sources import iter_ipc as _iter_ipc

        yield from _iter_ipc(
            cls,
            source,
            batch_size=batch_size,
            as_stream=as_stream,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
        )

    @classmethod
    def iter_csv(
        cls,
        path: str | Any,
        *,
        batch_size: int = 65_536,
        encoding: str = "utf-8",
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    ):
        """Stream CSV as batches of in-memory typed frames (stdlib CSV)."""
        from ._impl_lazy_sources import iter_csv as _iter_csv

        yield from _iter_csv(
            cls,
            path,
            batch_size=batch_size,
            encoding=encoding,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
        )

    @classmethod
    def iter_ndjson(
        cls,
        path: str | Any,
        *,
        batch_size: int = 65_536,
        encoding: str = "utf-8",
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    ):
        """Stream NDJSON/JSONL as batches of in-memory typed frames (stdlib json)."""
        from ._impl_lazy_sources import iter_ndjson as _iter_ndjson

        yield from _iter_ndjson(
            cls,
            path,
            batch_size=batch_size,
            encoding=encoding,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
        )

    @classmethod
    def iter_json_lines(
        cls,
        path: str | Any,
        *,
        batch_size: int = 65_536,
        encoding: str = "utf-8",
        trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
        fill_missing_optional: bool = True,
        ignore_errors: bool = False,
        on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    ):
        """Stream JSON Lines as batches of in-memory typed frames."""
        from ._impl_lazy_sources import iter_json_lines as _iter_json_lines

        yield from _iter_json_lines(
            cls,
            path,
            batch_size=batch_size,
            encoding=encoding,
            trusted_mode=trusted_mode,
            fill_missing_optional=fill_missing_optional,
            ignore_errors=ignore_errors,
            on_validation_errors=on_validation_errors,
        )

    @classmethod
    def _from_plan(
        cls,
        *,
        root_data: Any,
        root_schema_type: type[BaseModel],
        current_schema_type: type[BaseModel],
        rust_plan: Any,
        engine: Any | None = None,
    ) -> DataFrame[Any]:
        obj = cls.__new__(cls)
        obj._engine = engine if engine is not None else get_default_engine()
        obj._root_data = root_data
        obj._root_schema_type = root_schema_type
        obj._current_schema_type = current_schema_type
        obj._current_field_types = schema_field_types(current_schema_type)
        obj._rust_plan = rust_plan
        obj._schema_type = None
        obj._io_validation_enabled = False
        obj._io_validation_trusted_mode = None
        obj._io_validation_fill_missing_optional = True
        obj._io_validation_ignore_errors = False
        obj._io_validation_on_validation_errors = None
        # Optional default for Polars streaming collect on this object.
        obj._engine_streaming_default = None
        return cast("DataFrame[Any]", obj)

    def _apply_io_validation_if_configured(
        self, column_dict: dict[str, list[Any]]
    ) -> dict[str, list[Any]]:
        """
        Apply optional validation/row-skipping configured on lazy scan reads.

        This is intentionally post-execution: scan roots are lazy, so we can only
        validate once we have materialized Python columns.
        """
        if not self._io_validation_enabled:
            return column_dict
        mode = self._io_validation_trusted_mode or "off"
        return validate_columns_strict(
            column_dict,
            self._current_schema_type,
            validate_elements=None,
            trusted_mode=mode,
            fill_missing_optional=self._io_validation_fill_missing_optional,
            ignore_errors=self._io_validation_ignore_errors,
            on_validation_errors=self._io_validation_on_validation_errors,
        )

    def _column_dict_in_schema_order(
        self, column_dict: dict[str, list[Any]]
    ) -> dict[str, list[Any]]:
        """Materialized dicts may follow engine hash order; align to schema key order.

        Columns present in the materialized output but not in the current schema
        (e.g. ``_merge`` from ``merge(indicator=True)``) are appended after, stable
        by insertion order from the engine dict.
        """
        order = list(self._current_field_types.keys())
        out: dict[str, list[Any]] = {}
        for k in order:
            if k in column_dict:
                out[k] = column_dict[k]
        for k, v in column_dict.items():
            if k not in out:
                out[k] = v
        return out

    def _materialize_columns_with_missing_optional_fallback(
        self, *, streaming: bool
    ) -> dict[str, list[Any]]:
        """
        Materialize columns via the Rust engine, with a fallback for scan roots
        missing optional schema fields.

        Polars scans will error if a selected column is absent. For optional
        schema fields, we treat missing columns as all-null and retry execution
        with those columns omitted, then fill them with `None` during validation.
        """
        return materialize_with_optional_scan_fallback_sync(
            self._engine,
            plan=self._rust_plan,
            root_data=self._root_data,
            field_types=dict(self._current_field_types),
            current_schema_type=self._current_schema_type,
            io_validation_fill_missing_optional=self._io_validation_fill_missing_optional,
            streaming=streaming,
            error_context=self._materialize_error_context(),
        )

    async def _materialize_columns_with_missing_optional_fallback_async(
        self, *, streaming: bool, executor: Executor | None
    ) -> dict[str, list[Any]]:
        return await materialize_with_optional_scan_fallback_async(
            self._engine,
            plan=self._rust_plan,
            root_data=self._root_data,
            field_types=dict(self._current_field_types),
            current_schema_type=self._current_schema_type,
            io_validation_fill_missing_optional=self._io_validation_fill_missing_optional,
            streaming=streaming,
            error_context=self._materialize_error_context(),
            executor=executor,
        )

    async def _materialize_columns_async(
        self, *, streaming: bool, executor: Executor | None
    ) -> dict[str, list[Any]]:
        raw = await self._materialize_columns_with_missing_optional_fallback_async(
            streaming=streaming, executor=executor
        )
        raw = _coerce_enum_columns(raw, self._current_field_types)
        return self._apply_io_validation_if_configured(raw)

    @property
    def schema_type(self) -> type[BaseModel]:
        return self._current_schema_type

    def schema_fields(self) -> dict[str, Any]:
        return dict(self._current_field_types)

    @staticmethod
    def _expected_schema_fields(schema: type[BaseModel]) -> dict[str, Any]:
        # Prefer the direct Pydantic model_fields mapping to avoid evaluating
        # inherited annotations via typing.get_type_hints at runtime.
        return {
            name: field.annotation
            for name, field in schema.model_fields.items()
            if not name.startswith("_")
        }

    def as_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        if not isinstance(schema, type) or not issubclass(schema, BaseModel):
            raise TypeError("as_schema(schema=...) expects a Pydantic BaseModel type.")
        if validate_schema:
            expected = self._expected_schema_fields(schema)
            actual = self.schema_fields()
            if set(expected) != set(actual) or any(
                expected[k] != actual[k] for k in expected if k in actual
            ):
                raise TypeError(
                    "as_schema(schema mismatch): expected "
                    f"{sorted(expected)} got {sorted(actual)}"
                )
        # Avoid `DataFrame[schema]` here: some type checkers treat `[...]` as a
        # type expression and reject runtime variables.
        df_cls = DataFrame.__class_getitem__(schema)
        return cast(
            "DataFrame[AfterSchemaT]",
            df_cls._from_plan(
                root_data=self._root_data,
                root_schema_type=self._root_schema_type,
                current_schema_type=schema,
                rust_plan=self._rust_plan,
                engine=self._engine,
            ),
        )

    def try_as_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT] | None:
        if not isinstance(schema, type) or not issubclass(schema, BaseModel):
            raise TypeError(
                "try_as_schema(schema=...) expects a Pydantic BaseModel type."
            )
        if not validate_schema:
            return self.as_schema(schema, validate_schema=False)
        expected = self._expected_schema_fields(schema)
        actual = self.schema_fields()
        if set(expected) != set(actual) or any(
            expected[k] != actual[k] for k in expected if k in actual
        ):
            return None
        return self.as_schema(schema, validate_schema=False)

    def assert_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        if not isinstance(schema, type) or not issubclass(schema, BaseModel):
            raise TypeError(
                "assert_schema(schema=...) expects a Pydantic BaseModel type."
            )
        if not validate_schema:
            return self.as_schema(schema, validate_schema=False)
        expected = self._expected_schema_fields(schema)
        actual = self.schema_fields()
        if set(expected) != set(actual) or any(
            expected[k] != actual[k] for k in expected if k in actual
        ):
            missing = sorted(set(expected) - set(actual))
            extra = sorted(set(actual) - set(expected))
            mismatched = sorted(
                k for k in set(expected) & set(actual) if expected[k] != actual[k]
            )
            raise TypeError(
                "assert_schema(schema mismatch): "
                f"missing={missing} extra={extra} mismatched={mismatched}"
            )
        return self.as_schema(schema, validate_schema=False)

    # Aliases for parity with DataFrameModel's after-model helpers.
    def as_model(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.as_schema(schema, validate_schema=validate_schema)

    def try_as_model(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT] | None:
        return self.try_as_schema(schema, validate_schema=validate_schema)

    def assert_model(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.assert_schema(schema, validate_schema=validate_schema)

    @property
    def columns(self) -> list[str]:
        """Current logical column names (schema order)."""
        return list(self._current_field_types.keys())

    @property
    def shape(self) -> tuple[int, int]:
        """``(n_rows, n_cols)`` from the **root** column buffers when present.

        After lazy transforms (e.g. :meth:`filter`) that do not replace
        ``_root_data``, the row count may **not** match materialized output; use
        :meth:`to_dict` or :meth:`collect` for the true row count.
        """
        if not self._root_data:
            return (0, len(self._current_field_types))
        rd = self._root_data
        if _is_scan_file_root(rd):
            return (0, len(self._current_field_types))
        if _is_polars_dataframe(rd):
            return (len(rd), len(self._current_field_types))
        first = next(iter(rd.values()))
        return (len(first), len(self._current_field_types))

    @property
    def empty(self) -> bool:
        """True when the root buffer has zero rows (see :attr:`shape`)."""
        return self.shape[0] == 0

    @property
    def dtypes(self) -> dict[str, Any]:
        """Map column name → Pydantic field annotation (not pandas dtype objects)."""
        return dict(self._current_field_types)

    def info(self) -> str:
        """Multi-line summary string (schema, dtypes, :attr:`shape` caveat).

        **Cost:** does not materialize row data; uses schema and root-buffer
        :attr:`shape` only. See the EXECUTION guide (project docs)
        **Materialization costs**.
        """
        schema_qn = getattr(
            self._current_schema_type,
            "__qualname__",
            self._current_schema_type.__name__,
        )
        lines = [
            f"{self.__class__.__name__}",
            f"  schema: {schema_qn}",
            f"  columns: {len(self._current_field_types)}",
            f"  shape (root buffer): {self.shape[0]} x {self.shape[1]}",
            "  Note: after lazy transforms (e.g. filter), root row count may not "
            "match materialized rows; use to_dict() or collect() for true count.",
            "",
            "dtypes:",
        ]
        for name, ann in self._current_field_types.items():
            lines.append(f"  {name}: {_dtype_repr(ann)}")
        return "\n".join(lines)

    def describe(self) -> str:
        """Summary statistics for int, float, bool, str, and date/datetime columns.

        **Cost:** one full :meth:`to_dict()` materialization. String columns
        ``n_unique`` scans all non-null strings. See the EXECUTION guide (project docs).
        """
        numeric = [
            n for n, a in self._current_field_types.items() if _is_describe_numeric(a)
        ]
        bool_cols = [
            n for n, a in self._current_field_types.items() if _is_describe_bool(a)
        ]
        str_cols = [
            n for n, a in self._current_field_types.items() if _is_describe_str(a)
        ]
        temporal_cols = [
            n for n, a in self._current_field_types.items() if _is_describe_temporal(a)
        ]
        if not numeric and not bool_cols and not str_cols and not temporal_cols:
            return "describe(): no int/float/bool/str/date/datetime columns in schema."
        data = self.to_dict()
        lines = [
            "describe() — one to_dict(); int/float/bool/str/date/datetime columns.",
            "",
        ]
        for name in numeric:
            col = data[name]
            vals = [x for x in col if x is not None]
            c = len(vals)
            if c == 0:
                lines.append(f"{name}: count=0 (all null)")
                continue
            mean_v = statistics.mean(vals)
            mn, mx = min(vals), max(vals)
            if c >= 2:
                std_v = statistics.stdev(vals)
                extra = ""
                if c >= 4:
                    try:
                        import numpy as np  # type: ignore[import-not-found]

                        arr = np.asarray(vals, dtype=float)
                        m = float(arr.mean())
                        s = float(arr.std(ddof=1))
                        if s > 0:
                            skew_v = float(np.mean(((arr - m) / s) ** 3))
                            kurt_v = float(np.mean(((arr - m) / s) ** 4) - 3.0)
                            sem_v = s / (c**0.5)
                            extra = (
                                f" skew={skew_v:.6g} kurtosis={kurt_v:.6g} "
                                f"sem={sem_v:.6g}"
                            )
                    except ImportError:
                        pass
                lines.append(
                    f"{name}: count={c} mean={mean_v:.6g} std={std_v:.6g} "
                    f"min={mn} max={mx}{extra}"
                )
            else:
                lines.append(f"{name}: count={c} mean={mean_v:.6g} min={mn} max={mx}")
        for name in bool_cols:
            col = data[name]
            non_null = [x for x in col if x is not None]
            n_null = len(col) - len(non_null)
            nt = sum(1 for x in non_null if x is True)
            nf = sum(1 for x in non_null if x is False)
            lines.append(
                f"{name}: count={len(non_null)} true={nt} false={nf} null={n_null}"
            )
        for name in str_cols:
            col = data[name]
            raw = [x for x in col if x is not None]
            str_vals = [x for x in raw if isinstance(x, str)]
            n_null = len(col) - len(raw)
            if not str_vals:
                lines.append(f"{name}: count=0 (all null)")
                continue
            n_unique = len(set(str_vals))
            lens = [len(s) for s in str_vals]
            lines.append(
                f"{name}: count={len(str_vals)} n_unique={n_unique} "
                f"min_len={min(lens)} max_len={max(lens)} null={n_null}"
            )
        for name in temporal_cols:
            col = data[name]
            raw = [x for x in col if x is not None]
            n_null = len(col) - len(raw)
            if not raw:
                lines.append(f"{name}: count=0 (all null)")
                continue
            vals = [x for x in raw if isinstance(x, (date, datetime))]
            if not vals:
                lines.append(f"{name}: count=0 (all null)")
                continue
            mn, mx = min(vals), max(vals)
            lines.append(
                f"{name}: count={len(vals)} min={mn!s} max={mx!s} null={n_null}"
            )
        return "\n".join(lines)

    def value_counts(
        self,
        column: str,
        *,
        normalize: bool = False,
        dropna: bool = True,
    ) -> dict[Any, int | float]:
        """Count rows per distinct value in ``column`` (group-by aggregation).

        **Cost:** engine aggregation (same path as :meth:`group_by` / :meth:`agg`).
        See the EXECUTION guide (project docs).

        Returns a dict sorted by count descending, then ``repr(key)``. When
        ``normalize=True``, values are fractions in ``[0, 1]`` (``float``).
        """
        if column not in self._current_field_types:
            raise KeyError(f"Unknown column {column!r} for current schema.")
        out_name = "__pydantable_vc_n"
        g = self.group_by(column, drop_nulls=bool(dropna)).agg(
            streaming=None,
            **{out_name: ("count", column)},
        )
        d = g.to_dict()
        keys = d[column]
        counts = d[out_name]
        result: dict[Any, int] = {}
        for k, c in zip(keys, counts, strict=True):
            if dropna and k is None:
                continue
            result[k] = int(c) if c is not None else 0
        total = sum(result.values())
        if normalize:
            if total == 0:
                return {k: 0.0 for k in result}
            return {k: v / total for k, v in result.items()}
        return dict(sorted(result.items(), key=lambda kv: (-kv[1], repr(kv[0]))))

    def _materialize_error_context(self) -> str:
        st = self._current_schema_type
        qn = getattr(st, "__qualname__", st.__name__)
        return f"schema={qn}"

    def _field_types_from_descriptors(
        self,
        descriptors: Mapping[str, Mapping[str, Any]],
        *,
        previous: Mapping[str, Any] | None = None,
    ) -> dict[str, Any]:
        derived = schema_from_descriptors(descriptors)
        prev = self._current_field_types if previous is None else previous
        return merge_field_types_preserving_identity(prev, descriptors, derived)

    def col(self, name: str) -> ColumnRef:
        if name not in self._current_field_types:
            raise KeyError(f"Unknown column {name!r} for current schema.")
        return ColumnRef(name=name, dtype=self._current_field_types[name])

    def __getattr__(self, item: str) -> Any:
        # Called only when attribute resolution fails; treat schema fields as columns.
        if item in self._current_field_types:
            return self.col(item)
        raise AttributeError(item)

    def __repr__(self) -> str:
        fields = self._current_field_types
        schema = self._current_schema_type
        schema_qn = getattr(schema, "__qualname__", schema.__name__)
        cls_name = self.__class__.__name__
        n = len(fields)
        lines = [
            f"{cls_name}",
            f"  schema: {schema_qn}",
            f"  columns ({n}):",
        ]
        if not fields:
            lines.append("    (none)")
            return "\n".join(lines)
        items = list(fields.items())
        shown = items[:_REPR_MAX_COLUMNS]
        name_w = max(len(name) for name, _ in shown)
        for name, ann in shown:
            dtype_s = _dtype_repr(ann)
            lines.append(f"    {name:<{name_w}}  {dtype_s}")
        rest = len(items) - len(shown)
        if rest > 0:
            lines.append(f"    … and {rest} more")
        return "\n".join(lines)

    def _repr_html_(self) -> str:
        """Rich HTML table for Jupyter / IPython (preview only; materializes data).

        Row/column/cell limits default to module constants but can be set via
        :mod:`pydantable.display` or ``PYDANTABLE_REPR_HTML_*`` env vars.
        **Cost:** same as :meth:`head` for the bounded slice plus :meth:`to_dict`
        on that slice. See the EXECUTION guide (project docs) **Jupyter / HTML**.
        """
        try:
            return self._repr_html_impl()
        except Exception as e:  # pragma: no cover - defensive for notebook UX
            logging.getLogger(__name__).debug(
                "HTML repr failed; rendering error fallback.", exc_info=True
            )
            err = html.escape(str(e))
            return (
                '<div class="pydantable-render pydantable-render--error" '
                'style="font-family:ui-sans-serif,system-ui,sans-serif;'
                "font-size:13px;margin:0 0 1rem 0;padding:14px 16px;"
                "border-radius:12px;border:1px solid #fecaca;background:#fef2f2;"
                'color:#991b1b;box-shadow:0 1px 2px rgba(127,29,29,0.06);">'
                '<p style="margin:0 0 8px 0;font-weight:600;">HTML preview failed</p>'
                f'<pre style="margin:0;white-space:pre-wrap;word-break:break-word;'
                f'font-size:12px;color:#7f1d1d;">{err}</pre></div>'
            )

    def _repr_mimebundle_(
        self,
        include: Any = None,
        exclude: Any = None,
    ) -> dict[str, Any]:
        """Jupyter / IPython: prefer HTML table plus plain text fallback."""
        return {
            "text/plain": repr(self),
            "text/html": self._repr_html_(),
        }

    def _repr_html_impl(self) -> str:
        cols_all = list(self._current_field_types.keys())
        if not cols_all:
            return (
                '<div class="pydantable-render" style="margin:0 0 1rem 0;">'
                '<div class="pydantable-surface" style="border-radius:12px;'
                "border:1px dashed #cbd5e1;background:#f8fafc;"
                "padding:1.5rem 1.25rem;text-align:center;color:#64748b;"
                "font-family:ui-sans-serif,system-ui,sans-serif;font-size:13px;"
                'box-shadow:0 1px 2px rgba(15,23,42,0.04);">'
                '<p style="margin:0;"><em>No columns</em></p></div></div>'
            )

        lim = get_repr_html_limits()
        preview = self.head(lim.max_rows)
        data = preview.to_dict()
        n_rows = len(next(iter(data.values()))) if data else 0

        col_order = [c for c in cols_all if c in data]
        for c in data:
            if c not in col_order:
                col_order.append(c)

        n_cols_total = len(col_order)
        col_trunc = n_cols_total > lim.max_cols
        shown_cols = col_order[: lim.max_cols]
        sub: dict[str, list[Any]] = {c: data[c] for c in shown_cols}

        st = self._current_schema_type
        schema_qn = getattr(st, "__qualname__", st.__name__)
        caption = f"{self.__class__.__name__} · schema={schema_qn}"

        note_parts: list[str] = [
            f"Preview: {n_rows} row{'s' if n_rows != 1 else ''} x "
            f"{len(shown_cols)} column{'s' if len(shown_cols) != 1 else ''} shown"
        ]
        if n_rows >= lim.max_rows:
            note_parts.append(f"(up to {lim.max_rows} rows)")
        if col_trunc:
            rest = n_cols_total - len(shown_cols)
            col_s = "column" if rest == 1 else "columns"
            note_parts.append(f"(… and {rest} more {col_s} omitted)")
        note = " ".join(note_parts)

        return _dataframe_to_html_fragment(
            column_dict=sub,
            column_order=shown_cols,
            caption=caption,
            note=note,
            max_cell_len=lim.max_cell_len,
        )

    def with_columns(self, *exprs: Any, **new_columns: Expr | Any) -> DataFrame[Any]:
        """Add or replace columns.

        Values are :class:`~pydantable.expressions.Expr` or plain literals.
        """
        from ._ops import plan_with_columns

        return plan_with_columns(self, *exprs, **new_columns)

    def with_columns_cast(
        self, selector: Selector, dtype: Any, *, strict: bool = True
    ) -> DataFrame[Any]:
        """Cast columns selected by a schema-driven Selector."""
        if not isinstance(selector, Selector):
            raise TypeError("with_columns_cast(selector=...) expects a Selector.")
        selected = selector.resolve(self._current_field_types)
        if not selected:
            if strict:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"with_columns_cast({selector!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            return self
        updates: dict[str, Expr] = {c: self.col(c).cast(dtype) for c in selected}
        return self.with_columns(**updates)

    def with_columns_fill_null(
        self,
        selector: Selector,
        *,
        value: Any = None,
        strategy: str | None = None,
        strict: bool = True,
    ) -> DataFrame[Any]:
        """Fill nulls for columns selected by a schema-driven Selector."""
        if not isinstance(selector, Selector):
            raise TypeError("with_columns_fill_null(selector=...) expects a Selector.")
        selected = selector.resolve(self._current_field_types)
        if not selected:
            if strict:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"with_columns_fill_null({selector!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            return self
        return self.fill_null(value, strategy=strategy, subset=selected)

    def select_schema(self, selector: Selector) -> DataFrame[Any]:
        """Project columns using a schema-first Selector (explicit helper)."""
        if not isinstance(selector, Selector):
            raise TypeError("select_schema(selector) expects a Selector.")
        return self.select(selector)

    def select(
        self,
        *cols: str | ColumnRef | Expr | AliasedExpr | Selector,
        exclude: Selector | Sequence[str] | None = None,
        **named: Any,
    ) -> DataFrame[Any]:
        """Project columns and/or compute a **single-row** frame of global aggregates.

        Positional arguments: base column names, single-column refs, or globals such
        as :func:`~pydantable.expressions.global_sum`. Keyword arguments are only for
        named global aggregates. Plain projections and globals cannot be mixed.
        """
        named_items: list[tuple[str, Any]] = []
        for name, e in named.items():
            if not isinstance(e, Expr):
                raise TypeError(
                    "select() keyword arguments must be Expr instances "
                    "(global aggregates)."
                )
            named_items.append((name, e._rust_expr))

        exclude_set: set[str] = set()
        if exclude is not None:
            if isinstance(exclude, Selector):
                exclude_set = set(exclude.resolve(self._current_field_types))
            else:
                exclude_set = {str(c) for c in exclude}

        aggs: list[tuple[str, Any]] = []
        projects: list[str] = []
        computed: dict[str, Any] = {}
        for col in cols:
            if isinstance(col, str):
                projects.append(col)
            elif isinstance(col, Selector):
                resolved = col.resolve(self._current_field_types)
                if not resolved:
                    available = ", ".join(repr(c) for c in self._current_field_types)
                    raise ValueError(
                        f"select({col!r}) matched no columns. "
                        f"Available columns: [{available}]"
                    )
                projects.extend(resolved)
            elif isinstance(col, AliasedExpr):
                if not isinstance(col.expr, Expr):
                    raise TypeError("select(AliasedExpr) expects an Expr.")
                computed[col.name] = col.expr._rust_expr
                projects.append(col.name)
            elif isinstance(col, Expr):
                if self._engine.expr_is_global_agg(col._rust_expr):
                    alias = self._engine.expr_global_default_alias(col._rust_expr)
                    if alias is None:
                        raise TypeError(
                            "global aggregate in select() is missing a default "
                            "output name."
                        )
                    aggs.append((alias, col._rust_expr))
                else:
                    if isinstance(col, ColumnRef):
                        projects.append(col._column_name)  # type: ignore[attr-defined]
                    else:
                        raise TypeError(
                            "select() accepts column names, ColumnRef expressions, "
                            "global aggregates, or Expr.alias('name') for computed "
                            "expressions."
                        )
            else:
                raise TypeError(
                    "select() accepts column names, Selector objects, "
                    "ColumnRef expressions, global aggregates, or "
                    "Expr.alias('name') (AliasedExpr)."
                )

        if named_items and (projects or aggs):
            raise TypeError(
                "select() cannot mix keyword aggregates with positional column "
                "names or aggregates."
            )
        if aggs and projects:
            raise TypeError(
                "select() cannot mix global aggregates with plain column projections."
            )
        if exclude_set and (named_items or aggs):
            raise TypeError(
                "select(exclude=...) cannot be used with global aggregates."
            )
        if named_items:
            rust_plan = self._engine.plan_global_select(self._rust_plan, named_items)
        elif aggs:
            rust_plan = self._engine.plan_global_select(self._rust_plan, aggs)
        else:
            if not projects:
                if not exclude_set:
                    raise ValueError("select() requires at least one column.")
                projects = [
                    c for c in self._current_field_types if c not in exclude_set
                ]
            else:
                projects = [c for c in projects if c not in exclude_set]
            if not projects:
                raise ValueError("select(...) produced an empty projection.")
            rust_plan = self._rust_plan
            if computed:
                rust_plan = self._engine.plan_with_columns(rust_plan, computed)
            rust_plan = self._engine.plan_select(rust_plan, projects)
        desc = rust_plan.schema_descriptors()
        derived_fields = self._field_types_from_descriptors(desc)
        # Preserve order: for plain projections, match call order.
        if projects and not aggs and not named_items:
            desired_order = [c for c in projects if c in derived_fields]
            ordered: dict[str, Any] = {k: derived_fields[k] for k in desired_order}
            for k, v in derived_fields.items():
                if k not in ordered:
                    ordered[k] = v
            derived_fields = ordered
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def select_all(self) -> DataFrame[Any]:
        """Select all columns in schema order (schema-driven helper)."""
        return self.select(*list(self._current_field_types.keys()))

    def select_prefix(self, prefix: str) -> DataFrame[Any]:
        """Select columns whose names start with `prefix` (schema-driven helper)."""
        if not isinstance(prefix, str):
            raise TypeError("select_prefix(prefix) expects a string.")
        cols = [c for c in self._current_field_types if c.startswith(prefix)]
        if not cols:
            raise ValueError(f"select_prefix({prefix!r}) matched no columns.")
        return self.select(*cols)

    def select_suffix(self, suffix: str) -> DataFrame[Any]:
        """Select columns whose names end with `suffix` (schema-driven helper)."""
        if not isinstance(suffix, str):
            raise TypeError("select_suffix(suffix) expects a string.")
        cols = [c for c in self._current_field_types if c.endswith(suffix)]
        if not cols:
            raise ValueError(f"select_suffix({suffix!r}) matched no columns.")
        return self.select(*cols)

    def _resolve_column_names_or_selector(
        self, item: str | Selector, *, arg_name: str
    ) -> list[str]:
        if isinstance(item, str):
            return [item]
        if isinstance(item, Selector):
            resolved = item.resolve(self._current_field_types)
            if not resolved:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"{arg_name}={item!r} matched no columns. "
                    f"Available columns: [{available}]"
                )
            return resolved
        raise TypeError(f"{arg_name} expects a column name or Selector.")

    def reorder_columns(self, order: Sequence[str | Selector]) -> DataFrame[Any]:
        """Reorder columns by explicit names/selectors; append remaining columns."""
        wanted: list[str] = []
        for it in order:
            wanted.extend(self._resolve_column_names_or_selector(it, arg_name="order"))
        if len(set(wanted)) != len(wanted):
            raise ValueError("reorder_columns(order=...) contains duplicate columns.")
        remainder = [c for c in self._current_field_types if c not in wanted]
        return self.select(*wanted, *remainder)

    def select_first(self, *cols_or_selectors: str | Selector) -> DataFrame[Any]:
        """Move selected columns to the front; keep remaining order."""
        wanted: list[str] = []
        for it in cols_or_selectors:
            wanted.extend(self._resolve_column_names_or_selector(it, arg_name="cols"))
        if len(set(wanted)) != len(wanted):
            raise ValueError("select_first(...) contains duplicate columns.")
        remainder = [c for c in self._current_field_types if c not in wanted]
        return self.select(*wanted, *remainder)

    def select_last(self, *cols_or_selectors: str | Selector) -> DataFrame[Any]:
        """Move selected columns to the end; keep remaining order."""
        wanted: list[str] = []
        for it in cols_or_selectors:
            wanted.extend(self._resolve_column_names_or_selector(it, arg_name="cols"))
        if len(set(wanted)) != len(wanted):
            raise ValueError("select_last(...) contains duplicate columns.")
        remainder = [c for c in self._current_field_types if c not in wanted]
        return self.select(*remainder, *wanted)

    def move(
        self,
        cols_or_selector: str | Selector,
        *,
        before: str | None = None,
        after: str | None = None,
    ) -> DataFrame[Any]:
        """Move column(s) before/after an anchor column (schema-first helper)."""
        if (before is None) == (after is None):
            raise TypeError(
                "move(..., before=...) or move(..., after=...) is required."
            )
        moving = self._resolve_column_names_or_selector(
            cols_or_selector, arg_name="cols"
        )
        anchor = before if before is not None else after
        if not isinstance(anchor, str):
            raise TypeError("move(..., before/after=...) expects a column name.")
        if anchor not in self._current_field_types:
            raise KeyError(f"move() unknown anchor column {anchor!r}.")
        if anchor in moving:
            raise ValueError("move() cannot move a column relative to itself.")
        # Remove moving cols, then re-insert as a block.
        base = [c for c in self._current_field_types if c not in moving]
        idx = base.index(anchor)
        insert_at = idx if before is not None else idx + 1
        new_order = [*base[:insert_at], *moving, *base[insert_at:]]
        return self.select(*new_order)

    def filter(self, condition: Expr) -> DataFrame[Any]:
        """Keep rows where the boolean ``condition`` is true."""
        from ._ops import plan_filter

        return plan_filter(self, condition)

    def sort(
        self,
        *by: str | ColumnRef,
        descending: bool | Sequence[bool] = False,
        nulls_last: bool | Sequence[bool] | None = None,
        maintain_order: bool = False,
    ) -> DataFrame[Any]:
        """Sort by one or more columns (names or single-column expressions)."""
        keys: list[str] = []
        for key in by:
            if isinstance(key, str):
                keys.append(key)
            elif isinstance(key, Expr):
                referenced = key.referenced_columns()
                if len(referenced) != 1:
                    raise TypeError(
                        "sort() accepts column names or a ColumnRef expression."
                    )
                keys.append(next(iter(referenced)))
            else:
                raise TypeError("sort() accepts column names or ColumnRef objects.")

        desc = (
            [descending] * len(keys)
            if isinstance(descending, bool)
            else list(descending)
        )
        if len(desc) != len(keys):
            raise ValueError("sort(descending=...) length must match sort keys.")
        if nulls_last is None:
            nl = []
        elif isinstance(nulls_last, bool):
            nl = [nulls_last] * len(keys)
        else:
            nl = list(nulls_last)
        if nl and len(nl) != len(keys):
            raise ValueError("sort(nulls_last=...) length must match sort keys.")
        rust_plan = self._engine.plan_sort(
            self._rust_plan, keys, desc, nl, bool(maintain_order)
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=self._current_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def unique(
        self,
        subset: Sequence[str] | None = None,
        *,
        keep: str = "first",
        maintain_order: bool = False,
    ) -> DataFrame[Any]:
        rust_plan = self._engine.plan_unique(
            self._rust_plan,
            None if subset is None else list(subset),
            keep,
            bool(maintain_order),
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=self._current_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def duplicated(
        self,
        subset: Sequence[str] | None = None,
        *,
        keep: str | bool = "first",
    ) -> DataFrame[Any]:
        """Single-column boolean frame ``duplicated`` (row-wise duplicate mask)."""
        if keep is True:
            raise ValueError("duplicated(keep=True) is invalid; use 'first' or 'last'.")
        keep_s = "none" if keep is False else str(keep)
        if keep_s not in ("first", "last", "none"):
            raise ValueError(
                "duplicated(keep=...) must be 'first', 'last', or False "
                "(pandas parity)."
            )
        rust_plan = self._engine.plan_duplicate_mask(
            self._rust_plan,
            None if subset is None else list(subset),
            keep_s,
        )
        desc = rust_plan.schema_descriptors()
        derived_fields = self._field_types_from_descriptors(desc)
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def drop_duplicate_groups(
        self,
        subset: Sequence[str] | None = None,
    ) -> DataFrame[Any]:
        """Drop rows whose key appears in a duplicate group.

        ``subset`` selects key columns; if omitted, all columns participate.
        Same filter as pandas ``drop_duplicates(keep=False)``.
        """
        rust_plan = self._engine.plan_drop_duplicate_groups(
            self._rust_plan,
            None if subset is None else list(subset),
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=self._current_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def distinct(
        self,
        subset: Sequence[str] | None = None,
        *,
        keep: str = "first",
    ) -> DataFrame[Any]:
        return self.unique(subset=subset, keep=keep)

    def drop(
        self, *columns: str | ColumnRef | Selector, strict: bool = True
    ) -> DataFrame[Any]:
        selected: list[str] = []
        for col in columns:
            if isinstance(col, str):
                selected.append(col)
            elif isinstance(col, Selector):
                selected.extend(col.resolve(self._current_field_types))
            elif isinstance(col, Expr):
                referenced = col.referenced_columns()
                if len(referenced) != 1:
                    raise TypeError(
                        "drop() accepts column names or a ColumnRef expression."
                    )
                selected.append(next(iter(referenced)))
            else:
                raise TypeError("drop() accepts column names or ColumnRef objects.")
        if not strict:
            selected = [c for c in selected if c in self._current_field_types]
        if not selected:
            return self
        rust_plan = self._engine.plan_drop(self._rust_plan, selected)
        desc = rust_plan.schema_descriptors()
        derived_fields = self._field_types_from_descriptors(desc)
        # Preserve order: keep existing column order minus dropped columns.
        desired_order = [k for k in self._current_field_types if k in derived_fields]
        ordered: dict[str, Any] = {k: derived_fields[k] for k in desired_order}
        for k, v in derived_fields.items():
            if k not in ordered:
                ordered[k] = v
        derived_fields = ordered
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def rename(
        self, columns: Mapping[str, str], *, strict: bool = True
    ) -> DataFrame[Any]:
        rename_map = dict(columns)
        if not strict:
            rename_map = {
                k: v for k, v in rename_map.items() if k in self._current_field_types
            }
        rust_plan = self._engine.plan_rename(self._rust_plan, rename_map)
        desc = rust_plan.schema_descriptors()
        rename_prev: dict[str, Any] = dict(self._current_field_types)
        for old_name, new_name in rename_map.items():
            if old_name in self._current_field_types:
                rename_prev[new_name] = self._current_field_types[old_name]
        derived_fields = self._field_types_from_descriptors(desc, previous=rename_prev)
        # Preserve column order: renamed columns stay in the original position.
        desired_order: list[str] = []
        for name in self._current_field_types:
            desired_order.append(rename_map.get(name, name))
        ordered: dict[str, Any] = {
            k: derived_fields[k] for k in desired_order if k in derived_fields
        }
        for k, v in derived_fields.items():
            if k not in ordered:
                ordered[k] = v
        derived_fields = ordered
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def rename_with_selector(
        self,
        selector: Selector,
        fn: Callable[[str], str],
        *,
        strict: bool = True,
    ) -> DataFrame[Any]:
        """Rename columns selected by a schema-driven Selector."""
        if not isinstance(selector, Selector):
            raise TypeError("rename_with_selector(selector, ...) expects a Selector.")
        if not callable(fn):
            raise TypeError("rename_with_selector(..., fn=...) expects a callable.")
        selected = selector.resolve(self._current_field_types)
        if not selected:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"rename_with_selector({selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        rename_map: dict[str, str] = {old: str(fn(old)) for old in selected}
        new_names = list(rename_map.values())
        if len(set(new_names)) != len(new_names):
            raise ValueError(
                "rename_with_selector(...) produced duplicate output column names."
            )
        return self.rename(rename_map, strict=strict)

    def rename_prefix(
        self,
        prefix: str,
        *,
        selector: Selector | None = None,
        strict: bool = True,
    ) -> DataFrame[Any]:
        if not isinstance(prefix, str):
            raise TypeError("rename_prefix(prefix) expects a string.")
        target = (
            list(self._current_field_types.keys())
            if selector is None
            else selector.resolve(self._current_field_types)
        )
        if selector is not None and not target:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"rename_prefix(selector={selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        rename_map = {c: f"{prefix}{c}" for c in target}
        if len(set(rename_map.values())) != len(rename_map):
            raise ValueError(
                "rename_prefix(...) produced duplicate output column names."
            )
        return self.rename(rename_map, strict=strict)

    def rename_suffix(
        self,
        suffix: str,
        *,
        selector: Selector | None = None,
        strict: bool = True,
    ) -> DataFrame[Any]:
        if not isinstance(suffix, str):
            raise TypeError("rename_suffix(suffix) expects a string.")
        target = (
            list(self._current_field_types.keys())
            if selector is None
            else selector.resolve(self._current_field_types)
        )
        if selector is not None and not target:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"rename_suffix(selector={selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        rename_map = {c: f"{c}{suffix}" for c in target}
        if len(set(rename_map.values())) != len(rename_map):
            raise ValueError(
                "rename_suffix(...) produced duplicate output column names."
            )
        return self.rename(rename_map, strict=strict)

    def rename_replace(
        self,
        old: str,
        new: str,
        *,
        selector: Selector | None = None,
        strict: bool = True,
        literal: bool = True,
    ) -> DataFrame[Any]:
        if not isinstance(old, str) or not isinstance(new, str):
            raise TypeError("rename_replace(old, new) expects strings.")
        if literal is not True:
            raise NotImplementedError(
                "rename_replace(literal=False) is not supported "
                "(schema-first rename only)."
            )
        target = (
            list(self._current_field_types.keys())
            if selector is None
            else selector.resolve(self._current_field_types)
        )
        if selector is not None and not target:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"rename_replace(selector={selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        rename_map = {c: c.replace(old, new) for c in target}
        if len(set(rename_map.values())) != len(rename_map):
            raise ValueError(
                "rename_replace(...) produced duplicate output column names."
            )
        return self.rename(rename_map, strict=strict)

    def rename_upper(
        self, selector: Selector | None = None, *, strict: bool = True
    ) -> DataFrame[Any]:
        """Uppercase column names for a subset selected by a schema-driven Selector."""
        target = (
            list(self._current_field_types.keys())
            if selector is None
            else selector.resolve(self._current_field_types)
        )
        if selector is not None and not target:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"rename_upper(selector={selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        rename_map = {c: c.upper() for c in target}
        if len(set(rename_map.values())) != len(rename_map):
            raise ValueError(
                "rename_upper(...) produced duplicate output column names."
            )
        return self.rename(rename_map, strict=strict)

    def rename_lower(
        self, selector: Selector | None = None, *, strict: bool = True
    ) -> DataFrame[Any]:
        """Lowercase column names for a subset selected by a schema-driven Selector."""
        target = (
            list(self._current_field_types.keys())
            if selector is None
            else selector.resolve(self._current_field_types)
        )
        if selector is not None and not target:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"rename_lower(selector={selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        rename_map = {c: c.lower() for c in target}
        if len(set(rename_map.values())) != len(rename_map):
            raise ValueError(
                "rename_lower(...) produced duplicate output column names."
            )
        return self.rename(rename_map, strict=strict)

    def rename_title(
        self, selector: Selector | None = None, *, strict: bool = True
    ) -> DataFrame[Any]:
        """Title-case column names for a subset selected by a schema-driven Selector."""
        target = (
            list(self._current_field_types.keys())
            if selector is None
            else selector.resolve(self._current_field_types)
        )
        if selector is not None and not target:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"rename_title(selector={selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        rename_map = {c: c.title() for c in target}
        if len(set(rename_map.values())) != len(rename_map):
            raise ValueError(
                "rename_title(...) produced duplicate output column names."
            )
        return self.rename(rename_map, strict=strict)

    def rename_strip(
        self,
        selector: Selector | None = None,
        *,
        chars: str | None = None,
        strict: bool = True,
    ) -> DataFrame[Any]:
        """Strip leading/trailing characters from column names (schema-first)."""
        if chars is not None and not isinstance(chars, str):
            raise TypeError("rename_strip(chars=...) expects a string or None.")
        target = (
            list(self._current_field_types.keys())
            if selector is None
            else selector.resolve(self._current_field_types)
        )
        if selector is not None and not target:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"rename_strip(selector={selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        rename_map = {c: c.strip(chars) for c in target}
        if len(set(rename_map.values())) != len(rename_map):
            raise ValueError(
                "rename_strip(...) produced duplicate output column names."
            )
        return self.rename(rename_map, strict=strict)

    def slice(self, offset: int, length: int) -> DataFrame[Any]:
        rust_plan = self._engine.plan_slice(self._rust_plan, int(offset), int(length))
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=self._current_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def with_row_count(
        self, name: str = "row_nr", *, offset: int = 0
    ) -> DataFrame[Any]:
        """Add a deterministic row number column (Polars-style `with_row_count`)."""
        if not isinstance(name, str) or not name:
            raise TypeError("with_row_count(name=...) expects a non-empty string.")
        rust_plan = self._engine.plan_with_row_count(
            self._rust_plan, str(name), int(offset)
        )
        desc = rust_plan.schema_descriptors()
        derived_fields = self._field_types_from_descriptors(desc)
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def head(self, n: int = 5) -> DataFrame[Any]:
        """First ``n`` rows (lazy slice).

        Materialize the result with :meth:`to_dict` or :meth:`collect`.
        **Cost:** extends the logical plan only until you materialize. See
        the EXECUTION guide (project docs) **Materialization costs**.
        """
        return self.slice(0, n)

    def limit(self, n: int = 5) -> DataFrame[Any]:
        """First ``n`` rows (Polars-style alias of :meth:`head`)."""
        return self.head(n)

    def pipe(self, fn: Any, *args: Any, **kwargs: Any) -> Any:
        """Call ``fn(self, *args, **kwargs)`` (pandas/Polars-style helper)."""
        if not callable(fn):
            raise TypeError("pipe(fn, ...) expects a callable.")
        return fn(self, *args, **kwargs)

    def clip(
        self,
        *,
        lower: Any | None = None,
        upper: Any | None = None,
        subset: str | Sequence[str] | Selector | None = None,
    ) -> DataFrame[Any]:
        """Clamp numeric columns to the given bounds (schema-first)."""
        from pydantable import selectors as _selectors
        from pydantable.expressions import Literal, when

        if lower is None and upper is None:
            raise ValueError("clip() requires at least one of lower=... or upper=....")

        if isinstance(subset, str):
            subset = [subset]
        if isinstance(subset, Selector):
            cols = subset.resolve(self._current_field_types)
            if not cols:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"clip(subset={subset!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            subset = cols

        targets = (
            list(subset)
            if subset is not None
            else _selectors.numeric().resolve(self._current_field_types)
        )
        if not targets:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"clip() matched no numeric columns. Available columns: [{available}]"
            )

        updates: dict[str, Expr] = {}
        for c in targets:
            dt = self._current_field_types.get(c)
            if dt is None:
                raise KeyError(f"clip() unknown subset column {c!r}.")
            if not _is_describe_numeric(dt):
                raise TypeError(
                    f"clip(subset=...) expects numeric columns, got {c!r} dtype={dt!r}."
                )
            expr: Expr = self.col(c)
            if lower is not None:
                lo = Literal(value=lower).cast(expr.dtype)
                expr = when(expr < lo, lo).otherwise(expr)
            if upper is not None:
                hi = Literal(value=upper).cast(expr.dtype)
                expr = when(expr > hi, hi).otherwise(expr)
            updates[c] = expr

        return self.with_columns(**updates)

    def first(self) -> DataFrame[Any]:
        """First row as a single-row DataFrame (lazy slice)."""
        return self.head(1)

    def tail(self, n: int = 5) -> DataFrame[Any]:
        """Last ``n`` rows (lazy slice). **Cost:** same idea as :meth:`head`."""
        return self.slice(-n, n)

    def last(self) -> DataFrame[Any]:
        """Last row as a single-row DataFrame (lazy slice)."""
        return self.tail(1)

    def fill_null(
        self,
        value: Any = None,
        *,
        strategy: str | None = None,
        subset: str | Sequence[str] | Selector | None = None,
    ) -> DataFrame[Any]:
        if isinstance(subset, str):
            subset = [subset]
        if isinstance(subset, Selector):
            subset_cols = subset.resolve(self._current_field_types)
            if not subset_cols:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"fill_null(subset={subset!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            subset = subset_cols
        if value is None and strategy is None:
            raise ValueError("fill_null() requires either value or strategy.")
        if value is not None and strategy is not None:
            raise ValueError("fill_null() accepts value or strategy, not both.")
        rust_plan = self._engine.plan_fill_null(
            self._rust_plan,
            None if subset is None else list(subset),
            value,
            strategy,
        )
        desc = rust_plan.schema_descriptors()
        derived_fields = self._field_types_from_descriptors(desc)
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def drop_nulls(
        self,
        subset: str | Sequence[str] | Selector | None = None,
        *,
        how: str = "any",
        threshold: int | None = None,
    ) -> DataFrame[Any]:
        if isinstance(subset, str):
            subset = [subset]
        if isinstance(subset, Selector):
            subset_cols = subset.resolve(self._current_field_types)
            if not subset_cols:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"drop_nulls(subset={subset!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            subset = subset_cols
        rust_plan = self._engine.plan_drop_nulls(
            self._rust_plan,
            None if subset is None else list(subset),
            str(how),
            threshold,
        )
        return self._from_plan(
            root_data=self._root_data,
            root_schema_type=self._root_schema_type,
            current_schema_type=self._current_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def melt(
        self,
        *,
        id_vars: str | Sequence[str] | Selector | None = None,
        value_vars: str | Sequence[str] | Selector | None = None,
        variable_name: str = "variable",
        value_name: str = "value",
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        use_streaming = _resolve_engine_streaming(
            streaming=streaming, default=self._engine_streaming_default
        )
        if isinstance(id_vars, str):
            id_vars = [id_vars]
        if isinstance(value_vars, str):
            value_vars = [value_vars]
        if isinstance(id_vars, Selector):
            resolved = id_vars.resolve(self._current_field_types)
            if not resolved:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"melt(id_vars={id_vars!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            id_vars = resolved
        if isinstance(value_vars, Selector):
            resolved = value_vars.resolve(self._current_field_types)
            if not resolved:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"melt(value_vars={value_vars!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            value_vars = resolved
        out_data, schema_descriptors = self._engine.execute_melt(
            self._rust_plan,
            self._root_data,
            [] if id_vars is None else list(id_vars),
            None if value_vars is None else list(value_vars),
            variable_name,
            value_name,
            as_python_lists=True,
            streaming=use_streaming,
        )
        derived_fields = self._field_types_from_descriptors(schema_descriptors)
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        rust_plan = self._engine.make_plan(field_types_for_rust(derived_fields))
        return self._from_plan(
            root_data=out_data,
            root_schema_type=derived_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def melt_as_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        id_vars: str | Sequence[str] | Selector | None = None,
        value_vars: str | Sequence[str] | Selector | None = None,
        variable_name: str = "variable",
        value_name: str = "value",
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.melt(
            id_vars=id_vars,
            value_vars=value_vars,
            variable_name=variable_name,
            value_name=value_name,
            streaming=streaming,
        ).as_schema(schema, validate_schema=validate_schema)

    def melt_try_as_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        id_vars: str | Sequence[str] | Selector | None = None,
        value_vars: str | Sequence[str] | Selector | None = None,
        variable_name: str = "variable",
        value_name: str = "value",
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT] | None:
        return self.melt(
            id_vars=id_vars,
            value_vars=value_vars,
            variable_name=variable_name,
            value_name=value_name,
            streaming=streaming,
        ).try_as_schema(schema, validate_schema=validate_schema)

    def melt_assert_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        id_vars: str | Sequence[str] | Selector | None = None,
        value_vars: str | Sequence[str] | Selector | None = None,
        variable_name: str = "variable",
        value_name: str = "value",
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.melt(
            id_vars=id_vars,
            value_vars=value_vars,
            variable_name=variable_name,
            value_name=value_name,
            streaming=streaming,
        ).assert_schema(schema, validate_schema=validate_schema)

    # Aliases for parity with DataFrameModel's after-model helpers.
    def melt_as_model(
        self, schema: type[AfterSchemaT], *, validate_schema: bool = True, **kwargs: Any
    ) -> DataFrame[AfterSchemaT]:
        return self.melt_as_schema(schema, validate_schema=validate_schema, **kwargs)

    def melt_try_as_model(
        self, schema: type[AfterSchemaT], *, validate_schema: bool = True, **kwargs: Any
    ) -> DataFrame[AfterSchemaT] | None:
        return self.melt_try_as_schema(
            schema, validate_schema=validate_schema, **kwargs
        )

    def melt_assert_model(
        self, schema: type[AfterSchemaT], *, validate_schema: bool = True, **kwargs: Any
    ) -> DataFrame[AfterSchemaT]:
        return self.melt_assert_schema(
            schema, validate_schema=validate_schema, **kwargs
        )

    def unpivot(
        self,
        *,
        index: str | Sequence[str] | Selector | None = None,
        on: str | Sequence[str] | Selector | None = None,
        variable_name: str = "variable",
        value_name: str = "value",
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        return self.melt(
            id_vars=index,
            value_vars=on,
            variable_name=variable_name,
            value_name=value_name,
            streaming=streaming,
        )

    def unpivot_as_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        index: str | Sequence[str] | Selector | None = None,
        on: str | Sequence[str] | Selector | None = None,
        variable_name: str = "variable",
        value_name: str = "value",
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.unpivot(
            index=index,
            on=on,
            variable_name=variable_name,
            value_name=value_name,
            streaming=streaming,
        ).as_schema(schema, validate_schema=validate_schema)

    def unpivot_try_as_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        index: str | Sequence[str] | Selector | None = None,
        on: str | Sequence[str] | Selector | None = None,
        variable_name: str = "variable",
        value_name: str = "value",
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT] | None:
        return self.unpivot(
            index=index,
            on=on,
            variable_name=variable_name,
            value_name=value_name,
            streaming=streaming,
        ).try_as_schema(schema, validate_schema=validate_schema)

    def unpivot_assert_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        index: str | Sequence[str] | Selector | None = None,
        on: str | Sequence[str] | Selector | None = None,
        variable_name: str = "variable",
        value_name: str = "value",
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.unpivot(
            index=index,
            on=on,
            variable_name=variable_name,
            value_name=value_name,
            streaming=streaming,
        ).assert_schema(schema, validate_schema=validate_schema)

    def unpivot_as_model(
        self, schema: type[AfterSchemaT], *, validate_schema: bool = True, **kwargs: Any
    ) -> DataFrame[AfterSchemaT]:
        return self.unpivot_as_schema(schema, validate_schema=validate_schema, **kwargs)

    def unpivot_try_as_model(
        self, schema: type[AfterSchemaT], *, validate_schema: bool = True, **kwargs: Any
    ) -> DataFrame[AfterSchemaT] | None:
        return self.unpivot_try_as_schema(
            schema, validate_schema=validate_schema, **kwargs
        )

    def unpivot_assert_model(
        self, schema: type[AfterSchemaT], *, validate_schema: bool = True, **kwargs: Any
    ) -> DataFrame[AfterSchemaT]:
        return self.unpivot_assert_schema(
            schema, validate_schema=validate_schema, **kwargs
        )

    def pivot_longer(
        self,
        *,
        id_vars: str | Sequence[str] | Selector | None = None,
        value_vars: str | Sequence[str] | Selector | None = None,
        names_to: str = "variable",
        values_to: str = "value",
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        """Polars-friendly alias of :meth:`melt` (aka pivot_longer)."""
        return self.melt(
            id_vars=id_vars,
            value_vars=value_vars,
            variable_name=names_to,
            value_name=values_to,
            streaming=streaming,
        )

    def pivot_wider(
        self,
        *,
        index: str | Sequence[str] | Selector,
        names_from: str | Selector | ColumnRef,
        values_from: str | Sequence[str] | Selector,
        aggregate_function: str = "first",
        sort_columns: bool = False,
        separator: str = "_",
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        """Polars-friendly alias of :meth:`pivot` (aka pivot_wider)."""
        return self.pivot(
            index=index,
            columns=names_from,
            values=values_from,
            aggregate_function=aggregate_function,
            sort_columns=sort_columns,
            separator=separator,
            streaming=streaming,
        )

    def top_k(
        self,
        n: int,
        *,
        by: str | Sequence[str],
        descending: bool = True,
        nulls_last: bool | None = None,
    ) -> DataFrame[Any]:
        """Top-k rows by sort key(s) (schema-first helper: sort then limit)."""
        keys = [by] if isinstance(by, str) else list(by)
        return self.sort(
            *keys,
            descending=[bool(descending)] * len(keys),
            nulls_last=None if nulls_last is None else [bool(nulls_last)] * len(keys),
            maintain_order=True,
        ).limit(n)

    def bottom_k(
        self,
        n: int,
        *,
        by: str | Sequence[str],
        nulls_last: bool | None = None,
    ) -> DataFrame[Any]:
        """Bottom-k rows by sort key(s) (schema-first helper: sort then limit)."""
        keys = [by] if isinstance(by, str) else list(by)
        return self.sort(
            *keys,
            descending=[False] * len(keys),
            nulls_last=None if nulls_last is None else [bool(nulls_last)] * len(keys),
            maintain_order=True,
        ).limit(n)

    def pivot(
        self,
        *,
        index: str | Sequence[str] | Selector,
        columns: str | Selector | ColumnRef,
        values: str | Sequence[str] | Selector,
        aggregate_function: str = "first",
        pivot_values: Sequence[Any] | None = None,
        sort_columns: bool = False,
        separator: str = "_",
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        if isinstance(index, Selector):
            resolved = index.resolve(self._current_field_types)
            if not resolved:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"pivot(index={index!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            index_cols = resolved
        else:
            index_cols = [index] if isinstance(index, str) else list(index)

        if isinstance(values, Selector):
            resolved = values.resolve(self._current_field_types)
            if not resolved:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"pivot(values={values!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            value_cols = resolved
        else:
            value_cols = [values] if isinstance(values, str) else list(values)

        if isinstance(columns, Selector):
            resolved = columns.resolve(self._current_field_types)
            if len(resolved) != 1:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    "pivot(columns=...) selector must match exactly one column; "
                    f"matched={resolved}. Available columns: [{available}]"
                )
            columns_col = resolved[0]
        elif isinstance(columns, str):
            columns_col = columns
        elif isinstance(columns, Expr):
            referenced = columns.referenced_columns()
            if len(referenced) != 1:
                raise TypeError(
                    "pivot(columns=...) expects a column name or "
                    "single-column ColumnRef; "
                    f"referenced_columns={sorted(referenced)!r}"
                )
            columns_col = next(iter(referenced))
        else:
            raise TypeError(
                "pivot(columns=...) expects a column name, Selector, or "
                "single-column ColumnRef."
            )
        if not isinstance(separator, str) or not separator:
            raise TypeError("pivot(separator=...) expects a non-empty string.")
        use_streaming = _resolve_engine_streaming(
            streaming=streaming, default=self._engine_streaming_default
        )
        out_data, schema_descriptors = self._engine.execute_pivot(
            self._rust_plan,
            self._root_data,
            index_cols,
            columns_col,
            value_cols,
            aggregate_function,
            pivot_values=pivot_values,
            sort_columns=bool(sort_columns),
            separator=str(separator),
            as_python_lists=True,
            streaming=use_streaming,
        )
        derived_fields = self._field_types_from_descriptors(schema_descriptors)
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        rust_plan = self._engine.make_plan(field_types_for_rust(derived_fields))
        return self._from_plan(
            root_data=out_data,
            root_schema_type=derived_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def explode(
        self,
        columns: str | Sequence[str] | Selector,
        *,
        outer: bool = False,
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        if isinstance(columns, Selector):
            cols = columns.resolve(self._current_field_types)
            if not cols:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"explode(columns={columns!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
        else:
            cols = [columns] if isinstance(columns, str) else list(columns)
        use_streaming = _resolve_engine_streaming(
            streaming=streaming, default=self._engine_streaming_default
        )
        out_data, schema_descriptors = self._engine.execute_explode(
            self._rust_plan,
            self._root_data,
            cols,
            streaming=use_streaming,
            outer=outer,
        )
        derived_fields = self._field_types_from_descriptors(schema_descriptors)
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        rust_plan = self._engine.make_plan(field_types_for_rust(derived_fields))
        return self._from_plan(
            root_data=out_data,
            root_schema_type=derived_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def explode_outer(
        self,
        columns: str | Sequence[str] | Selector,
        *,
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        """Explode lists; Spark-ish *outer* null/empty handling (see docs)."""
        return self.explode(columns, outer=True, streaming=streaming)

    def posexplode(
        self,
        column: str,
        *,
        pos: str = "pos",
        value: str | None = None,
        outer: bool = False,
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        """Explode one list column with a 0-based index (Spark ``posexplode``)."""
        if not isinstance(column, str) or not column:
            raise TypeError("posexplode() expects a non-empty str column name.")
        if not isinstance(pos, str) or not pos:
            raise TypeError("posexplode(pos=...) must be a non-empty str.")
        value_name = column if value is None else value
        if not isinstance(value_name, str) or not value_name:
            raise TypeError("posexplode(value=...) must be a non-empty str when set.")
        use_streaming = _resolve_engine_streaming(
            streaming=streaming, default=self._engine_streaming_default
        )
        out_data, schema_descriptors = self._engine.execute_posexplode(
            self._rust_plan,
            self._root_data,
            column,
            pos,
            value_name,
            streaming=use_streaming,
            outer=outer,
        )
        derived_fields = self._field_types_from_descriptors(schema_descriptors)
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        rust_plan = self._engine.make_plan(field_types_for_rust(derived_fields))
        return self._from_plan(
            root_data=out_data,
            root_schema_type=derived_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def posexplode_outer(
        self,
        column: str,
        *,
        pos: str = "pos",
        value: str | None = None,
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        """``posexplode(..., outer=True)`` alias."""
        return self.posexplode(
            column, pos=pos, value=value, outer=True, streaming=streaming
        )

    def unnest(
        self, columns: str | Sequence[str] | Selector, *, streaming: bool | None = None
    ) -> DataFrame[Any]:
        if isinstance(columns, Selector):
            cols = columns.resolve(self._current_field_types)
            if not cols:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"unnest(columns={columns!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
        else:
            cols = [columns] if isinstance(columns, str) else list(columns)
        use_streaming = _resolve_engine_streaming(
            streaming=streaming, default=self._engine_streaming_default
        )
        out_data, schema_descriptors = self._engine.execute_unnest(
            self._rust_plan, self._root_data, cols, streaming=use_streaming
        )
        derived_fields = self._field_types_from_descriptors(schema_descriptors)
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        rust_plan = self._engine.make_plan(field_types_for_rust(derived_fields))
        return self._from_plan(
            root_data=out_data,
            root_schema_type=derived_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def explode_all(self, *, streaming: bool | None = None) -> DataFrame[Any]:
        """Explode all list-typed columns (schema-driven)."""
        from pydantable import selectors as _selectors

        sel = _selectors.by_dtype(_selectors.LIST)
        matched = sel.resolve(self._current_field_types)
        if not matched:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                "explode_all() matched no list-typed columns. "
                f"Available columns: [{available}]"
            )
        return self.explode(sel, streaming=streaming)

    def unnest_all(self, *, streaming: bool | None = None) -> DataFrame[Any]:
        """Unnest all struct-typed columns (schema-driven)."""
        from pydantable import selectors as _selectors

        sel = _selectors.by_dtype(_selectors.STRUCT)
        matched = sel.resolve(self._current_field_types)
        if not matched:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                "unnest_all() matched no struct-typed columns. "
                f"Available columns: [{available}]"
            )
        return self.unnest(sel, streaming=streaming)

    def join(
        self,
        other: DataFrame[Any],
        *,
        on: str | Sequence[str] | Selector | None = None,
        left_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
        right_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
        how: str = "inner",
        suffix: str = "_right",
        coalesce: bool | None = None,
        validate: str | None = None,
        join_nulls: bool | None = None,
        maintain_order: bool | str | None = None,
        allow_parallel: bool | None = None,
        force_parallel: bool | None = None,
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        """Join two frames on key column(s); ``how`` is e.g. ``inner``, ``left``.

        ``allow_parallel`` and ``force_parallel`` match Polars' keyword names but are
        not implemented in the native engine: passing either raises
        :exc:`NotImplementedError` (see the parity scorecard in the docs).
        """
        if not isinstance(other, DataFrame):
            raise TypeError("join(other=...) expects another DataFrame.")
        if coalesce is not None and not isinstance(coalesce, bool):
            raise TypeError("join(coalesce=...) expects a bool or None.")
        if on is not None and (left_on is not None or right_on is not None):
            raise ValueError(
                "join() use either on=... or left_on=/right_on=..., not both."
            )

        def _available_cols_text(field_types: Mapping[str, Any]) -> str:
            return ", ".join(repr(c) for c in field_types)

        def _resolve_selector(
            *, sel: Selector, field_types: Mapping[str, Any], arg_name: str
        ) -> list[str]:
            matched = sel.resolve(field_types)
            if not matched:
                available = _available_cols_text(field_types)
                raise ValueError(
                    f"join({arg_name}=...) selector matched no columns. "
                    f"Available columns: [{available}]"
                )
            return matched

        def _resolve_keys(keys: str | Expr | Sequence[str | Expr] | None) -> list[str]:
            if keys is None:
                return []
            raw: list[str | Expr] = (
                [keys] if isinstance(keys, (str, Expr)) else list(keys)
            )
            out: list[str] = []
            for key in raw:
                if isinstance(key, str):
                    out.append(key)
                elif isinstance(key, Expr):
                    referenced = key.referenced_columns()
                    if len(referenced) != 1:
                        raise TypeError(
                            "join expression keys must reference exactly one column."
                        )
                    out.append(next(iter(referenced)))
                else:
                    raise TypeError(
                        "join keys must be str, Expr, or sequences thereof."
                    )
            return out

        def _base_type(tp: Any) -> Any:
            origin = get_origin(tp)
            if origin is None:
                return tp
            if origin is getattr(__import__("typing"), "Union", object()) or str(
                origin
            ).endswith("types.UnionType"):
                args = [a for a in get_args(tp) if a is not type(None)]
                if len(args) == 1:
                    return args[0]
            return tp

        if on is not None:
            if isinstance(on, Selector):
                left_keys = _resolve_selector(
                    sel=on, field_types=self._current_field_types, arg_name="on"
                )
            else:
                left_keys = [on] if isinstance(on, str) else list(on)
            missing_in_right = [
                k for k in left_keys if k not in other._current_field_types
            ]
            if missing_in_right:
                available = _available_cols_text(other._current_field_types)
                missing = ", ".join(repr(c) for c in missing_in_right)
                raise KeyError(
                    "join() unknown right join key(s): "
                    f"[{missing}]. Right available columns: [{available}]. "
                    "Hint: use left_on=.../right_on=... for differently named "
                    "key columns."
                )
            right_keys = list(left_keys)
            used_non_columnref_expr_keys = False
        else:
            used_non_columnref_expr_keys = False
            if isinstance(left_on, Selector):
                left_keys = _resolve_selector(
                    sel=left_on,
                    field_types=self._current_field_types,
                    arg_name="left_on",
                )
            else:
                left_keys = _resolve_keys(left_on)

            if isinstance(right_on, Selector):
                right_keys = _resolve_selector(
                    sel=right_on,
                    field_types=other._current_field_types,
                    arg_name="right_on",
                )
            else:
                right_keys = _resolve_keys(right_on)
            raw_left = (
                []
                if left_on is None
                else (
                    [left_on]
                    if isinstance(left_on, (str, Expr, Selector))
                    else list(left_on)
                )
            )
            raw_right = (
                []
                if right_on is None
                else (
                    [right_on]
                    if isinstance(right_on, (str, Expr, Selector))
                    else list(right_on)
                )
            )
            used_non_columnref_expr_keys = any(
                isinstance(x, Expr) and not isinstance(x, ColumnRef)
                for x in [*raw_left, *raw_right]
            )

        if validate is not None:
            v = str(validate)
            mapping = {
                "1:1": "one_to_one",
                "1:m": "one_to_many",
                "m:1": "many_to_one",
                "m:m": "many_to_many",
            }
            v = mapping.get(v, v)
            if v not in ("one_to_one", "one_to_many", "many_to_one", "many_to_many"):
                raise ValueError(
                    "join(validate=...) must be one of one_to_one, one_to_many, "
                    "many_to_one, many_to_many (or 1:1/1:m/m:1/m:m)."
                )
            if how == "cross":
                raise ValueError(
                    "cross join does not support validate=...; remove validate or "
                    "use a keyed join."
                )
            validate = v

        if join_nulls is not None and not isinstance(join_nulls, bool):
            raise TypeError("join(join_nulls=...) expects a bool or None.")
        if allow_parallel is not None or force_parallel is not None:
            # Polars join parallelism knobs are not currently exposed in the Polars
            # Rust API version pinned by pydantable-core; keep the argument surface
            # for future parity but fail explicitly for now.
            raise NotImplementedError(
                "join(allow_parallel=..., force_parallel=...) is not supported in "
                "this build."
            )

        maintain_order_norm: str | None
        if maintain_order is None:
            maintain_order_norm = None
        elif isinstance(maintain_order, bool):
            maintain_order_norm = "left" if maintain_order else "none"
        else:
            m = str(maintain_order).strip().lower()
            if m not in ("none", "left", "right"):
                raise ValueError(
                    "join(maintain_order=...) must be one of 'none', 'left', 'right', "
                    "a bool (True->'left', False->'none'), or None."
                )
            maintain_order_norm = m

        if coalesce is True:
            if how == "cross":
                raise ValueError(
                    "cross join does not support coalesce=...; remove coalesce or "
                    "use a keyed join."
                )
            if used_non_columnref_expr_keys and on is None:
                raise NotImplementedError(
                    "join(coalesce=True) is only supported for column-name keys or "
                    "simple ColumnRef expression keys."
                )
            if how in ("semi", "anti"):
                # Left-only output: coalesce has no observable effect but is accepted
                # for parity.
                pass
            elif how in ("full", "outer") and on is None and left_keys != right_keys:
                # Typed-safe subset: require exact base dtype match per key pair
                # (no casts).
                for lk, rk in zip(left_keys, right_keys, strict=True):
                    lt = self._current_field_types.get(lk)
                    rt = other._current_field_types.get(rk)
                    if lt is None or rt is None:
                        continue
                    if _base_type(lt) is not _base_type(rt):
                        raise NotImplementedError(
                            "join(coalesce=True) for full joins requires matching key "
                            "base dtypes (no casts)."
                        )

        if coalesce is False and on is None and left_keys != right_keys:
            # Typed-safe subset: only keep both key columns when it won't collide
            # with an existing left-side column name.
            for _lk, rk in zip(left_keys, right_keys, strict=True):
                if rk in self._current_field_types:
                    raise NotImplementedError(
                        "join(coalesce=False) cannot retain both key columns when the "
                        "right key name collides with an existing left column."
                    )

        if how == "cross":
            if left_keys or right_keys:
                raise ValueError("cross join does not accept on/left_on/right_on keys.")
            for name, val in (
                ("join_nulls", join_nulls),
                ("maintain_order", maintain_order),
                ("allow_parallel", allow_parallel),
                ("force_parallel", force_parallel),
            ):
                if val is not None:
                    raise ValueError(f"cross join does not support {name}=...")
        else:
            if not left_keys or not right_keys:
                raise ValueError(
                    "join() requires on=... or both left_on=... "
                    "and right_on=... for non-cross joins."
                )
            if len(left_keys) != len(right_keys):
                raise ValueError(
                    "join() left_on and right_on must have the same length."
                )

        use_streaming = _resolve_engine_streaming(
            streaming=streaming, default=self._engine_streaming_default
        )
        joined_data, schema_descriptors = self._engine.execute_join(
            self._rust_plan,
            self._root_data,
            other._rust_plan,
            other._root_data,
            left_keys,
            right_keys,
            how,
            suffix,
            validate=validate,
            coalesce=coalesce,
            join_nulls=join_nulls,
            maintain_order=maintain_order_norm,
            allow_parallel=allow_parallel,
            force_parallel=force_parallel,
            as_python_lists=True,
            streaming=use_streaming,
        )
        join_prev = previous_field_types_for_join(
            self._current_field_types,
            other._current_field_types,
            suffix=suffix,
            output_columns=list(schema_descriptors.keys()),
        )
        derived_fields = self._field_types_from_descriptors(
            schema_descriptors, previous=join_prev
        )
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, derived_fields
        )
        rust_plan = self._engine.make_plan(field_types_for_rust(derived_fields))
        return self._from_plan(
            root_data=joined_data,
            root_schema_type=derived_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def join_as_schema(
        self,
        other: DataFrame[Any],
        schema: type[AfterSchemaT],
        *,
        on: str | Sequence[str] | Selector | None = None,
        left_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
        right_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
        how: str = "inner",
        suffix: str = "_right",
        coalesce: bool | None = None,
        validate: str | None = None,
        join_nulls: bool | None = None,
        maintain_order: bool | str | None = None,
        allow_parallel: bool | None = None,
        force_parallel: bool | None = None,
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.join(
            other,
            on=on,
            left_on=left_on,
            right_on=right_on,
            how=how,
            suffix=suffix,
            coalesce=coalesce,
            validate=validate,
            join_nulls=join_nulls,
            maintain_order=maintain_order,
            allow_parallel=allow_parallel,
            force_parallel=force_parallel,
            streaming=streaming,
        ).as_schema(schema, validate_schema=validate_schema)

    def join_try_as_schema(
        self,
        other: DataFrame[Any],
        schema: type[AfterSchemaT],
        *,
        on: str | Sequence[str] | Selector | None = None,
        left_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
        right_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
        how: str = "inner",
        suffix: str = "_right",
        coalesce: bool | None = None,
        validate: str | None = None,
        join_nulls: bool | None = None,
        maintain_order: bool | str | None = None,
        allow_parallel: bool | None = None,
        force_parallel: bool | None = None,
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT] | None:
        return self.join(
            other,
            on=on,
            left_on=left_on,
            right_on=right_on,
            how=how,
            suffix=suffix,
            coalesce=coalesce,
            validate=validate,
            join_nulls=join_nulls,
            maintain_order=maintain_order,
            allow_parallel=allow_parallel,
            force_parallel=force_parallel,
            streaming=streaming,
        ).try_as_schema(schema, validate_schema=validate_schema)

    def join_assert_schema(
        self,
        other: DataFrame[Any],
        schema: type[AfterSchemaT],
        *,
        on: str | Sequence[str] | Selector | None = None,
        left_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
        right_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
        how: str = "inner",
        suffix: str = "_right",
        coalesce: bool | None = None,
        validate: str | None = None,
        join_nulls: bool | None = None,
        maintain_order: bool | str | None = None,
        allow_parallel: bool | None = None,
        force_parallel: bool | None = None,
        streaming: bool | None = None,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.join(
            other,
            on=on,
            left_on=left_on,
            right_on=right_on,
            how=how,
            suffix=suffix,
            coalesce=coalesce,
            validate=validate,
            join_nulls=join_nulls,
            maintain_order=maintain_order,
            allow_parallel=allow_parallel,
            force_parallel=force_parallel,
            streaming=streaming,
        ).assert_schema(schema, validate_schema=validate_schema)

    def join_as_model(
        self,
        other: DataFrame[Any],
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
        **kwargs: Any,
    ) -> DataFrame[AfterSchemaT]:
        return self.join_as_schema(
            other, schema, validate_schema=validate_schema, **kwargs
        )

    def join_try_as_model(
        self,
        other: DataFrame[Any],
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
        **kwargs: Any,
    ) -> DataFrame[AfterSchemaT] | None:
        return self.join_try_as_schema(
            other, schema, validate_schema=validate_schema, **kwargs
        )

    def join_assert_model(
        self,
        other: DataFrame[Any],
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
        **kwargs: Any,
    ) -> DataFrame[AfterSchemaT]:
        return self.join_assert_schema(
            other, schema, validate_schema=validate_schema, **kwargs
        )

    def group_by(
        self,
        *keys: str | ColumnRef,
        maintain_order: bool = False,
        drop_nulls: bool = True,
    ) -> GroupedDataFrame:
        """Group by key column(s); finish with :meth:`GroupedDataFrame.agg`.

        `maintain_order` / `drop_nulls` are accepted for Polars parity, but only the
        default behavior is implemented today.
        """
        selected: list[str] = []
        for key in keys:
            if isinstance(key, str):
                selected.append(key)
            elif isinstance(key, Expr):
                referenced = key.referenced_columns()
                if len(referenced) != 1:
                    raise TypeError(
                        "group_by() accepts column names or ColumnRef expressions."
                    )
                selected.append(next(iter(referenced)))
            else:
                raise TypeError(
                    "group_by() accepts column names or ColumnRef expressions."
                )
        return GroupedDataFrame(
            self,
            selected,
            maintain_order=bool(maintain_order),
            drop_nulls=bool(drop_nulls),
        )

    def rolling_agg(
        self,
        *,
        on: str,
        column: str,
        window_size: int | str,
        op: str,
        out_name: str,
        by: Sequence[str] | None = None,
        min_periods: int = 1,
    ) -> DataFrame[Any]:
        data = self.collect(as_lists=True)
        if on not in data or column not in data:
            raise KeyError("rolling_agg() requires existing on/column names.")
        by_cols = [] if by is None else list(by)
        for c in by_cols:
            if c not in data:
                raise KeyError(f"rolling_agg() unknown grouping column '{c}'.")
        n = len(data[on])
        idxs = list(range(n))
        idxs.sort(
            key=lambda i: tuple(data[c][i] for c in [*by_cols, on])  # type: ignore[misc]
        )
        out: list[Any] = [None] * n

        def _duration_seconds(v: int | str) -> float:
            if isinstance(v, int):
                return float(v)
            unit = v[-1]
            num = float(v[:-1])
            factors = {"s": 1.0, "m": 60.0, "h": 3600.0, "d": 86400.0}
            if unit not in factors:
                raise ValueError(
                    "rolling_agg(window_size=...) supports s/m/h/d suffix."
                )
            return num * factors[unit]

        def _to_seconds(x: Any) -> float:
            if isinstance(x, datetime):
                return x.timestamp()
            if isinstance(x, date):
                return float(datetime.combine(x, datetime.min.time()).timestamp())
            if isinstance(x, timedelta):
                return x.total_seconds()
            if isinstance(x, (int, float)):
                return float(x)
            raise TypeError(
                "rolling_agg(on=...) requires numeric/date/datetime/timedelta."
            )

        win_seconds = _duration_seconds(window_size)
        supported = {"sum", "mean", "min", "max", "count"}
        if op not in supported:
            raise ValueError(
                f"Unsupported rolling op '{op}'. Use one of {sorted(supported)}."
            )
        for pos, i in enumerate(idxs):
            current_group = tuple(data[c][i] for c in by_cols)
            current_t = _to_seconds(data[on][i])
            window_idxs: list[int] = []
            j = pos
            while j >= 0:
                k = idxs[j]
                if tuple(data[c][k] for c in by_cols) != current_group:
                    break
                if current_t - _to_seconds(data[on][k]) <= win_seconds:
                    window_idxs.append(k)
                    j -= 1
                else:
                    break
            vals = [
                data[column][k]
                for k in reversed(window_idxs)
                if data[column][k] is not None
            ]
            if len(vals) < min_periods:
                out[i] = None
                continue
            if op == "count":
                out[i] = len(vals)
            elif op == "sum":
                out[i] = sum(vals)
            elif op == "mean":
                out[i] = sum(vals) / len(vals) if vals else None
            elif op == "min":
                out[i] = min(vals) if vals else None
            else:
                out[i] = max(vals) if vals else None

        out_data = dict(data)
        out_data[out_name] = out
        fields = dict(self._current_field_types)
        in_dtype = self._current_field_types[column]
        if op == "count":
            fields[out_name] = int
        elif op == "mean":
            fields[out_name] = float | None
        elif op in {"sum", "min", "max"}:
            fields[out_name] = in_dtype
        else:
            fields[out_name] = in_dtype
        derived_schema_type = make_derived_schema_type(
            self._current_schema_type, fields
        )
        rust_plan = self._engine.make_plan(field_types_for_rust(fields))
        return self._from_plan(
            root_data=out_data,
            root_schema_type=derived_schema_type,
            current_schema_type=derived_schema_type,
            rust_plan=rust_plan,
            engine=self._engine,
        )

    def rolling_agg_as_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        on: str,
        column: str,
        window_size: int | str,
        op: str,
        out_name: str,
        by: Sequence[str] | None = None,
        min_periods: int = 1,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.rolling_agg(
            on=on,
            column=column,
            window_size=window_size,
            op=op,
            out_name=out_name,
            by=by,
            min_periods=min_periods,
        ).as_schema(schema, validate_schema=validate_schema)

    def rolling_agg_try_as_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        on: str,
        column: str,
        window_size: int | str,
        op: str,
        out_name: str,
        by: Sequence[str] | None = None,
        min_periods: int = 1,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT] | None:
        return self.rolling_agg(
            on=on,
            column=column,
            window_size=window_size,
            op=op,
            out_name=out_name,
            by=by,
            min_periods=min_periods,
        ).try_as_schema(schema, validate_schema=validate_schema)

    def rolling_agg_assert_schema(
        self,
        schema: type[AfterSchemaT],
        *,
        on: str,
        column: str,
        window_size: int | str,
        op: str,
        out_name: str,
        by: Sequence[str] | None = None,
        min_periods: int = 1,
        validate_schema: bool = True,
    ) -> DataFrame[AfterSchemaT]:
        return self.rolling_agg(
            on=on,
            column=column,
            window_size=window_size,
            op=op,
            out_name=out_name,
            by=by,
            min_periods=min_periods,
        ).assert_schema(schema, validate_schema=validate_schema)

    def rolling_agg_as_model(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
        **kwargs: Any,
    ) -> DataFrame[AfterSchemaT]:
        return self.rolling_agg_as_schema(
            schema, validate_schema=validate_schema, **kwargs
        )

    def rolling_agg_try_as_model(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
        **kwargs: Any,
    ) -> DataFrame[AfterSchemaT] | None:
        return self.rolling_agg_try_as_schema(
            schema, validate_schema=validate_schema, **kwargs
        )

    def rolling_agg_assert_model(
        self,
        schema: type[AfterSchemaT],
        *,
        validate_schema: bool = True,
        **kwargs: Any,
    ) -> DataFrame[AfterSchemaT]:
        return self.rolling_agg_assert_schema(
            schema, validate_schema=validate_schema, **kwargs
        )

    def group_by_dynamic(
        self,
        index_column: str,
        *,
        every: str,
        period: str | None = None,
        by: Sequence[str] | None = None,
    ) -> DynamicGroupedDataFrame:
        return DynamicGroupedDataFrame(
            self,
            index_column=index_column,
            every=every,
            period=period,
            by=[] if by is None else list(by),
        )

    def collect(
        self,
        *,
        as_lists: bool = False,
        as_numpy: bool = False,
        as_polars: bool | None = None,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
    ) -> Any:
        """
        Materialize this typed logical DataFrame.

        **Cost:** full engine execution on the current thread (same as :meth:`to_dict`
        when returning rows or lists). See the EXECUTION guide (project docs)
        **Materialization costs**.

        By default returns a list of Pydantic models, one per row, validated
        against :attr:`schema_type` (the current projected schema).

        Use ``as_lists=True`` for a columnar ``dict[str, list]``. Use
        :meth:`to_dict` as a readable alias for that shape.

        With ``as_numpy=True``, returns ``dict[str, numpy.ndarray]`` (requires
        ``numpy``).

        With ``streaming=True``, or when the environment variable
        ``PYDANTABLE_ENGINE_STREAMING`` is set to a truthy value (and
        ``streaming`` is omitted), Polars uses its streaming engine for
        ``collect`` where supported (best-effort; some plans fall back or error).

        The ``as_polars`` argument is deprecated: use :meth:`to_polars` for a
        Polars ``DataFrame`` when the optional ``polars`` package is installed.
        """
        if as_polars is not None:
            warnings.warn(
                "as_polars is deprecated and will be removed in pydantable 2.0.0; "
                "use to_polars() for a Polars DataFrame, or collect(as_lists=True) "
                "/ to_dict() for columnar dicts.",
                DeprecationWarning,
                stacklevel=2,
            )
            if as_polars:
                return self.to_polars(streaming=streaming)
            return self.to_dict(streaming=streaming)
        if as_numpy and as_lists:
            raise ValueError(
                "collect() cannot specify both as_numpy=True and as_lists=True."
            )
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        column_dict = self._materialize_columns_with_missing_optional_fallback(
            streaming=use_streaming
        )
        column_dict = _coerce_enum_columns(column_dict, self._current_field_types)
        column_dict = self._apply_io_validation_if_configured(column_dict)
        if as_lists:
            return self._column_dict_in_schema_order(column_dict)
        if as_numpy:
            import numpy as np  # type: ignore[import-not-found]

            ordered = self._column_dict_in_schema_order(column_dict)
            return {k: np.asarray(v) for k, v in ordered.items()}
        ordered = self._column_dict_in_schema_order(column_dict)
        return _rows_from_column_dict(ordered, self._current_schema_type)

    def to_dict(
        self,
        *,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
    ) -> dict[str, list[Any]]:
        """Columnar materialization (alias for ``collect(as_lists=True)`` shape).

        **Cost:** full Rust execution for the current plan. See the EXECUTION guide
        (project docs).
        Pass ``streaming=`` or set ``PYDANTABLE_ENGINE_STREAMING`` like :meth:`collect`.
        """
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        raw = self._materialize_columns_with_missing_optional_fallback(
            streaming=use_streaming
        )
        raw = _coerce_enum_columns(raw, self._current_field_types)
        raw = self._apply_io_validation_if_configured(raw)
        return self._column_dict_in_schema_order(raw)

    def null_count(self) -> dict[str, int]:
        """Count nulls per column (materializes via :meth:`to_dict`)."""
        d = self.to_dict()
        return {k: sum(v is None for v in vs) for k, vs in d.items()}

    def is_empty(self) -> bool:
        """True if the materialized result has zero rows.

        This is eager (materializes via :meth:`to_dict`).
        """
        d = self.to_dict()
        if not d:
            return True
        return len(next(iter(d.values()))) == 0

    def shift(self, periods: int = 1) -> DataFrame[Any]:
        """Shift every column by `periods` rows.

        This is eager (materializes via :meth:`to_dict`).
        """
        p = int(periods)
        d = self.to_dict()
        if not d:
            return self
        n = len(next(iter(d.values())))
        out: dict[str, list[Any]] = {}
        for k, vs in d.items():
            if p == 0:
                out[k] = list(vs)
            elif p > 0:
                out[k] = [None] * min(p, n) + list(vs[: max(0, n - p)])
            else:
                q = -p
                out[k] = list(vs[q:]) + [None] * min(q, n)
        fields = dict(self._current_field_types)
        schema_t = make_derived_schema_type(self._current_schema_type, fields)
        plan = self._engine.make_plan(field_types_for_rust(fields))
        return self._from_plan(
            root_data=out,
            root_schema_type=schema_t,
            current_schema_type=schema_t,
            rust_plan=plan,
            engine=self._engine,
        )

    def sample(
        self,
        *,
        n: int | None = None,
        fraction: float | None = None,
        seed: int | None = None,
        with_replacement: bool = False,
    ) -> DataFrame[Any]:
        """Sample rows (eager; materializes via :meth:`to_dict`)."""
        if n is not None and fraction is not None:
            raise ValueError("sample() accepts n or fraction, not both.")
        if n is None and fraction is None:
            raise ValueError("sample() requires n or fraction.")
        d = self.to_dict()
        if not d:
            return self
        row_count = len(next(iter(d.values())))
        if fraction is not None:
            f = float(fraction)
            if f < 0 or f > 1:
                raise ValueError("sample(fraction=...) must be between 0 and 1.")
            n = int(row_count * f)
        assert n is not None
        if n < 0:
            raise ValueError("sample(n=...) must be >= 0.")
        rng = random.Random(seed)
        if with_replacement:
            idxs = [rng.randrange(row_count) for _ in range(n)]
        else:
            idxs = list(range(row_count))
            rng.shuffle(idxs)
            idxs = idxs[: min(n, row_count)]
        out: dict[str, list[Any]] = {k: [vs[i] for i in idxs] for k, vs in d.items()}
        fields = dict(self._current_field_types)
        schema_t = make_derived_schema_type(self._current_schema_type, fields)
        plan = self._engine.make_plan(field_types_for_rust(fields))
        return self._from_plan(
            root_data=out,
            root_schema_type=schema_t,
            current_schema_type=schema_t,
            rust_plan=plan,
            engine=self._engine,
        )

    def write_parquet(
        self,
        path: str | Any,
        *,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        write_kwargs: dict[str, Any] | None = None,
        partition_by: tuple[str, ...] | list[str] | None = None,
        mkdir: bool = True,
    ) -> None:
        """Write this lazy plan to Parquet without a Python ``dict[str, list]``.

        Optional ``streaming=`` (or ``PYDANTABLE_ENGINE_STREAMING``) uses Polars
        streaming collect before writing, when supported.

        ``write_kwargs`` may include Polars writer options such as ``compression``,
        ``row_group_size``, ``data_page_size``, ``statistics``, ``parallel``.

        ``partition_by`` is an optional list of column names; when set, ``path`` is a
        **dataset root directory** and rows are written as hive-style
        ``col=value/.../00000000.parquet`` shards (partition columns are omitted from
        each file). Use ``mkdir=False`` only if the root directory already exists.
        """
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        self._engine.write_parquet(
            self._rust_plan,
            self._root_data,
            str(path),
            streaming=use_streaming,
            write_kwargs=write_kwargs,
            partition_by=partition_by,
            mkdir=mkdir,
        )

    def write_csv(
        self,
        path: str | Any,
        *,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        separator: str = ",",
        write_kwargs: dict[str, Any] | None = None,
    ) -> None:
        """Write this lazy plan to CSV from Rust (no Python column dict)."""
        if len(separator) != 1:
            raise ValueError("write_csv separator must be a single character.")
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        self._engine.write_csv(
            self._rust_plan,
            self._root_data,
            str(path),
            streaming=use_streaming,
            separator=ord(separator),
            write_kwargs=write_kwargs,
        )

    def write_ipc(
        self,
        path: str | Any,
        *,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        compression: str | None = None,
        write_kwargs: dict[str, Any] | None = None,
    ) -> None:
        """Write Arrow IPC file from Rust.

        ``compression`` is ``None``, ``'lz4'``, or ``'zstd'``.
        """
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        self._engine.write_ipc(
            self._rust_plan,
            self._root_data,
            str(path),
            streaming=use_streaming,
            compression=compression,
            write_kwargs=write_kwargs,
        )

    def write_ndjson(
        self,
        path: str | Any,
        *,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        write_kwargs: dict[str, Any] | None = None,
    ) -> None:
        """Write newline-delimited JSON from Rust.

        ``write_kwargs`` may include ``json_format``.
        """
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        self._engine.write_ndjson(
            self._rust_plan,
            self._root_data,
            str(path),
            streaming=use_streaming,
            write_kwargs=write_kwargs,
        )

    def collect_batches(
        self,
        *,
        batch_size: int = 65_536,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
    ) -> list[Any]:
        """Return Polars ``DataFrame`` chunks after one engine collect.

        See the EXECUTION guide (project docs).
        """
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        return self._engine.collect_batches(
            self._rust_plan,
            self._root_data,
            batch_size=batch_size,
            streaming=use_streaming,
        )

    def stream(
        self,
        *,
        batch_size: int = 65_536,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
    ) -> Iterator[dict[str, list[Any]]]:
        """Yield ``dict[str, list]`` row chunks after one full engine collect.

        Synchronous counterpart of :meth:`astream` for **sync** route handlers and
        ``StreamingResponse`` iterators that must not ``await``. Same semantics as
        :meth:`collect_batches` (full collect, then slice — not out-of-core
        streaming). Requires ``pip install 'pydantable[polars]'`` for chunk
        conversion.

        **FastAPI:** use ``stream()`` from ``def`` routes; use ``async for`` over
        ``astream()`` from ``async def`` routes when you want the event loop free
        between chunks.
        """
        try:
            importlib.import_module("polars")
        except ImportError as e:
            raise ImportError(
                "polars is required for stream(). Install with: "
                "pip install 'pydantable[polars]'"
            ) from e
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        pl_batches = self._engine.collect_batches(
            self._rust_plan,
            self._root_data,
            batch_size=batch_size,
            streaming=use_streaming,
        )
        for pl_df in pl_batches:
            yield pl_df.to_dict(as_series=False)

    def to_polars(
        self, *, streaming: bool | None = None, engine_streaming: bool | None = None
    ) -> Any:
        """
        Materialize as a Polars ``DataFrame`` (requires the optional ``polars``
        Python package: ``pip install 'pydantable[polars]'``).

        **Cost:** builds a columnar dict via the Rust engine, then a Polars frame.
        See the EXECUTION guide (project docs). Optional ``streaming=`` matches
        :meth:`collect`.
        """
        try:
            pl = importlib.import_module("polars")
        except ImportError as e:
            raise ImportError(
                "polars is required for to_polars(). Install with: "
                "pip install 'pydantable[polars]'"
            ) from e
        return pl.DataFrame(
            self.to_dict(streaming=streaming, engine_streaming=engine_streaming)
        )

    def to_arrow(
        self, *, streaming: bool | None = None, engine_streaming: bool | None = None
    ) -> Any:
        """
        Materialize as a PyArrow ``Table`` (requires the optional ``pyarrow``
        package: ``pip install 'pydantable[arrow]'``).

        This runs the same Rust execution path as :meth:`to_dict`, then builds
        Arrow arrays from Python lists—it is not a zero-copy export of internal
        buffers. **Cost:** same materialization class as :meth:`to_dict`.
        Optional ``streaming=`` matches :meth:`collect`.
        """
        try:
            pa = importlib.import_module("pyarrow")
        except ImportError as e:
            raise ImportError(
                "pyarrow is required for to_arrow(). Install with: "
                "pip install 'pydantable[arrow]'"
            ) from e
        return pa.Table.from_pydict(
            self.to_dict(streaming=streaming, engine_streaming=engine_streaming)
        )

    def __dataframe__(
        self, *, nan_as_null: bool = False, allow_copy: bool = True
    ) -> Any:
        """
        Python DataFrame Interchange Protocol export (for Streamlit and friends).

        This materializes the current plan to a PyArrow ``Table`` (same cost class as
        :meth:`to_arrow`) and then delegates to Arrow's protocol implementation.

        Requires the optional ``pyarrow`` dependency:
        ``pip install 'pydantable[arrow]'``.
        """
        table = self.to_arrow()
        # PyArrow implements the interchange protocol on Table; delegate to it.
        return table.__dataframe__(nan_as_null=nan_as_null, allow_copy=allow_copy)

    def __dataframe_consortium_standard__(self, api_version: str | None = None) -> Any:
        """
        Entry point to the Dataframe API Consortium Standard (optional).

        This is distinct from the interchange protocol (`__dataframe__`). When the
        optional `dataframe-api-compat` package is installed, this returns a
        standards-compliant wrapper object that exposes the Consortium's draft
        DataFrame API.
        """
        try:
            import dataframe_api_compat  # type: ignore[import-untyped]
        except ImportError as e:
            raise ImportError(
                "dataframe-api-compat is required for "
                "__dataframe_consortium_standard__(). "
                "Install with: pip install 'dataframe-api-compat'"
            ) from e
        try:
            import pandas as pd  # type: ignore[import-untyped]
        except ImportError as e:
            raise ImportError(
                "pandas is required for pydantable's "
                "__dataframe_consortium_standard__() implementation. "
                "Install with: pip install 'pydantable[pandas]'"
            ) from e

        pdf = pd.DataFrame(self.to_dict())
        converter = (
            dataframe_api_compat.pandas_standard.convert_to_standard_compliant_dataframe
        )
        return converter(pdf, api_version=api_version)

    async def acollect(
        self,
        *,
        as_lists: bool = False,
        as_numpy: bool = False,
        as_polars: bool | None = None,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        executor: Executor | None = None,
    ) -> Any:
        """
        Async version of :meth:`collect`: same semantics.

        When :meth:`~pydantable.engine.protocols.ExecutionEngine.has_async_execute_plan`
        is true on this frame's engine, work is awaited via a native async path
        (Rust/Tokio when using :class:`~pydantable.engine.native.NativePolarsEngine`);
        otherwise the same logic runs in :func:`asyncio.to_thread` or in ``executor``.

        Cancelling the awaiting task does **not** cancel in-flight Rust/Polars
        execution.
        """
        if as_polars is not None:
            warnings.warn(
                "as_polars is deprecated and will be removed in pydantable 2.0.0; "
                "use to_polars() for a Polars DataFrame, or collect(as_lists=True) "
                "/ to_dict() for columnar dicts.",
                DeprecationWarning,
                stacklevel=2,
            )
            if as_polars:
                return await self.ato_polars(
                    streaming=streaming,
                    engine_streaming=engine_streaming,
                    executor=executor,
                )
            return await self.ato_dict(
                streaming=streaming,
                engine_streaming=engine_streaming,
                executor=executor,
            )
        if as_numpy and as_lists:
            raise ValueError(
                "collect() cannot specify both as_numpy=True and as_lists=True."
            )
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        column_dict = await self._materialize_columns_async(
            streaming=use_streaming, executor=executor
        )
        column_dict = self._column_dict_in_schema_order(column_dict)
        if as_lists:
            return column_dict
        if as_numpy:
            import numpy as np  # type: ignore[import-not-found]

            return {k: np.asarray(v) for k, v in column_dict.items()}
        return _rows_from_column_dict(column_dict, self._current_schema_type)

    async def ato_dict(
        self,
        *,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        executor: Executor | None = None,
    ) -> dict[str, list[Any]]:
        """Async version of :meth:`to_dict` (see :meth:`acollect`)."""
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        raw = await self._materialize_columns_async(
            streaming=use_streaming, executor=executor
        )
        return self._column_dict_in_schema_order(raw)

    async def ato_polars(
        self,
        *,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        executor: Executor | None = None,
    ) -> Any:
        """
        Async version of :meth:`to_polars` (see :meth:`acollect`).

        This still materializes a columnar Python dict first, then builds the
        Polars frame—same copies as the synchronous path.
        """
        try:
            pl = importlib.import_module("polars")
        except ImportError as e:
            raise ImportError(
                "polars is required for to_polars(). Install with: "
                "pip install 'pydantable[polars]'"
            ) from e
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        d = await self._materialize_columns_async(
            streaming=use_streaming, executor=executor
        )
        return pl.DataFrame(d)

    async def ato_arrow(
        self,
        *,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        executor: Executor | None = None,
    ) -> Any:
        """
        Async version of :meth:`to_arrow` (see :meth:`acollect`).

        Same materialization and copies as the synchronous path.
        """
        try:
            pa = importlib.import_module("pyarrow")
        except ImportError as e:
            raise ImportError(
                "pyarrow is required for to_arrow(). Install with: "
                "pip install 'pydantable[arrow]'"
            ) from e
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        d = await self._materialize_columns_async(
            streaming=use_streaming, executor=executor
        )
        return pa.Table.from_pydict(d)

    def submit(
        self,
        *,
        as_lists: bool = False,
        as_numpy: bool = False,
        as_polars: bool | None = None,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        executor: Executor | None = None,
    ) -> ExecutionHandle:
        """Background :meth:`collect`; await :meth:`ExecutionHandle.result`."""

        def _run() -> Any:
            return self.collect(
                as_lists=as_lists,
                as_numpy=as_numpy,
                as_polars=as_polars,
                streaming=streaming,
                engine_streaming=engine_streaming,
            )

        if executor is not None:
            fut: concurrent.futures.Future[Any] = executor.submit(_run)
        else:
            fut = concurrent.futures.Future[Any]()

            def _bg() -> None:
                # Mirror ThreadPoolExecutor semantics: if the user cancels the
                # handle before work starts, do not execute or attempt to set
                # results on a cancelled future.
                if not fut.set_running_or_notify_cancel():
                    return
                try:
                    fut.set_result(_run())
                except Exception as e:
                    # If the future was cancelled mid-flight, avoid raising
                    # InvalidStateError from set_exception on a cancelled future.
                    if fut.cancelled():
                        return
                    fut.set_exception(e)

            threading.Thread(target=_bg, daemon=True).start()
        return ExecutionHandle(fut)

    async def astream(
        self,
        *,
        batch_size: int = 65_536,
        streaming: bool | None = None,
        engine_streaming: bool | None = None,
        executor: Executor | None = None,
    ) -> Any:
        """Yield ``dict[str, list]`` chunks after one full engine collect.

        Async counterpart of :meth:`stream` for ``async def`` handlers. See
        :meth:`stream` for semantics and FastAPI usage.
        """
        use_streaming = _resolve_engine_streaming(
            streaming=streaming,
            engine_streaming=engine_streaming,
            default=self._engine_streaming_default,
        )
        from pydantable.engine._capability import prefer_native_async_collect_batches

        if prefer_native_async_collect_batches(self._engine):
            pl_batches = await self._engine.async_collect_plan_batches(
                self._rust_plan,
                self._root_data,
                batch_size=batch_size,
                streaming=use_streaming,
            )
        else:
            pl_batches = await _materialize_in_thread(
                functools.partial(
                    self.collect_batches,
                    batch_size=batch_size,
                    streaming=streaming,
                    engine_streaming=engine_streaming,
                ),
                executor=executor,
            )
        try:
            importlib.import_module("polars")
        except ImportError as e:
            raise ImportError(
                "polars is required for astream() (Polars chunk type). Install with: "
                "pip install 'pydantable[polars]'"
            ) from e
        for pl_df in pl_batches:
            col = await _materialize_in_thread(
                functools.partial(pl_df.to_dict, as_series=False),
                executor=executor,
            )
            yield col

    def explain(
        self,
        *,
        format: Literal["text", "json"] = "text",
        streaming: bool | None = None,
    ) -> str | dict[str, Any]:
        """
        Introspect the current logical plan.

        - `format="text"` returns a compact, stable string summary.
        - `format="json"` returns a JSON-serializable `dict`.

        This is a *plan* view, not an execution trace; it does not materialize data.
        """
        from pydantable.plan import explain as _explain

        use_streaming = _resolve_engine_streaming(
            streaming=streaming, default=self._engine_streaming_default
        )
        root_kind = (
            "scan_file_root" if _is_scan_file_root(self._root_data) else "in_memory"
        )
        return _explain(
            self._rust_plan,
            format=format,
            engine_streaming=use_streaming,
            root_data_kind=root_kind,
        )

    @classmethod
    def concat(
        cls,
        dfs: Sequence[DataFrame[Any]],
        *,
        how: str = "vertical",
        streaming: bool | None = None,
    ) -> DataFrame[Any]:
        """Stack or otherwise combine two or more frames (see Rust ``how`` values)."""
        if len(dfs) < 2:
            raise ValueError("concat() requires at least two DataFrame inputs.")
        base = dfs[0]
        out_data = base._root_data
        out_schema_type = base._current_schema_type
        merged_ft = dict(base._current_field_types)
        out_plan = base._rust_plan
        use_streaming = _resolve_engine_streaming(
            streaming=streaming, default=base._engine_streaming_default
        )
        for df in dfs[1:]:
            out_data, schema_descriptors = base._engine.execute_concat(
                out_plan,
                out_data,
                df._rust_plan,
                df._root_data,
                how,
                as_python_lists=True,
                streaming=use_streaming,
            )
            derived_fields = schema_from_descriptors(schema_descriptors)
            merged_ft = merge_field_types_preserving_identity(
                merged_ft, schema_descriptors, derived_fields
            )
            out_schema_type = make_derived_schema_type(out_schema_type, merged_ft)
            out_plan = base._engine.make_plan(field_types_for_rust(merged_ft))
        return cls._from_plan(
            root_data=out_data,
            root_schema_type=out_schema_type,
            current_schema_type=out_schema_type,
            rust_plan=out_plan,
            engine=base._engine,
        )

columns property

columns

Current logical column names (schema order).

shape property

shape

(n_rows, n_cols) from the root column buffers when present.

After lazy transforms (e.g. :meth:filter) that do not replace _root_data, the row count may not match materialized output; use :meth:to_dict or :meth:collect for the true row count.

empty property

empty

True when the root buffer has zero rows (see :attr:shape).

dtypes property

dtypes

Map column name → Pydantic field annotation (not pandas dtype objects).

read_parquet classmethod

read_parquet(path, *, columns=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **scan_kwargs)

Lazy Parquet read (local file path).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def read_parquet(
    cls,
    path: str | Any,
    *,
    columns: list[str] | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **scan_kwargs: Any,
) -> DataFrame[Any]:
    """Lazy Parquet read (local file path)."""
    from ._impl_lazy_sources import read_parquet as _read_parquet

    return _read_parquet(
        cls,
        path,
        columns=columns,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **scan_kwargs,
    )

aread_parquet async classmethod

aread_parquet(path, *, columns=None, executor=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **scan_kwargs)

Async lazy Parquet read (local path).

Returns a typed lazy frame backed by a native ScanFileRoot. Ingest validation options are applied when you materialize (to_dict() / collect() / to_arrow() / to_polars()), not at scan time.

Source code in python/pydantable/dataframe/_impl.py
@classmethod
async def aread_parquet(
    cls,
    path: str | Any,
    *,
    columns: list[str] | None = None,
    executor: Executor | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **scan_kwargs: Any,
) -> DataFrame[Any]:
    """Async lazy Parquet read (local path).

    Returns a typed lazy frame backed by a native `ScanFileRoot`. Ingest
    validation options are applied when you materialize (`to_dict()` /
    `collect()` / `to_arrow()` / `to_polars()`), not at scan time.
    """
    from ._impl_lazy_sources import aread_parquet as _aread_parquet

    return await _aread_parquet(
        cls,
        path,
        columns=columns,
        executor=executor,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **scan_kwargs,
    )

read_parquet_url classmethod

read_parquet_url(url, *, experimental=True, columns=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **kwargs)

Lazy Parquet read after HTTP(S) download to a temp file.

See the DATA_IO_SOURCES guide (project docs).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def read_parquet_url(
    cls,
    url: str,
    *,
    experimental: bool = True,
    columns: list[str] | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **kwargs: Any,
) -> DataFrame[Any]:
    """Lazy Parquet read after HTTP(S) download to a temp file.

    See the DATA_IO_SOURCES guide (project docs).
    """
    from ._impl_lazy_sources import read_parquet_url as _read_parquet_url

    return _read_parquet_url(
        cls,
        url,
        experimental=experimental,
        columns=columns,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **kwargs,
    )

aread_parquet_url async classmethod

aread_parquet_url(url, *, experimental=True, columns=None, executor=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **kwargs)

Async lazy Parquet over HTTP(S) (downloads to a temp file).

Prefer DataFrameModel.aread_parquet_url_ctx / pydantable.io.aread_parquet_url_ctx for automatic temp-file cleanup. Validation options apply on materialization.

Source code in python/pydantable/dataframe/_impl.py
@classmethod
async def aread_parquet_url(
    cls,
    url: str,
    *,
    experimental: bool = True,
    columns: list[str] | None = None,
    executor: Executor | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **kwargs: Any,
) -> DataFrame[Any]:
    """Async lazy Parquet over HTTP(S) (downloads to a temp file).

    Prefer `DataFrameModel.aread_parquet_url_ctx` /
    `pydantable.io.aread_parquet_url_ctx` for automatic temp-file cleanup.
    Validation options apply on materialization.
    """
    from ._impl_lazy_sources import aread_parquet_url as _aread_parquet_url

    return await _aread_parquet_url(
        cls,
        url,
        experimental=experimental,
        columns=columns,
        executor=executor,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **kwargs,
    )

aread_csv async classmethod

aread_csv(path, *, columns=None, executor=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **scan_kwargs)

Async lazy CSV read (local path).

Validation options apply on materialization (see aread_parquet).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
async def aread_csv(
    cls,
    path: str | Any,
    *,
    columns: list[str] | None = None,
    executor: Executor | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **scan_kwargs: Any,
) -> DataFrame[Any]:
    """Async lazy CSV read (local path).

    Validation options apply on materialization (see `aread_parquet`).
    """
    from ._impl_lazy_sources import aread_csv as _aread_csv

    return await _aread_csv(
        cls,
        path,
        columns=columns,
        executor=executor,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **scan_kwargs,
    )

aread_ndjson async classmethod

aread_ndjson(path, *, columns=None, executor=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **scan_kwargs)

Async lazy NDJSON read (local path).

Validation options apply on materialization (see aread_parquet).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
async def aread_ndjson(
    cls,
    path: str | Any,
    *,
    columns: list[str] | None = None,
    executor: Executor | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **scan_kwargs: Any,
) -> DataFrame[Any]:
    """Async lazy NDJSON read (local path).

    Validation options apply on materialization (see `aread_parquet`).
    """
    from ._impl_lazy_sources import aread_ndjson as _aread_ndjson

    return await _aread_ndjson(
        cls,
        path,
        columns=columns,
        executor=executor,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **scan_kwargs,
    )

read_json classmethod

read_json(path, *, columns=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **scan_kwargs)

Lazy JSON Lines (same as :meth:read_ndjson).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def read_json(
    cls,
    path: str | Any,
    *,
    columns: list[str] | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **scan_kwargs: Any,
) -> DataFrame[Any]:
    """Lazy JSON Lines (same as :meth:`read_ndjson`)."""
    from ._impl_lazy_sources import read_json as _read_json

    return _read_json(
        cls,
        path,
        columns=columns,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **scan_kwargs,
    )

aread_json async classmethod

aread_json(path, *, columns=None, executor=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **scan_kwargs)

Async lazy JSON Lines read (local path; alias of NDJSON engine).

Validation options apply on materialization (see aread_parquet).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
async def aread_json(
    cls,
    path: str | Any,
    *,
    columns: list[str] | None = None,
    executor: Executor | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **scan_kwargs: Any,
) -> DataFrame[Any]:
    """Async lazy JSON Lines read (local path; alias of NDJSON engine).

    Validation options apply on materialization (see `aread_parquet`).
    """
    from ._impl_lazy_sources import aread_json as _aread_json

    return await _aread_json(
        cls,
        path,
        columns=columns,
        executor=executor,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **scan_kwargs,
    )

aread_ipc async classmethod

aread_ipc(path, *, columns=None, executor=None, engine_streaming=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None, **scan_kwargs)

Async lazy Arrow IPC file read (local path).

Validation options apply on materialization (see aread_parquet).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
async def aread_ipc(
    cls,
    path: str | Any,
    *,
    columns: list[str] | None = None,
    executor: Executor | None = None,
    engine_streaming: bool | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
    **scan_kwargs: Any,
) -> DataFrame[Any]:
    """Async lazy Arrow IPC file read (local path).

    Validation options apply on materialization (see `aread_parquet`).
    """
    from ._impl_lazy_sources import aread_ipc as _aread_ipc

    return await _aread_ipc(
        cls,
        path,
        columns=columns,
        executor=executor,
        engine_streaming=engine_streaming,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
        **scan_kwargs,
    )

iter_parquet classmethod

iter_parquet(path, *, batch_size=65536, columns=None, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None)

Stream Parquet as batches of in-memory typed frames (PyArrow-backed).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def iter_parquet(
    cls,
    path: str | Any,
    *,
    batch_size: int = 65_536,
    columns: list[str] | None = None,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
):
    """Stream Parquet as batches of in-memory typed frames (PyArrow-backed)."""
    from ._impl_lazy_sources import iter_parquet as _iter_parquet

    yield from _iter_parquet(
        cls,
        path,
        batch_size=batch_size,
        columns=columns,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
    )

iter_ipc classmethod

iter_ipc(source, *, batch_size=65536, as_stream=False, trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None)

Stream IPC as batches of in-memory typed frames (PyArrow-backed).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def iter_ipc(
    cls,
    source: str | Any,
    *,
    batch_size: int = 65_536,
    as_stream: bool = False,
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
):
    """Stream IPC as batches of in-memory typed frames (PyArrow-backed)."""
    from ._impl_lazy_sources import iter_ipc as _iter_ipc

    yield from _iter_ipc(
        cls,
        source,
        batch_size=batch_size,
        as_stream=as_stream,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
    )

iter_csv classmethod

iter_csv(path, *, batch_size=65536, encoding='utf-8', trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None)

Stream CSV as batches of in-memory typed frames (stdlib CSV).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def iter_csv(
    cls,
    path: str | Any,
    *,
    batch_size: int = 65_536,
    encoding: str = "utf-8",
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
):
    """Stream CSV as batches of in-memory typed frames (stdlib CSV)."""
    from ._impl_lazy_sources import iter_csv as _iter_csv

    yield from _iter_csv(
        cls,
        path,
        batch_size=batch_size,
        encoding=encoding,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
    )

iter_ndjson classmethod

iter_ndjson(path, *, batch_size=65536, encoding='utf-8', trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None)

Stream NDJSON/JSONL as batches of in-memory typed frames (stdlib json).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def iter_ndjson(
    cls,
    path: str | Any,
    *,
    batch_size: int = 65_536,
    encoding: str = "utf-8",
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
):
    """Stream NDJSON/JSONL as batches of in-memory typed frames (stdlib json)."""
    from ._impl_lazy_sources import iter_ndjson as _iter_ndjson

    yield from _iter_ndjson(
        cls,
        path,
        batch_size=batch_size,
        encoding=encoding,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
    )

iter_json_lines classmethod

iter_json_lines(path, *, batch_size=65536, encoding='utf-8', trusted_mode=None, fill_missing_optional=True, ignore_errors=False, on_validation_errors=None)

Stream JSON Lines as batches of in-memory typed frames.

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def iter_json_lines(
    cls,
    path: str | Any,
    *,
    batch_size: int = 65_536,
    encoding: str = "utf-8",
    trusted_mode: Literal["off", "shape_only", "strict"] | None = None,
    fill_missing_optional: bool = True,
    ignore_errors: bool = False,
    on_validation_errors: Callable[[list[dict[str, Any]]], None] | None = None,
):
    """Stream JSON Lines as batches of in-memory typed frames."""
    from ._impl_lazy_sources import iter_json_lines as _iter_json_lines

    yield from _iter_json_lines(
        cls,
        path,
        batch_size=batch_size,
        encoding=encoding,
        trusted_mode=trusted_mode,
        fill_missing_optional=fill_missing_optional,
        ignore_errors=ignore_errors,
        on_validation_errors=on_validation_errors,
    )

info

info()

Multi-line summary string (schema, dtypes, :attr:shape caveat).

Cost: does not materialize row data; uses schema and root-buffer :attr:shape only. See the EXECUTION guide (project docs) Materialization costs.

Source code in python/pydantable/dataframe/_impl.py
def info(self) -> str:
    """Multi-line summary string (schema, dtypes, :attr:`shape` caveat).

    **Cost:** does not materialize row data; uses schema and root-buffer
    :attr:`shape` only. See the EXECUTION guide (project docs)
    **Materialization costs**.
    """
    schema_qn = getattr(
        self._current_schema_type,
        "__qualname__",
        self._current_schema_type.__name__,
    )
    lines = [
        f"{self.__class__.__name__}",
        f"  schema: {schema_qn}",
        f"  columns: {len(self._current_field_types)}",
        f"  shape (root buffer): {self.shape[0]} x {self.shape[1]}",
        "  Note: after lazy transforms (e.g. filter), root row count may not "
        "match materialized rows; use to_dict() or collect() for true count.",
        "",
        "dtypes:",
    ]
    for name, ann in self._current_field_types.items():
        lines.append(f"  {name}: {_dtype_repr(ann)}")
    return "\n".join(lines)

describe

describe()

Summary statistics for int, float, bool, str, and date/datetime columns.

Cost: one full :meth:to_dict() materialization. String columns n_unique scans all non-null strings. See the EXECUTION guide (project docs).

Source code in python/pydantable/dataframe/_impl.py
def describe(self) -> str:
    """Summary statistics for int, float, bool, str, and date/datetime columns.

    **Cost:** one full :meth:`to_dict()` materialization. String columns
    ``n_unique`` scans all non-null strings. See the EXECUTION guide (project docs).
    """
    numeric = [
        n for n, a in self._current_field_types.items() if _is_describe_numeric(a)
    ]
    bool_cols = [
        n for n, a in self._current_field_types.items() if _is_describe_bool(a)
    ]
    str_cols = [
        n for n, a in self._current_field_types.items() if _is_describe_str(a)
    ]
    temporal_cols = [
        n for n, a in self._current_field_types.items() if _is_describe_temporal(a)
    ]
    if not numeric and not bool_cols and not str_cols and not temporal_cols:
        return "describe(): no int/float/bool/str/date/datetime columns in schema."
    data = self.to_dict()
    lines = [
        "describe() — one to_dict(); int/float/bool/str/date/datetime columns.",
        "",
    ]
    for name in numeric:
        col = data[name]
        vals = [x for x in col if x is not None]
        c = len(vals)
        if c == 0:
            lines.append(f"{name}: count=0 (all null)")
            continue
        mean_v = statistics.mean(vals)
        mn, mx = min(vals), max(vals)
        if c >= 2:
            std_v = statistics.stdev(vals)
            extra = ""
            if c >= 4:
                try:
                    import numpy as np  # type: ignore[import-not-found]

                    arr = np.asarray(vals, dtype=float)
                    m = float(arr.mean())
                    s = float(arr.std(ddof=1))
                    if s > 0:
                        skew_v = float(np.mean(((arr - m) / s) ** 3))
                        kurt_v = float(np.mean(((arr - m) / s) ** 4) - 3.0)
                        sem_v = s / (c**0.5)
                        extra = (
                            f" skew={skew_v:.6g} kurtosis={kurt_v:.6g} "
                            f"sem={sem_v:.6g}"
                        )
                except ImportError:
                    pass
            lines.append(
                f"{name}: count={c} mean={mean_v:.6g} std={std_v:.6g} "
                f"min={mn} max={mx}{extra}"
            )
        else:
            lines.append(f"{name}: count={c} mean={mean_v:.6g} min={mn} max={mx}")
    for name in bool_cols:
        col = data[name]
        non_null = [x for x in col if x is not None]
        n_null = len(col) - len(non_null)
        nt = sum(1 for x in non_null if x is True)
        nf = sum(1 for x in non_null if x is False)
        lines.append(
            f"{name}: count={len(non_null)} true={nt} false={nf} null={n_null}"
        )
    for name in str_cols:
        col = data[name]
        raw = [x for x in col if x is not None]
        str_vals = [x for x in raw if isinstance(x, str)]
        n_null = len(col) - len(raw)
        if not str_vals:
            lines.append(f"{name}: count=0 (all null)")
            continue
        n_unique = len(set(str_vals))
        lens = [len(s) for s in str_vals]
        lines.append(
            f"{name}: count={len(str_vals)} n_unique={n_unique} "
            f"min_len={min(lens)} max_len={max(lens)} null={n_null}"
        )
    for name in temporal_cols:
        col = data[name]
        raw = [x for x in col if x is not None]
        n_null = len(col) - len(raw)
        if not raw:
            lines.append(f"{name}: count=0 (all null)")
            continue
        vals = [x for x in raw if isinstance(x, (date, datetime))]
        if not vals:
            lines.append(f"{name}: count=0 (all null)")
            continue
        mn, mx = min(vals), max(vals)
        lines.append(
            f"{name}: count={len(vals)} min={mn!s} max={mx!s} null={n_null}"
        )
    return "\n".join(lines)

value_counts

value_counts(column, *, normalize=False, dropna=True)

Count rows per distinct value in column (group-by aggregation).

Cost: engine aggregation (same path as :meth:group_by / :meth:agg). See the EXECUTION guide (project docs).

Returns a dict sorted by count descending, then repr(key). When normalize=True, values are fractions in [0, 1] (float).

Source code in python/pydantable/dataframe/_impl.py
def value_counts(
    self,
    column: str,
    *,
    normalize: bool = False,
    dropna: bool = True,
) -> dict[Any, int | float]:
    """Count rows per distinct value in ``column`` (group-by aggregation).

    **Cost:** engine aggregation (same path as :meth:`group_by` / :meth:`agg`).
    See the EXECUTION guide (project docs).

    Returns a dict sorted by count descending, then ``repr(key)``. When
    ``normalize=True``, values are fractions in ``[0, 1]`` (``float``).
    """
    if column not in self._current_field_types:
        raise KeyError(f"Unknown column {column!r} for current schema.")
    out_name = "__pydantable_vc_n"
    g = self.group_by(column, drop_nulls=bool(dropna)).agg(
        streaming=None,
        **{out_name: ("count", column)},
    )
    d = g.to_dict()
    keys = d[column]
    counts = d[out_name]
    result: dict[Any, int] = {}
    for k, c in zip(keys, counts, strict=True):
        if dropna and k is None:
            continue
        result[k] = int(c) if c is not None else 0
    total = sum(result.values())
    if normalize:
        if total == 0:
            return {k: 0.0 for k in result}
        return {k: v / total for k, v in result.items()}
    return dict(sorted(result.items(), key=lambda kv: (-kv[1], repr(kv[0]))))

with_columns

with_columns(*exprs, **new_columns)

Add or replace columns.

Values are :class:~pydantable.expressions.Expr or plain literals.

Source code in python/pydantable/dataframe/_impl.py
def with_columns(self, *exprs: Any, **new_columns: Expr | Any) -> DataFrame[Any]:
    """Add or replace columns.

    Values are :class:`~pydantable.expressions.Expr` or plain literals.
    """
    from ._ops import plan_with_columns

    return plan_with_columns(self, *exprs, **new_columns)

with_columns_cast

with_columns_cast(selector, dtype, *, strict=True)

Cast columns selected by a schema-driven Selector.

Source code in python/pydantable/dataframe/_impl.py
def with_columns_cast(
    self, selector: Selector, dtype: Any, *, strict: bool = True
) -> DataFrame[Any]:
    """Cast columns selected by a schema-driven Selector."""
    if not isinstance(selector, Selector):
        raise TypeError("with_columns_cast(selector=...) expects a Selector.")
    selected = selector.resolve(self._current_field_types)
    if not selected:
        if strict:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"with_columns_cast({selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        return self
    updates: dict[str, Expr] = {c: self.col(c).cast(dtype) for c in selected}
    return self.with_columns(**updates)

with_columns_fill_null

with_columns_fill_null(selector, *, value=None, strategy=None, strict=True)

Fill nulls for columns selected by a schema-driven Selector.

Source code in python/pydantable/dataframe/_impl.py
def with_columns_fill_null(
    self,
    selector: Selector,
    *,
    value: Any = None,
    strategy: str | None = None,
    strict: bool = True,
) -> DataFrame[Any]:
    """Fill nulls for columns selected by a schema-driven Selector."""
    if not isinstance(selector, Selector):
        raise TypeError("with_columns_fill_null(selector=...) expects a Selector.")
    selected = selector.resolve(self._current_field_types)
    if not selected:
        if strict:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"with_columns_fill_null({selector!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        return self
    return self.fill_null(value, strategy=strategy, subset=selected)

select_schema

select_schema(selector)

Project columns using a schema-first Selector (explicit helper).

Source code in python/pydantable/dataframe/_impl.py
def select_schema(self, selector: Selector) -> DataFrame[Any]:
    """Project columns using a schema-first Selector (explicit helper)."""
    if not isinstance(selector, Selector):
        raise TypeError("select_schema(selector) expects a Selector.")
    return self.select(selector)

select

select(*cols, exclude=None, **named)

Project columns and/or compute a single-row frame of global aggregates.

Positional arguments: base column names, single-column refs, or globals such as :func:~pydantable.expressions.global_sum. Keyword arguments are only for named global aggregates. Plain projections and globals cannot be mixed.

Source code in python/pydantable/dataframe/_impl.py
def select(
    self,
    *cols: str | ColumnRef | Expr | AliasedExpr | Selector,
    exclude: Selector | Sequence[str] | None = None,
    **named: Any,
) -> DataFrame[Any]:
    """Project columns and/or compute a **single-row** frame of global aggregates.

    Positional arguments: base column names, single-column refs, or globals such
    as :func:`~pydantable.expressions.global_sum`. Keyword arguments are only for
    named global aggregates. Plain projections and globals cannot be mixed.
    """
    named_items: list[tuple[str, Any]] = []
    for name, e in named.items():
        if not isinstance(e, Expr):
            raise TypeError(
                "select() keyword arguments must be Expr instances "
                "(global aggregates)."
            )
        named_items.append((name, e._rust_expr))

    exclude_set: set[str] = set()
    if exclude is not None:
        if isinstance(exclude, Selector):
            exclude_set = set(exclude.resolve(self._current_field_types))
        else:
            exclude_set = {str(c) for c in exclude}

    aggs: list[tuple[str, Any]] = []
    projects: list[str] = []
    computed: dict[str, Any] = {}
    for col in cols:
        if isinstance(col, str):
            projects.append(col)
        elif isinstance(col, Selector):
            resolved = col.resolve(self._current_field_types)
            if not resolved:
                available = ", ".join(repr(c) for c in self._current_field_types)
                raise ValueError(
                    f"select({col!r}) matched no columns. "
                    f"Available columns: [{available}]"
                )
            projects.extend(resolved)
        elif isinstance(col, AliasedExpr):
            if not isinstance(col.expr, Expr):
                raise TypeError("select(AliasedExpr) expects an Expr.")
            computed[col.name] = col.expr._rust_expr
            projects.append(col.name)
        elif isinstance(col, Expr):
            if self._engine.expr_is_global_agg(col._rust_expr):
                alias = self._engine.expr_global_default_alias(col._rust_expr)
                if alias is None:
                    raise TypeError(
                        "global aggregate in select() is missing a default "
                        "output name."
                    )
                aggs.append((alias, col._rust_expr))
            else:
                if isinstance(col, ColumnRef):
                    projects.append(col._column_name)  # type: ignore[attr-defined]
                else:
                    raise TypeError(
                        "select() accepts column names, ColumnRef expressions, "
                        "global aggregates, or Expr.alias('name') for computed "
                        "expressions."
                    )
        else:
            raise TypeError(
                "select() accepts column names, Selector objects, "
                "ColumnRef expressions, global aggregates, or "
                "Expr.alias('name') (AliasedExpr)."
            )

    if named_items and (projects or aggs):
        raise TypeError(
            "select() cannot mix keyword aggregates with positional column "
            "names or aggregates."
        )
    if aggs and projects:
        raise TypeError(
            "select() cannot mix global aggregates with plain column projections."
        )
    if exclude_set and (named_items or aggs):
        raise TypeError(
            "select(exclude=...) cannot be used with global aggregates."
        )
    if named_items:
        rust_plan = self._engine.plan_global_select(self._rust_plan, named_items)
    elif aggs:
        rust_plan = self._engine.plan_global_select(self._rust_plan, aggs)
    else:
        if not projects:
            if not exclude_set:
                raise ValueError("select() requires at least one column.")
            projects = [
                c for c in self._current_field_types if c not in exclude_set
            ]
        else:
            projects = [c for c in projects if c not in exclude_set]
        if not projects:
            raise ValueError("select(...) produced an empty projection.")
        rust_plan = self._rust_plan
        if computed:
            rust_plan = self._engine.plan_with_columns(rust_plan, computed)
        rust_plan = self._engine.plan_select(rust_plan, projects)
    desc = rust_plan.schema_descriptors()
    derived_fields = self._field_types_from_descriptors(desc)
    # Preserve order: for plain projections, match call order.
    if projects and not aggs and not named_items:
        desired_order = [c for c in projects if c in derived_fields]
        ordered: dict[str, Any] = {k: derived_fields[k] for k in desired_order}
        for k, v in derived_fields.items():
            if k not in ordered:
                ordered[k] = v
        derived_fields = ordered
    derived_schema_type = make_derived_schema_type(
        self._current_schema_type, derived_fields
    )
    return self._from_plan(
        root_data=self._root_data,
        root_schema_type=self._root_schema_type,
        current_schema_type=derived_schema_type,
        rust_plan=rust_plan,
        engine=self._engine,
    )

select_all

select_all()

Select all columns in schema order (schema-driven helper).

Source code in python/pydantable/dataframe/_impl.py
def select_all(self) -> DataFrame[Any]:
    """Select all columns in schema order (schema-driven helper)."""
    return self.select(*list(self._current_field_types.keys()))

select_prefix

select_prefix(prefix)

Select columns whose names start with prefix (schema-driven helper).

Source code in python/pydantable/dataframe/_impl.py
def select_prefix(self, prefix: str) -> DataFrame[Any]:
    """Select columns whose names start with `prefix` (schema-driven helper)."""
    if not isinstance(prefix, str):
        raise TypeError("select_prefix(prefix) expects a string.")
    cols = [c for c in self._current_field_types if c.startswith(prefix)]
    if not cols:
        raise ValueError(f"select_prefix({prefix!r}) matched no columns.")
    return self.select(*cols)

select_suffix

select_suffix(suffix)

Select columns whose names end with suffix (schema-driven helper).

Source code in python/pydantable/dataframe/_impl.py
def select_suffix(self, suffix: str) -> DataFrame[Any]:
    """Select columns whose names end with `suffix` (schema-driven helper)."""
    if not isinstance(suffix, str):
        raise TypeError("select_suffix(suffix) expects a string.")
    cols = [c for c in self._current_field_types if c.endswith(suffix)]
    if not cols:
        raise ValueError(f"select_suffix({suffix!r}) matched no columns.")
    return self.select(*cols)

reorder_columns

reorder_columns(order)

Reorder columns by explicit names/selectors; append remaining columns.

Source code in python/pydantable/dataframe/_impl.py
def reorder_columns(self, order: Sequence[str | Selector]) -> DataFrame[Any]:
    """Reorder columns by explicit names/selectors; append remaining columns."""
    wanted: list[str] = []
    for it in order:
        wanted.extend(self._resolve_column_names_or_selector(it, arg_name="order"))
    if len(set(wanted)) != len(wanted):
        raise ValueError("reorder_columns(order=...) contains duplicate columns.")
    remainder = [c for c in self._current_field_types if c not in wanted]
    return self.select(*wanted, *remainder)

select_first

select_first(*cols_or_selectors)

Move selected columns to the front; keep remaining order.

Source code in python/pydantable/dataframe/_impl.py
def select_first(self, *cols_or_selectors: str | Selector) -> DataFrame[Any]:
    """Move selected columns to the front; keep remaining order."""
    wanted: list[str] = []
    for it in cols_or_selectors:
        wanted.extend(self._resolve_column_names_or_selector(it, arg_name="cols"))
    if len(set(wanted)) != len(wanted):
        raise ValueError("select_first(...) contains duplicate columns.")
    remainder = [c for c in self._current_field_types if c not in wanted]
    return self.select(*wanted, *remainder)

select_last

select_last(*cols_or_selectors)

Move selected columns to the end; keep remaining order.

Source code in python/pydantable/dataframe/_impl.py
def select_last(self, *cols_or_selectors: str | Selector) -> DataFrame[Any]:
    """Move selected columns to the end; keep remaining order."""
    wanted: list[str] = []
    for it in cols_or_selectors:
        wanted.extend(self._resolve_column_names_or_selector(it, arg_name="cols"))
    if len(set(wanted)) != len(wanted):
        raise ValueError("select_last(...) contains duplicate columns.")
    remainder = [c for c in self._current_field_types if c not in wanted]
    return self.select(*remainder, *wanted)

move

move(cols_or_selector, *, before=None, after=None)

Move column(s) before/after an anchor column (schema-first helper).

Source code in python/pydantable/dataframe/_impl.py
def move(
    self,
    cols_or_selector: str | Selector,
    *,
    before: str | None = None,
    after: str | None = None,
) -> DataFrame[Any]:
    """Move column(s) before/after an anchor column (schema-first helper)."""
    if (before is None) == (after is None):
        raise TypeError(
            "move(..., before=...) or move(..., after=...) is required."
        )
    moving = self._resolve_column_names_or_selector(
        cols_or_selector, arg_name="cols"
    )
    anchor = before if before is not None else after
    if not isinstance(anchor, str):
        raise TypeError("move(..., before/after=...) expects a column name.")
    if anchor not in self._current_field_types:
        raise KeyError(f"move() unknown anchor column {anchor!r}.")
    if anchor in moving:
        raise ValueError("move() cannot move a column relative to itself.")
    # Remove moving cols, then re-insert as a block.
    base = [c for c in self._current_field_types if c not in moving]
    idx = base.index(anchor)
    insert_at = idx if before is not None else idx + 1
    new_order = [*base[:insert_at], *moving, *base[insert_at:]]
    return self.select(*new_order)

filter

filter(condition)

Keep rows where the boolean condition is true.

Source code in python/pydantable/dataframe/_impl.py
def filter(self, condition: Expr) -> DataFrame[Any]:
    """Keep rows where the boolean ``condition`` is true."""
    from ._ops import plan_filter

    return plan_filter(self, condition)

sort

sort(*by, descending=False, nulls_last=None, maintain_order=False)

Sort by one or more columns (names or single-column expressions).

Source code in python/pydantable/dataframe/_impl.py
def sort(
    self,
    *by: str | ColumnRef,
    descending: bool | Sequence[bool] = False,
    nulls_last: bool | Sequence[bool] | None = None,
    maintain_order: bool = False,
) -> DataFrame[Any]:
    """Sort by one or more columns (names or single-column expressions)."""
    keys: list[str] = []
    for key in by:
        if isinstance(key, str):
            keys.append(key)
        elif isinstance(key, Expr):
            referenced = key.referenced_columns()
            if len(referenced) != 1:
                raise TypeError(
                    "sort() accepts column names or a ColumnRef expression."
                )
            keys.append(next(iter(referenced)))
        else:
            raise TypeError("sort() accepts column names or ColumnRef objects.")

    desc = (
        [descending] * len(keys)
        if isinstance(descending, bool)
        else list(descending)
    )
    if len(desc) != len(keys):
        raise ValueError("sort(descending=...) length must match sort keys.")
    if nulls_last is None:
        nl = []
    elif isinstance(nulls_last, bool):
        nl = [nulls_last] * len(keys)
    else:
        nl = list(nulls_last)
    if nl and len(nl) != len(keys):
        raise ValueError("sort(nulls_last=...) length must match sort keys.")
    rust_plan = self._engine.plan_sort(
        self._rust_plan, keys, desc, nl, bool(maintain_order)
    )
    return self._from_plan(
        root_data=self._root_data,
        root_schema_type=self._root_schema_type,
        current_schema_type=self._current_schema_type,
        rust_plan=rust_plan,
        engine=self._engine,
    )

duplicated

duplicated(subset=None, *, keep='first')

Single-column boolean frame duplicated (row-wise duplicate mask).

Source code in python/pydantable/dataframe/_impl.py
def duplicated(
    self,
    subset: Sequence[str] | None = None,
    *,
    keep: str | bool = "first",
) -> DataFrame[Any]:
    """Single-column boolean frame ``duplicated`` (row-wise duplicate mask)."""
    if keep is True:
        raise ValueError("duplicated(keep=True) is invalid; use 'first' or 'last'.")
    keep_s = "none" if keep is False else str(keep)
    if keep_s not in ("first", "last", "none"):
        raise ValueError(
            "duplicated(keep=...) must be 'first', 'last', or False "
            "(pandas parity)."
        )
    rust_plan = self._engine.plan_duplicate_mask(
        self._rust_plan,
        None if subset is None else list(subset),
        keep_s,
    )
    desc = rust_plan.schema_descriptors()
    derived_fields = self._field_types_from_descriptors(desc)
    derived_schema_type = make_derived_schema_type(
        self._current_schema_type, derived_fields
    )
    return self._from_plan(
        root_data=self._root_data,
        root_schema_type=self._root_schema_type,
        current_schema_type=derived_schema_type,
        rust_plan=rust_plan,
        engine=self._engine,
    )

drop_duplicate_groups

drop_duplicate_groups(subset=None)

Drop rows whose key appears in a duplicate group.

subset selects key columns; if omitted, all columns participate. Same filter as pandas drop_duplicates(keep=False).

Source code in python/pydantable/dataframe/_impl.py
def drop_duplicate_groups(
    self,
    subset: Sequence[str] | None = None,
) -> DataFrame[Any]:
    """Drop rows whose key appears in a duplicate group.

    ``subset`` selects key columns; if omitted, all columns participate.
    Same filter as pandas ``drop_duplicates(keep=False)``.
    """
    rust_plan = self._engine.plan_drop_duplicate_groups(
        self._rust_plan,
        None if subset is None else list(subset),
    )
    return self._from_plan(
        root_data=self._root_data,
        root_schema_type=self._root_schema_type,
        current_schema_type=self._current_schema_type,
        rust_plan=rust_plan,
        engine=self._engine,
    )

rename_with_selector

rename_with_selector(selector, fn, *, strict=True)

Rename columns selected by a schema-driven Selector.

Source code in python/pydantable/dataframe/_impl.py
def rename_with_selector(
    self,
    selector: Selector,
    fn: Callable[[str], str],
    *,
    strict: bool = True,
) -> DataFrame[Any]:
    """Rename columns selected by a schema-driven Selector."""
    if not isinstance(selector, Selector):
        raise TypeError("rename_with_selector(selector, ...) expects a Selector.")
    if not callable(fn):
        raise TypeError("rename_with_selector(..., fn=...) expects a callable.")
    selected = selector.resolve(self._current_field_types)
    if not selected:
        available = ", ".join(repr(c) for c in self._current_field_types)
        raise ValueError(
            f"rename_with_selector({selector!r}) matched no columns. "
            f"Available columns: [{available}]"
        )
    rename_map: dict[str, str] = {old: str(fn(old)) for old in selected}
    new_names = list(rename_map.values())
    if len(set(new_names)) != len(new_names):
        raise ValueError(
            "rename_with_selector(...) produced duplicate output column names."
        )
    return self.rename(rename_map, strict=strict)

rename_upper

rename_upper(selector=None, *, strict=True)

Uppercase column names for a subset selected by a schema-driven Selector.

Source code in python/pydantable/dataframe/_impl.py
def rename_upper(
    self, selector: Selector | None = None, *, strict: bool = True
) -> DataFrame[Any]:
    """Uppercase column names for a subset selected by a schema-driven Selector."""
    target = (
        list(self._current_field_types.keys())
        if selector is None
        else selector.resolve(self._current_field_types)
    )
    if selector is not None and not target:
        available = ", ".join(repr(c) for c in self._current_field_types)
        raise ValueError(
            f"rename_upper(selector={selector!r}) matched no columns. "
            f"Available columns: [{available}]"
        )
    rename_map = {c: c.upper() for c in target}
    if len(set(rename_map.values())) != len(rename_map):
        raise ValueError(
            "rename_upper(...) produced duplicate output column names."
        )
    return self.rename(rename_map, strict=strict)

rename_lower

rename_lower(selector=None, *, strict=True)

Lowercase column names for a subset selected by a schema-driven Selector.

Source code in python/pydantable/dataframe/_impl.py
def rename_lower(
    self, selector: Selector | None = None, *, strict: bool = True
) -> DataFrame[Any]:
    """Lowercase column names for a subset selected by a schema-driven Selector."""
    target = (
        list(self._current_field_types.keys())
        if selector is None
        else selector.resolve(self._current_field_types)
    )
    if selector is not None and not target:
        available = ", ".join(repr(c) for c in self._current_field_types)
        raise ValueError(
            f"rename_lower(selector={selector!r}) matched no columns. "
            f"Available columns: [{available}]"
        )
    rename_map = {c: c.lower() for c in target}
    if len(set(rename_map.values())) != len(rename_map):
        raise ValueError(
            "rename_lower(...) produced duplicate output column names."
        )
    return self.rename(rename_map, strict=strict)

rename_title

rename_title(selector=None, *, strict=True)

Title-case column names for a subset selected by a schema-driven Selector.

Source code in python/pydantable/dataframe/_impl.py
def rename_title(
    self, selector: Selector | None = None, *, strict: bool = True
) -> DataFrame[Any]:
    """Title-case column names for a subset selected by a schema-driven Selector."""
    target = (
        list(self._current_field_types.keys())
        if selector is None
        else selector.resolve(self._current_field_types)
    )
    if selector is not None and not target:
        available = ", ".join(repr(c) for c in self._current_field_types)
        raise ValueError(
            f"rename_title(selector={selector!r}) matched no columns. "
            f"Available columns: [{available}]"
        )
    rename_map = {c: c.title() for c in target}
    if len(set(rename_map.values())) != len(rename_map):
        raise ValueError(
            "rename_title(...) produced duplicate output column names."
        )
    return self.rename(rename_map, strict=strict)

rename_strip

rename_strip(selector=None, *, chars=None, strict=True)

Strip leading/trailing characters from column names (schema-first).

Source code in python/pydantable/dataframe/_impl.py
def rename_strip(
    self,
    selector: Selector | None = None,
    *,
    chars: str | None = None,
    strict: bool = True,
) -> DataFrame[Any]:
    """Strip leading/trailing characters from column names (schema-first)."""
    if chars is not None and not isinstance(chars, str):
        raise TypeError("rename_strip(chars=...) expects a string or None.")
    target = (
        list(self._current_field_types.keys())
        if selector is None
        else selector.resolve(self._current_field_types)
    )
    if selector is not None and not target:
        available = ", ".join(repr(c) for c in self._current_field_types)
        raise ValueError(
            f"rename_strip(selector={selector!r}) matched no columns. "
            f"Available columns: [{available}]"
        )
    rename_map = {c: c.strip(chars) for c in target}
    if len(set(rename_map.values())) != len(rename_map):
        raise ValueError(
            "rename_strip(...) produced duplicate output column names."
        )
    return self.rename(rename_map, strict=strict)

with_row_count

with_row_count(name='row_nr', *, offset=0)

Add a deterministic row number column (Polars-style with_row_count).

Source code in python/pydantable/dataframe/_impl.py
def with_row_count(
    self, name: str = "row_nr", *, offset: int = 0
) -> DataFrame[Any]:
    """Add a deterministic row number column (Polars-style `with_row_count`)."""
    if not isinstance(name, str) or not name:
        raise TypeError("with_row_count(name=...) expects a non-empty string.")
    rust_plan = self._engine.plan_with_row_count(
        self._rust_plan, str(name), int(offset)
    )
    desc = rust_plan.schema_descriptors()
    derived_fields = self._field_types_from_descriptors(desc)
    derived_schema_type = make_derived_schema_type(
        self._current_schema_type, derived_fields
    )
    return self._from_plan(
        root_data=self._root_data,
        root_schema_type=self._root_schema_type,
        current_schema_type=derived_schema_type,
        rust_plan=rust_plan,
        engine=self._engine,
    )

head

head(n=5)

First n rows (lazy slice).

Materialize the result with :meth:to_dict or :meth:collect. Cost: extends the logical plan only until you materialize. See the EXECUTION guide (project docs) Materialization costs.

Source code in python/pydantable/dataframe/_impl.py
def head(self, n: int = 5) -> DataFrame[Any]:
    """First ``n`` rows (lazy slice).

    Materialize the result with :meth:`to_dict` or :meth:`collect`.
    **Cost:** extends the logical plan only until you materialize. See
    the EXECUTION guide (project docs) **Materialization costs**.
    """
    return self.slice(0, n)

limit

limit(n=5)

First n rows (Polars-style alias of :meth:head).

Source code in python/pydantable/dataframe/_impl.py
def limit(self, n: int = 5) -> DataFrame[Any]:
    """First ``n`` rows (Polars-style alias of :meth:`head`)."""
    return self.head(n)

pipe

pipe(fn, *args, **kwargs)

Call fn(self, *args, **kwargs) (pandas/Polars-style helper).

Source code in python/pydantable/dataframe/_impl.py
def pipe(self, fn: Any, *args: Any, **kwargs: Any) -> Any:
    """Call ``fn(self, *args, **kwargs)`` (pandas/Polars-style helper)."""
    if not callable(fn):
        raise TypeError("pipe(fn, ...) expects a callable.")
    return fn(self, *args, **kwargs)

clip

clip(*, lower=None, upper=None, subset=None)

Clamp numeric columns to the given bounds (schema-first).

Source code in python/pydantable/dataframe/_impl.py
def clip(
    self,
    *,
    lower: Any | None = None,
    upper: Any | None = None,
    subset: str | Sequence[str] | Selector | None = None,
) -> DataFrame[Any]:
    """Clamp numeric columns to the given bounds (schema-first)."""
    from pydantable import selectors as _selectors
    from pydantable.expressions import Literal, when

    if lower is None and upper is None:
        raise ValueError("clip() requires at least one of lower=... or upper=....")

    if isinstance(subset, str):
        subset = [subset]
    if isinstance(subset, Selector):
        cols = subset.resolve(self._current_field_types)
        if not cols:
            available = ", ".join(repr(c) for c in self._current_field_types)
            raise ValueError(
                f"clip(subset={subset!r}) matched no columns. "
                f"Available columns: [{available}]"
            )
        subset = cols

    targets = (
        list(subset)
        if subset is not None
        else _selectors.numeric().resolve(self._current_field_types)
    )
    if not targets:
        available = ", ".join(repr(c) for c in self._current_field_types)
        raise ValueError(
            f"clip() matched no numeric columns. Available columns: [{available}]"
        )

    updates: dict[str, Expr] = {}
    for c in targets:
        dt = self._current_field_types.get(c)
        if dt is None:
            raise KeyError(f"clip() unknown subset column {c!r}.")
        if not _is_describe_numeric(dt):
            raise TypeError(
                f"clip(subset=...) expects numeric columns, got {c!r} dtype={dt!r}."
            )
        expr: Expr = self.col(c)
        if lower is not None:
            lo = Literal(value=lower).cast(expr.dtype)
            expr = when(expr < lo, lo).otherwise(expr)
        if upper is not None:
            hi = Literal(value=upper).cast(expr.dtype)
            expr = when(expr > hi, hi).otherwise(expr)
        updates[c] = expr

    return self.with_columns(**updates)

first

first()

First row as a single-row DataFrame (lazy slice).

Source code in python/pydantable/dataframe/_impl.py
def first(self) -> DataFrame[Any]:
    """First row as a single-row DataFrame (lazy slice)."""
    return self.head(1)

tail

tail(n=5)

Last n rows (lazy slice). Cost: same idea as :meth:head.

Source code in python/pydantable/dataframe/_impl.py
def tail(self, n: int = 5) -> DataFrame[Any]:
    """Last ``n`` rows (lazy slice). **Cost:** same idea as :meth:`head`."""
    return self.slice(-n, n)

last

last()

Last row as a single-row DataFrame (lazy slice).

Source code in python/pydantable/dataframe/_impl.py
def last(self) -> DataFrame[Any]:
    """Last row as a single-row DataFrame (lazy slice)."""
    return self.tail(1)

pivot_longer

pivot_longer(*, id_vars=None, value_vars=None, names_to='variable', values_to='value', streaming=None)

Polars-friendly alias of :meth:melt (aka pivot_longer).

Source code in python/pydantable/dataframe/_impl.py
def pivot_longer(
    self,
    *,
    id_vars: str | Sequence[str] | Selector | None = None,
    value_vars: str | Sequence[str] | Selector | None = None,
    names_to: str = "variable",
    values_to: str = "value",
    streaming: bool | None = None,
) -> DataFrame[Any]:
    """Polars-friendly alias of :meth:`melt` (aka pivot_longer)."""
    return self.melt(
        id_vars=id_vars,
        value_vars=value_vars,
        variable_name=names_to,
        value_name=values_to,
        streaming=streaming,
    )

pivot_wider

pivot_wider(*, index, names_from, values_from, aggregate_function='first', sort_columns=False, separator='_', streaming=None)

Polars-friendly alias of :meth:pivot (aka pivot_wider).

Source code in python/pydantable/dataframe/_impl.py
def pivot_wider(
    self,
    *,
    index: str | Sequence[str] | Selector,
    names_from: str | Selector | ColumnRef,
    values_from: str | Sequence[str] | Selector,
    aggregate_function: str = "first",
    sort_columns: bool = False,
    separator: str = "_",
    streaming: bool | None = None,
) -> DataFrame[Any]:
    """Polars-friendly alias of :meth:`pivot` (aka pivot_wider)."""
    return self.pivot(
        index=index,
        columns=names_from,
        values=values_from,
        aggregate_function=aggregate_function,
        sort_columns=sort_columns,
        separator=separator,
        streaming=streaming,
    )

top_k

top_k(n, *, by, descending=True, nulls_last=None)

Top-k rows by sort key(s) (schema-first helper: sort then limit).

Source code in python/pydantable/dataframe/_impl.py
def top_k(
    self,
    n: int,
    *,
    by: str | Sequence[str],
    descending: bool = True,
    nulls_last: bool | None = None,
) -> DataFrame[Any]:
    """Top-k rows by sort key(s) (schema-first helper: sort then limit)."""
    keys = [by] if isinstance(by, str) else list(by)
    return self.sort(
        *keys,
        descending=[bool(descending)] * len(keys),
        nulls_last=None if nulls_last is None else [bool(nulls_last)] * len(keys),
        maintain_order=True,
    ).limit(n)

bottom_k

bottom_k(n, *, by, nulls_last=None)

Bottom-k rows by sort key(s) (schema-first helper: sort then limit).

Source code in python/pydantable/dataframe/_impl.py
def bottom_k(
    self,
    n: int,
    *,
    by: str | Sequence[str],
    nulls_last: bool | None = None,
) -> DataFrame[Any]:
    """Bottom-k rows by sort key(s) (schema-first helper: sort then limit)."""
    keys = [by] if isinstance(by, str) else list(by)
    return self.sort(
        *keys,
        descending=[False] * len(keys),
        nulls_last=None if nulls_last is None else [bool(nulls_last)] * len(keys),
        maintain_order=True,
    ).limit(n)

explode_outer

explode_outer(columns, *, streaming=None)

Explode lists; Spark-ish outer null/empty handling (see docs).

Source code in python/pydantable/dataframe/_impl.py
def explode_outer(
    self,
    columns: str | Sequence[str] | Selector,
    *,
    streaming: bool | None = None,
) -> DataFrame[Any]:
    """Explode lists; Spark-ish *outer* null/empty handling (see docs)."""
    return self.explode(columns, outer=True, streaming=streaming)

posexplode

posexplode(column, *, pos='pos', value=None, outer=False, streaming=None)

Explode one list column with a 0-based index (Spark posexplode).

Source code in python/pydantable/dataframe/_impl.py
def posexplode(
    self,
    column: str,
    *,
    pos: str = "pos",
    value: str | None = None,
    outer: bool = False,
    streaming: bool | None = None,
) -> DataFrame[Any]:
    """Explode one list column with a 0-based index (Spark ``posexplode``)."""
    if not isinstance(column, str) or not column:
        raise TypeError("posexplode() expects a non-empty str column name.")
    if not isinstance(pos, str) or not pos:
        raise TypeError("posexplode(pos=...) must be a non-empty str.")
    value_name = column if value is None else value
    if not isinstance(value_name, str) or not value_name:
        raise TypeError("posexplode(value=...) must be a non-empty str when set.")
    use_streaming = _resolve_engine_streaming(
        streaming=streaming, default=self._engine_streaming_default
    )
    out_data, schema_descriptors = self._engine.execute_posexplode(
        self._rust_plan,
        self._root_data,
        column,
        pos,
        value_name,
        streaming=use_streaming,
        outer=outer,
    )
    derived_fields = self._field_types_from_descriptors(schema_descriptors)
    derived_schema_type = make_derived_schema_type(
        self._current_schema_type, derived_fields
    )
    rust_plan = self._engine.make_plan(field_types_for_rust(derived_fields))
    return self._from_plan(
        root_data=out_data,
        root_schema_type=derived_schema_type,
        current_schema_type=derived_schema_type,
        rust_plan=rust_plan,
        engine=self._engine,
    )

posexplode_outer

posexplode_outer(column, *, pos='pos', value=None, streaming=None)

posexplode(..., outer=True) alias.

Source code in python/pydantable/dataframe/_impl.py
def posexplode_outer(
    self,
    column: str,
    *,
    pos: str = "pos",
    value: str | None = None,
    streaming: bool | None = None,
) -> DataFrame[Any]:
    """``posexplode(..., outer=True)`` alias."""
    return self.posexplode(
        column, pos=pos, value=value, outer=True, streaming=streaming
    )

explode_all

explode_all(*, streaming=None)

Explode all list-typed columns (schema-driven).

Source code in python/pydantable/dataframe/_impl.py
def explode_all(self, *, streaming: bool | None = None) -> DataFrame[Any]:
    """Explode all list-typed columns (schema-driven)."""
    from pydantable import selectors as _selectors

    sel = _selectors.by_dtype(_selectors.LIST)
    matched = sel.resolve(self._current_field_types)
    if not matched:
        available = ", ".join(repr(c) for c in self._current_field_types)
        raise ValueError(
            "explode_all() matched no list-typed columns. "
            f"Available columns: [{available}]"
        )
    return self.explode(sel, streaming=streaming)

unnest_all

unnest_all(*, streaming=None)

Unnest all struct-typed columns (schema-driven).

Source code in python/pydantable/dataframe/_impl.py
def unnest_all(self, *, streaming: bool | None = None) -> DataFrame[Any]:
    """Unnest all struct-typed columns (schema-driven)."""
    from pydantable import selectors as _selectors

    sel = _selectors.by_dtype(_selectors.STRUCT)
    matched = sel.resolve(self._current_field_types)
    if not matched:
        available = ", ".join(repr(c) for c in self._current_field_types)
        raise ValueError(
            "unnest_all() matched no struct-typed columns. "
            f"Available columns: [{available}]"
        )
    return self.unnest(sel, streaming=streaming)

join

join(other, *, on=None, left_on=None, right_on=None, how='inner', suffix='_right', coalesce=None, validate=None, join_nulls=None, maintain_order=None, allow_parallel=None, force_parallel=None, streaming=None)

Join two frames on key column(s); how is e.g. inner, left.

allow_parallel and force_parallel match Polars' keyword names but are not implemented in the native engine: passing either raises :exc:NotImplementedError (see the parity scorecard in the docs).

Source code in python/pydantable/dataframe/_impl.py
def join(
    self,
    other: DataFrame[Any],
    *,
    on: str | Sequence[str] | Selector | None = None,
    left_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
    right_on: str | Expr | Sequence[str | Expr] | Selector | None = None,
    how: str = "inner",
    suffix: str = "_right",
    coalesce: bool | None = None,
    validate: str | None = None,
    join_nulls: bool | None = None,
    maintain_order: bool | str | None = None,
    allow_parallel: bool | None = None,
    force_parallel: bool | None = None,
    streaming: bool | None = None,
) -> DataFrame[Any]:
    """Join two frames on key column(s); ``how`` is e.g. ``inner``, ``left``.

    ``allow_parallel`` and ``force_parallel`` match Polars' keyword names but are
    not implemented in the native engine: passing either raises
    :exc:`NotImplementedError` (see the parity scorecard in the docs).
    """
    if not isinstance(other, DataFrame):
        raise TypeError("join(other=...) expects another DataFrame.")
    if coalesce is not None and not isinstance(coalesce, bool):
        raise TypeError("join(coalesce=...) expects a bool or None.")
    if on is not None and (left_on is not None or right_on is not None):
        raise ValueError(
            "join() use either on=... or left_on=/right_on=..., not both."
        )

    def _available_cols_text(field_types: Mapping[str, Any]) -> str:
        return ", ".join(repr(c) for c in field_types)

    def _resolve_selector(
        *, sel: Selector, field_types: Mapping[str, Any], arg_name: str
    ) -> list[str]:
        matched = sel.resolve(field_types)
        if not matched:
            available = _available_cols_text(field_types)
            raise ValueError(
                f"join({arg_name}=...) selector matched no columns. "
                f"Available columns: [{available}]"
            )
        return matched

    def _resolve_keys(keys: str | Expr | Sequence[str | Expr] | None) -> list[str]:
        if keys is None:
            return []
        raw: list[str | Expr] = (
            [keys] if isinstance(keys, (str, Expr)) else list(keys)
        )
        out: list[str] = []
        for key in raw:
            if isinstance(key, str):
                out.append(key)
            elif isinstance(key, Expr):
                referenced = key.referenced_columns()
                if len(referenced) != 1:
                    raise TypeError(
                        "join expression keys must reference exactly one column."
                    )
                out.append(next(iter(referenced)))
            else:
                raise TypeError(
                    "join keys must be str, Expr, or sequences thereof."
                )
        return out

    def _base_type(tp: Any) -> Any:
        origin = get_origin(tp)
        if origin is None:
            return tp
        if origin is getattr(__import__("typing"), "Union", object()) or str(
            origin
        ).endswith("types.UnionType"):
            args = [a for a in get_args(tp) if a is not type(None)]
            if len(args) == 1:
                return args[0]
        return tp

    if on is not None:
        if isinstance(on, Selector):
            left_keys = _resolve_selector(
                sel=on, field_types=self._current_field_types, arg_name="on"
            )
        else:
            left_keys = [on] if isinstance(on, str) else list(on)
        missing_in_right = [
            k for k in left_keys if k not in other._current_field_types
        ]
        if missing_in_right:
            available = _available_cols_text(other._current_field_types)
            missing = ", ".join(repr(c) for c in missing_in_right)
            raise KeyError(
                "join() unknown right join key(s): "
                f"[{missing}]. Right available columns: [{available}]. "
                "Hint: use left_on=.../right_on=... for differently named "
                "key columns."
            )
        right_keys = list(left_keys)
        used_non_columnref_expr_keys = False
    else:
        used_non_columnref_expr_keys = False
        if isinstance(left_on, Selector):
            left_keys = _resolve_selector(
                sel=left_on,
                field_types=self._current_field_types,
                arg_name="left_on",
            )
        else:
            left_keys = _resolve_keys(left_on)

        if isinstance(right_on, Selector):
            right_keys = _resolve_selector(
                sel=right_on,
                field_types=other._current_field_types,
                arg_name="right_on",
            )
        else:
            right_keys = _resolve_keys(right_on)
        raw_left = (
            []
            if left_on is None
            else (
                [left_on]
                if isinstance(left_on, (str, Expr, Selector))
                else list(left_on)
            )
        )
        raw_right = (
            []
            if right_on is None
            else (
                [right_on]
                if isinstance(right_on, (str, Expr, Selector))
                else list(right_on)
            )
        )
        used_non_columnref_expr_keys = any(
            isinstance(x, Expr) and not isinstance(x, ColumnRef)
            for x in [*raw_left, *raw_right]
        )

    if validate is not None:
        v = str(validate)
        mapping = {
            "1:1": "one_to_one",
            "1:m": "one_to_many",
            "m:1": "many_to_one",
            "m:m": "many_to_many",
        }
        v = mapping.get(v, v)
        if v not in ("one_to_one", "one_to_many", "many_to_one", "many_to_many"):
            raise ValueError(
                "join(validate=...) must be one of one_to_one, one_to_many, "
                "many_to_one, many_to_many (or 1:1/1:m/m:1/m:m)."
            )
        if how == "cross":
            raise ValueError(
                "cross join does not support validate=...; remove validate or "
                "use a keyed join."
            )
        validate = v

    if join_nulls is not None and not isinstance(join_nulls, bool):
        raise TypeError("join(join_nulls=...) expects a bool or None.")
    if allow_parallel is not None or force_parallel is not None:
        # Polars join parallelism knobs are not currently exposed in the Polars
        # Rust API version pinned by pydantable-core; keep the argument surface
        # for future parity but fail explicitly for now.
        raise NotImplementedError(
            "join(allow_parallel=..., force_parallel=...) is not supported in "
            "this build."
        )

    maintain_order_norm: str | None
    if maintain_order is None:
        maintain_order_norm = None
    elif isinstance(maintain_order, bool):
        maintain_order_norm = "left" if maintain_order else "none"
    else:
        m = str(maintain_order).strip().lower()
        if m not in ("none", "left", "right"):
            raise ValueError(
                "join(maintain_order=...) must be one of 'none', 'left', 'right', "
                "a bool (True->'left', False->'none'), or None."
            )
        maintain_order_norm = m

    if coalesce is True:
        if how == "cross":
            raise ValueError(
                "cross join does not support coalesce=...; remove coalesce or "
                "use a keyed join."
            )
        if used_non_columnref_expr_keys and on is None:
            raise NotImplementedError(
                "join(coalesce=True) is only supported for column-name keys or "
                "simple ColumnRef expression keys."
            )
        if how in ("semi", "anti"):
            # Left-only output: coalesce has no observable effect but is accepted
            # for parity.
            pass
        elif how in ("full", "outer") and on is None and left_keys != right_keys:
            # Typed-safe subset: require exact base dtype match per key pair
            # (no casts).
            for lk, rk in zip(left_keys, right_keys, strict=True):
                lt = self._current_field_types.get(lk)
                rt = other._current_field_types.get(rk)
                if lt is None or rt is None:
                    continue
                if _base_type(lt) is not _base_type(rt):
                    raise NotImplementedError(
                        "join(coalesce=True) for full joins requires matching key "
                        "base dtypes (no casts)."
                    )

    if coalesce is False and on is None and left_keys != right_keys:
        # Typed-safe subset: only keep both key columns when it won't collide
        # with an existing left-side column name.
        for _lk, rk in zip(left_keys, right_keys, strict=True):
            if rk in self._current_field_types:
                raise NotImplementedError(
                    "join(coalesce=False) cannot retain both key columns when the "
                    "right key name collides with an existing left column."
                )

    if how == "cross":
        if left_keys or right_keys:
            raise ValueError("cross join does not accept on/left_on/right_on keys.")
        for name, val in (
            ("join_nulls", join_nulls),
            ("maintain_order", maintain_order),
            ("allow_parallel", allow_parallel),
            ("force_parallel", force_parallel),
        ):
            if val is not None:
                raise ValueError(f"cross join does not support {name}=...")
    else:
        if not left_keys or not right_keys:
            raise ValueError(
                "join() requires on=... or both left_on=... "
                "and right_on=... for non-cross joins."
            )
        if len(left_keys) != len(right_keys):
            raise ValueError(
                "join() left_on and right_on must have the same length."
            )

    use_streaming = _resolve_engine_streaming(
        streaming=streaming, default=self._engine_streaming_default
    )
    joined_data, schema_descriptors = self._engine.execute_join(
        self._rust_plan,
        self._root_data,
        other._rust_plan,
        other._root_data,
        left_keys,
        right_keys,
        how,
        suffix,
        validate=validate,
        coalesce=coalesce,
        join_nulls=join_nulls,
        maintain_order=maintain_order_norm,
        allow_parallel=allow_parallel,
        force_parallel=force_parallel,
        as_python_lists=True,
        streaming=use_streaming,
    )
    join_prev = previous_field_types_for_join(
        self._current_field_types,
        other._current_field_types,
        suffix=suffix,
        output_columns=list(schema_descriptors.keys()),
    )
    derived_fields = self._field_types_from_descriptors(
        schema_descriptors, previous=join_prev
    )
    derived_schema_type = make_derived_schema_type(
        self._current_schema_type, derived_fields
    )
    rust_plan = self._engine.make_plan(field_types_for_rust(derived_fields))
    return self._from_plan(
        root_data=joined_data,
        root_schema_type=derived_schema_type,
        current_schema_type=derived_schema_type,
        rust_plan=rust_plan,
        engine=self._engine,
    )

group_by

group_by(*keys, maintain_order=False, drop_nulls=True)

Group by key column(s); finish with :meth:GroupedDataFrame.agg.

maintain_order / drop_nulls are accepted for Polars parity, but only the default behavior is implemented today.

Source code in python/pydantable/dataframe/_impl.py
def group_by(
    self,
    *keys: str | ColumnRef,
    maintain_order: bool = False,
    drop_nulls: bool = True,
) -> GroupedDataFrame:
    """Group by key column(s); finish with :meth:`GroupedDataFrame.agg`.

    `maintain_order` / `drop_nulls` are accepted for Polars parity, but only the
    default behavior is implemented today.
    """
    selected: list[str] = []
    for key in keys:
        if isinstance(key, str):
            selected.append(key)
        elif isinstance(key, Expr):
            referenced = key.referenced_columns()
            if len(referenced) != 1:
                raise TypeError(
                    "group_by() accepts column names or ColumnRef expressions."
                )
            selected.append(next(iter(referenced)))
        else:
            raise TypeError(
                "group_by() accepts column names or ColumnRef expressions."
            )
    return GroupedDataFrame(
        self,
        selected,
        maintain_order=bool(maintain_order),
        drop_nulls=bool(drop_nulls),
    )

collect

collect(*, as_lists=False, as_numpy=False, as_polars=None, streaming=None, engine_streaming=None)

Materialize this typed logical DataFrame.

Cost: full engine execution on the current thread (same as :meth:to_dict when returning rows or lists). See the EXECUTION guide (project docs) Materialization costs.

By default returns a list of Pydantic models, one per row, validated against :attr:schema_type (the current projected schema).

Use as_lists=True for a columnar dict[str, list]. Use :meth:to_dict as a readable alias for that shape.

With as_numpy=True, returns dict[str, numpy.ndarray] (requires numpy).

With streaming=True, or when the environment variable PYDANTABLE_ENGINE_STREAMING is set to a truthy value (and streaming is omitted), Polars uses its streaming engine for collect where supported (best-effort; some plans fall back or error).

The as_polars argument is deprecated: use :meth:to_polars for a Polars DataFrame when the optional polars package is installed.

Source code in python/pydantable/dataframe/_impl.py
def collect(
    self,
    *,
    as_lists: bool = False,
    as_numpy: bool = False,
    as_polars: bool | None = None,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
) -> Any:
    """
    Materialize this typed logical DataFrame.

    **Cost:** full engine execution on the current thread (same as :meth:`to_dict`
    when returning rows or lists). See the EXECUTION guide (project docs)
    **Materialization costs**.

    By default returns a list of Pydantic models, one per row, validated
    against :attr:`schema_type` (the current projected schema).

    Use ``as_lists=True`` for a columnar ``dict[str, list]``. Use
    :meth:`to_dict` as a readable alias for that shape.

    With ``as_numpy=True``, returns ``dict[str, numpy.ndarray]`` (requires
    ``numpy``).

    With ``streaming=True``, or when the environment variable
    ``PYDANTABLE_ENGINE_STREAMING`` is set to a truthy value (and
    ``streaming`` is omitted), Polars uses its streaming engine for
    ``collect`` where supported (best-effort; some plans fall back or error).

    The ``as_polars`` argument is deprecated: use :meth:`to_polars` for a
    Polars ``DataFrame`` when the optional ``polars`` package is installed.
    """
    if as_polars is not None:
        warnings.warn(
            "as_polars is deprecated and will be removed in pydantable 2.0.0; "
            "use to_polars() for a Polars DataFrame, or collect(as_lists=True) "
            "/ to_dict() for columnar dicts.",
            DeprecationWarning,
            stacklevel=2,
        )
        if as_polars:
            return self.to_polars(streaming=streaming)
        return self.to_dict(streaming=streaming)
    if as_numpy and as_lists:
        raise ValueError(
            "collect() cannot specify both as_numpy=True and as_lists=True."
        )
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    column_dict = self._materialize_columns_with_missing_optional_fallback(
        streaming=use_streaming
    )
    column_dict = _coerce_enum_columns(column_dict, self._current_field_types)
    column_dict = self._apply_io_validation_if_configured(column_dict)
    if as_lists:
        return self._column_dict_in_schema_order(column_dict)
    if as_numpy:
        import numpy as np  # type: ignore[import-not-found]

        ordered = self._column_dict_in_schema_order(column_dict)
        return {k: np.asarray(v) for k, v in ordered.items()}
    ordered = self._column_dict_in_schema_order(column_dict)
    return _rows_from_column_dict(ordered, self._current_schema_type)

to_dict

to_dict(*, streaming=None, engine_streaming=None)

Columnar materialization (alias for collect(as_lists=True) shape).

Cost: full Rust execution for the current plan. See the EXECUTION guide (project docs). Pass streaming= or set PYDANTABLE_ENGINE_STREAMING like :meth:collect.

Source code in python/pydantable/dataframe/_impl.py
def to_dict(
    self,
    *,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
) -> dict[str, list[Any]]:
    """Columnar materialization (alias for ``collect(as_lists=True)`` shape).

    **Cost:** full Rust execution for the current plan. See the EXECUTION guide
    (project docs).
    Pass ``streaming=`` or set ``PYDANTABLE_ENGINE_STREAMING`` like :meth:`collect`.
    """
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    raw = self._materialize_columns_with_missing_optional_fallback(
        streaming=use_streaming
    )
    raw = _coerce_enum_columns(raw, self._current_field_types)
    raw = self._apply_io_validation_if_configured(raw)
    return self._column_dict_in_schema_order(raw)

null_count

null_count()

Count nulls per column (materializes via :meth:to_dict).

Source code in python/pydantable/dataframe/_impl.py
def null_count(self) -> dict[str, int]:
    """Count nulls per column (materializes via :meth:`to_dict`)."""
    d = self.to_dict()
    return {k: sum(v is None for v in vs) for k, vs in d.items()}

is_empty

is_empty()

True if the materialized result has zero rows.

This is eager (materializes via :meth:to_dict).

Source code in python/pydantable/dataframe/_impl.py
def is_empty(self) -> bool:
    """True if the materialized result has zero rows.

    This is eager (materializes via :meth:`to_dict`).
    """
    d = self.to_dict()
    if not d:
        return True
    return len(next(iter(d.values()))) == 0

shift

shift(periods=1)

Shift every column by periods rows.

This is eager (materializes via :meth:to_dict).

Source code in python/pydantable/dataframe/_impl.py
def shift(self, periods: int = 1) -> DataFrame[Any]:
    """Shift every column by `periods` rows.

    This is eager (materializes via :meth:`to_dict`).
    """
    p = int(periods)
    d = self.to_dict()
    if not d:
        return self
    n = len(next(iter(d.values())))
    out: dict[str, list[Any]] = {}
    for k, vs in d.items():
        if p == 0:
            out[k] = list(vs)
        elif p > 0:
            out[k] = [None] * min(p, n) + list(vs[: max(0, n - p)])
        else:
            q = -p
            out[k] = list(vs[q:]) + [None] * min(q, n)
    fields = dict(self._current_field_types)
    schema_t = make_derived_schema_type(self._current_schema_type, fields)
    plan = self._engine.make_plan(field_types_for_rust(fields))
    return self._from_plan(
        root_data=out,
        root_schema_type=schema_t,
        current_schema_type=schema_t,
        rust_plan=plan,
        engine=self._engine,
    )

sample

sample(*, n=None, fraction=None, seed=None, with_replacement=False)

Sample rows (eager; materializes via :meth:to_dict).

Source code in python/pydantable/dataframe/_impl.py
def sample(
    self,
    *,
    n: int | None = None,
    fraction: float | None = None,
    seed: int | None = None,
    with_replacement: bool = False,
) -> DataFrame[Any]:
    """Sample rows (eager; materializes via :meth:`to_dict`)."""
    if n is not None and fraction is not None:
        raise ValueError("sample() accepts n or fraction, not both.")
    if n is None and fraction is None:
        raise ValueError("sample() requires n or fraction.")
    d = self.to_dict()
    if not d:
        return self
    row_count = len(next(iter(d.values())))
    if fraction is not None:
        f = float(fraction)
        if f < 0 or f > 1:
            raise ValueError("sample(fraction=...) must be between 0 and 1.")
        n = int(row_count * f)
    assert n is not None
    if n < 0:
        raise ValueError("sample(n=...) must be >= 0.")
    rng = random.Random(seed)
    if with_replacement:
        idxs = [rng.randrange(row_count) for _ in range(n)]
    else:
        idxs = list(range(row_count))
        rng.shuffle(idxs)
        idxs = idxs[: min(n, row_count)]
    out: dict[str, list[Any]] = {k: [vs[i] for i in idxs] for k, vs in d.items()}
    fields = dict(self._current_field_types)
    schema_t = make_derived_schema_type(self._current_schema_type, fields)
    plan = self._engine.make_plan(field_types_for_rust(fields))
    return self._from_plan(
        root_data=out,
        root_schema_type=schema_t,
        current_schema_type=schema_t,
        rust_plan=plan,
        engine=self._engine,
    )

write_parquet

write_parquet(path, *, streaming=None, engine_streaming=None, write_kwargs=None, partition_by=None, mkdir=True)

Write this lazy plan to Parquet without a Python dict[str, list].

Optional streaming= (or PYDANTABLE_ENGINE_STREAMING) uses Polars streaming collect before writing, when supported.

write_kwargs may include Polars writer options such as compression, row_group_size, data_page_size, statistics, parallel.

partition_by is an optional list of column names; when set, path is a dataset root directory and rows are written as hive-style col=value/.../00000000.parquet shards (partition columns are omitted from each file). Use mkdir=False only if the root directory already exists.

Source code in python/pydantable/dataframe/_impl.py
def write_parquet(
    self,
    path: str | Any,
    *,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    write_kwargs: dict[str, Any] | None = None,
    partition_by: tuple[str, ...] | list[str] | None = None,
    mkdir: bool = True,
) -> None:
    """Write this lazy plan to Parquet without a Python ``dict[str, list]``.

    Optional ``streaming=`` (or ``PYDANTABLE_ENGINE_STREAMING``) uses Polars
    streaming collect before writing, when supported.

    ``write_kwargs`` may include Polars writer options such as ``compression``,
    ``row_group_size``, ``data_page_size``, ``statistics``, ``parallel``.

    ``partition_by`` is an optional list of column names; when set, ``path`` is a
    **dataset root directory** and rows are written as hive-style
    ``col=value/.../00000000.parquet`` shards (partition columns are omitted from
    each file). Use ``mkdir=False`` only if the root directory already exists.
    """
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    self._engine.write_parquet(
        self._rust_plan,
        self._root_data,
        str(path),
        streaming=use_streaming,
        write_kwargs=write_kwargs,
        partition_by=partition_by,
        mkdir=mkdir,
    )

write_csv

write_csv(path, *, streaming=None, engine_streaming=None, separator=',', write_kwargs=None)

Write this lazy plan to CSV from Rust (no Python column dict).

Source code in python/pydantable/dataframe/_impl.py
def write_csv(
    self,
    path: str | Any,
    *,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    separator: str = ",",
    write_kwargs: dict[str, Any] | None = None,
) -> None:
    """Write this lazy plan to CSV from Rust (no Python column dict)."""
    if len(separator) != 1:
        raise ValueError("write_csv separator must be a single character.")
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    self._engine.write_csv(
        self._rust_plan,
        self._root_data,
        str(path),
        streaming=use_streaming,
        separator=ord(separator),
        write_kwargs=write_kwargs,
    )

write_ipc

write_ipc(path, *, streaming=None, engine_streaming=None, compression=None, write_kwargs=None)

Write Arrow IPC file from Rust.

compression is None, 'lz4', or 'zstd'.

Source code in python/pydantable/dataframe/_impl.py
def write_ipc(
    self,
    path: str | Any,
    *,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    compression: str | None = None,
    write_kwargs: dict[str, Any] | None = None,
) -> None:
    """Write Arrow IPC file from Rust.

    ``compression`` is ``None``, ``'lz4'``, or ``'zstd'``.
    """
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    self._engine.write_ipc(
        self._rust_plan,
        self._root_data,
        str(path),
        streaming=use_streaming,
        compression=compression,
        write_kwargs=write_kwargs,
    )

write_ndjson

write_ndjson(path, *, streaming=None, engine_streaming=None, write_kwargs=None)

Write newline-delimited JSON from Rust.

write_kwargs may include json_format.

Source code in python/pydantable/dataframe/_impl.py
def write_ndjson(
    self,
    path: str | Any,
    *,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    write_kwargs: dict[str, Any] | None = None,
) -> None:
    """Write newline-delimited JSON from Rust.

    ``write_kwargs`` may include ``json_format``.
    """
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    self._engine.write_ndjson(
        self._rust_plan,
        self._root_data,
        str(path),
        streaming=use_streaming,
        write_kwargs=write_kwargs,
    )

collect_batches

collect_batches(*, batch_size=65536, streaming=None, engine_streaming=None)

Return Polars DataFrame chunks after one engine collect.

See the EXECUTION guide (project docs).

Source code in python/pydantable/dataframe/_impl.py
def collect_batches(
    self,
    *,
    batch_size: int = 65_536,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
) -> list[Any]:
    """Return Polars ``DataFrame`` chunks after one engine collect.

    See the EXECUTION guide (project docs).
    """
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    return self._engine.collect_batches(
        self._rust_plan,
        self._root_data,
        batch_size=batch_size,
        streaming=use_streaming,
    )

stream

stream(*, batch_size=65536, streaming=None, engine_streaming=None)

Yield dict[str, list] row chunks after one full engine collect.

Synchronous counterpart of :meth:astream for sync route handlers and StreamingResponse iterators that must not await. Same semantics as :meth:collect_batches (full collect, then slice — not out-of-core streaming). Requires pip install 'pydantable[polars]' for chunk conversion.

FastAPI: use stream() from def routes; use async for over astream() from async def routes when you want the event loop free between chunks.

Source code in python/pydantable/dataframe/_impl.py
def stream(
    self,
    *,
    batch_size: int = 65_536,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
) -> Iterator[dict[str, list[Any]]]:
    """Yield ``dict[str, list]`` row chunks after one full engine collect.

    Synchronous counterpart of :meth:`astream` for **sync** route handlers and
    ``StreamingResponse`` iterators that must not ``await``. Same semantics as
    :meth:`collect_batches` (full collect, then slice — not out-of-core
    streaming). Requires ``pip install 'pydantable[polars]'`` for chunk
    conversion.

    **FastAPI:** use ``stream()`` from ``def`` routes; use ``async for`` over
    ``astream()`` from ``async def`` routes when you want the event loop free
    between chunks.
    """
    try:
        importlib.import_module("polars")
    except ImportError as e:
        raise ImportError(
            "polars is required for stream(). Install with: "
            "pip install 'pydantable[polars]'"
        ) from e
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    pl_batches = self._engine.collect_batches(
        self._rust_plan,
        self._root_data,
        batch_size=batch_size,
        streaming=use_streaming,
    )
    for pl_df in pl_batches:
        yield pl_df.to_dict(as_series=False)

to_polars

to_polars(*, streaming=None, engine_streaming=None)

Materialize as a Polars DataFrame (requires the optional polars Python package: pip install 'pydantable[polars]').

Cost: builds a columnar dict via the Rust engine, then a Polars frame. See the EXECUTION guide (project docs). Optional streaming= matches :meth:collect.

Source code in python/pydantable/dataframe/_impl.py
def to_polars(
    self, *, streaming: bool | None = None, engine_streaming: bool | None = None
) -> Any:
    """
    Materialize as a Polars ``DataFrame`` (requires the optional ``polars``
    Python package: ``pip install 'pydantable[polars]'``).

    **Cost:** builds a columnar dict via the Rust engine, then a Polars frame.
    See the EXECUTION guide (project docs). Optional ``streaming=`` matches
    :meth:`collect`.
    """
    try:
        pl = importlib.import_module("polars")
    except ImportError as e:
        raise ImportError(
            "polars is required for to_polars(). Install with: "
            "pip install 'pydantable[polars]'"
        ) from e
    return pl.DataFrame(
        self.to_dict(streaming=streaming, engine_streaming=engine_streaming)
    )

to_arrow

to_arrow(*, streaming=None, engine_streaming=None)

Materialize as a PyArrow Table (requires the optional pyarrow package: pip install 'pydantable[arrow]').

This runs the same Rust execution path as :meth:to_dict, then builds Arrow arrays from Python lists—it is not a zero-copy export of internal buffers. Cost: same materialization class as :meth:to_dict. Optional streaming= matches :meth:collect.

Source code in python/pydantable/dataframe/_impl.py
def to_arrow(
    self, *, streaming: bool | None = None, engine_streaming: bool | None = None
) -> Any:
    """
    Materialize as a PyArrow ``Table`` (requires the optional ``pyarrow``
    package: ``pip install 'pydantable[arrow]'``).

    This runs the same Rust execution path as :meth:`to_dict`, then builds
    Arrow arrays from Python lists—it is not a zero-copy export of internal
    buffers. **Cost:** same materialization class as :meth:`to_dict`.
    Optional ``streaming=`` matches :meth:`collect`.
    """
    try:
        pa = importlib.import_module("pyarrow")
    except ImportError as e:
        raise ImportError(
            "pyarrow is required for to_arrow(). Install with: "
            "pip install 'pydantable[arrow]'"
        ) from e
    return pa.Table.from_pydict(
        self.to_dict(streaming=streaming, engine_streaming=engine_streaming)
    )

__dataframe__

__dataframe__(*, nan_as_null=False, allow_copy=True)

Python DataFrame Interchange Protocol export (for Streamlit and friends).

This materializes the current plan to a PyArrow Table (same cost class as :meth:to_arrow) and then delegates to Arrow's protocol implementation.

Requires the optional pyarrow dependency: pip install 'pydantable[arrow]'.

Source code in python/pydantable/dataframe/_impl.py
def __dataframe__(
    self, *, nan_as_null: bool = False, allow_copy: bool = True
) -> Any:
    """
    Python DataFrame Interchange Protocol export (for Streamlit and friends).

    This materializes the current plan to a PyArrow ``Table`` (same cost class as
    :meth:`to_arrow`) and then delegates to Arrow's protocol implementation.

    Requires the optional ``pyarrow`` dependency:
    ``pip install 'pydantable[arrow]'``.
    """
    table = self.to_arrow()
    # PyArrow implements the interchange protocol on Table; delegate to it.
    return table.__dataframe__(nan_as_null=nan_as_null, allow_copy=allow_copy)

__dataframe_consortium_standard__

__dataframe_consortium_standard__(api_version=None)

Entry point to the Dataframe API Consortium Standard (optional).

This is distinct from the interchange protocol (__dataframe__). When the optional dataframe-api-compat package is installed, this returns a standards-compliant wrapper object that exposes the Consortium's draft DataFrame API.

Source code in python/pydantable/dataframe/_impl.py
def __dataframe_consortium_standard__(self, api_version: str | None = None) -> Any:
    """
    Entry point to the Dataframe API Consortium Standard (optional).

    This is distinct from the interchange protocol (`__dataframe__`). When the
    optional `dataframe-api-compat` package is installed, this returns a
    standards-compliant wrapper object that exposes the Consortium's draft
    DataFrame API.
    """
    try:
        import dataframe_api_compat  # type: ignore[import-untyped]
    except ImportError as e:
        raise ImportError(
            "dataframe-api-compat is required for "
            "__dataframe_consortium_standard__(). "
            "Install with: pip install 'dataframe-api-compat'"
        ) from e
    try:
        import pandas as pd  # type: ignore[import-untyped]
    except ImportError as e:
        raise ImportError(
            "pandas is required for pydantable's "
            "__dataframe_consortium_standard__() implementation. "
            "Install with: pip install 'pydantable[pandas]'"
        ) from e

    pdf = pd.DataFrame(self.to_dict())
    converter = (
        dataframe_api_compat.pandas_standard.convert_to_standard_compliant_dataframe
    )
    return converter(pdf, api_version=api_version)

acollect async

acollect(*, as_lists=False, as_numpy=False, as_polars=None, streaming=None, engine_streaming=None, executor=None)

Async version of :meth:collect: same semantics.

When :meth:~pydantable.engine.protocols.ExecutionEngine.has_async_execute_plan is true on this frame's engine, work is awaited via a native async path (Rust/Tokio when using :class:~pydantable.engine.native.NativePolarsEngine); otherwise the same logic runs in :func:asyncio.to_thread or in executor.

Cancelling the awaiting task does not cancel in-flight Rust/Polars execution.

Source code in python/pydantable/dataframe/_impl.py
async def acollect(
    self,
    *,
    as_lists: bool = False,
    as_numpy: bool = False,
    as_polars: bool | None = None,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    executor: Executor | None = None,
) -> Any:
    """
    Async version of :meth:`collect`: same semantics.

    When :meth:`~pydantable.engine.protocols.ExecutionEngine.has_async_execute_plan`
    is true on this frame's engine, work is awaited via a native async path
    (Rust/Tokio when using :class:`~pydantable.engine.native.NativePolarsEngine`);
    otherwise the same logic runs in :func:`asyncio.to_thread` or in ``executor``.

    Cancelling the awaiting task does **not** cancel in-flight Rust/Polars
    execution.
    """
    if as_polars is not None:
        warnings.warn(
            "as_polars is deprecated and will be removed in pydantable 2.0.0; "
            "use to_polars() for a Polars DataFrame, or collect(as_lists=True) "
            "/ to_dict() for columnar dicts.",
            DeprecationWarning,
            stacklevel=2,
        )
        if as_polars:
            return await self.ato_polars(
                streaming=streaming,
                engine_streaming=engine_streaming,
                executor=executor,
            )
        return await self.ato_dict(
            streaming=streaming,
            engine_streaming=engine_streaming,
            executor=executor,
        )
    if as_numpy and as_lists:
        raise ValueError(
            "collect() cannot specify both as_numpy=True and as_lists=True."
        )
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    column_dict = await self._materialize_columns_async(
        streaming=use_streaming, executor=executor
    )
    column_dict = self._column_dict_in_schema_order(column_dict)
    if as_lists:
        return column_dict
    if as_numpy:
        import numpy as np  # type: ignore[import-not-found]

        return {k: np.asarray(v) for k, v in column_dict.items()}
    return _rows_from_column_dict(column_dict, self._current_schema_type)

ato_dict async

ato_dict(*, streaming=None, engine_streaming=None, executor=None)

Async version of :meth:to_dict (see :meth:acollect).

Source code in python/pydantable/dataframe/_impl.py
async def ato_dict(
    self,
    *,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    executor: Executor | None = None,
) -> dict[str, list[Any]]:
    """Async version of :meth:`to_dict` (see :meth:`acollect`)."""
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    raw = await self._materialize_columns_async(
        streaming=use_streaming, executor=executor
    )
    return self._column_dict_in_schema_order(raw)

ato_polars async

ato_polars(*, streaming=None, engine_streaming=None, executor=None)

Async version of :meth:to_polars (see :meth:acollect).

This still materializes a columnar Python dict first, then builds the Polars frame—same copies as the synchronous path.

Source code in python/pydantable/dataframe/_impl.py
async def ato_polars(
    self,
    *,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    executor: Executor | None = None,
) -> Any:
    """
    Async version of :meth:`to_polars` (see :meth:`acollect`).

    This still materializes a columnar Python dict first, then builds the
    Polars frame—same copies as the synchronous path.
    """
    try:
        pl = importlib.import_module("polars")
    except ImportError as e:
        raise ImportError(
            "polars is required for to_polars(). Install with: "
            "pip install 'pydantable[polars]'"
        ) from e
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    d = await self._materialize_columns_async(
        streaming=use_streaming, executor=executor
    )
    return pl.DataFrame(d)

ato_arrow async

ato_arrow(*, streaming=None, engine_streaming=None, executor=None)

Async version of :meth:to_arrow (see :meth:acollect).

Same materialization and copies as the synchronous path.

Source code in python/pydantable/dataframe/_impl.py
async def ato_arrow(
    self,
    *,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    executor: Executor | None = None,
) -> Any:
    """
    Async version of :meth:`to_arrow` (see :meth:`acollect`).

    Same materialization and copies as the synchronous path.
    """
    try:
        pa = importlib.import_module("pyarrow")
    except ImportError as e:
        raise ImportError(
            "pyarrow is required for to_arrow(). Install with: "
            "pip install 'pydantable[arrow]'"
        ) from e
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    d = await self._materialize_columns_async(
        streaming=use_streaming, executor=executor
    )
    return pa.Table.from_pydict(d)

submit

submit(*, as_lists=False, as_numpy=False, as_polars=None, streaming=None, engine_streaming=None, executor=None)

Background :meth:collect; await :meth:ExecutionHandle.result.

Source code in python/pydantable/dataframe/_impl.py
def submit(
    self,
    *,
    as_lists: bool = False,
    as_numpy: bool = False,
    as_polars: bool | None = None,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    executor: Executor | None = None,
) -> ExecutionHandle:
    """Background :meth:`collect`; await :meth:`ExecutionHandle.result`."""

    def _run() -> Any:
        return self.collect(
            as_lists=as_lists,
            as_numpy=as_numpy,
            as_polars=as_polars,
            streaming=streaming,
            engine_streaming=engine_streaming,
        )

    if executor is not None:
        fut: concurrent.futures.Future[Any] = executor.submit(_run)
    else:
        fut = concurrent.futures.Future[Any]()

        def _bg() -> None:
            # Mirror ThreadPoolExecutor semantics: if the user cancels the
            # handle before work starts, do not execute or attempt to set
            # results on a cancelled future.
            if not fut.set_running_or_notify_cancel():
                return
            try:
                fut.set_result(_run())
            except Exception as e:
                # If the future was cancelled mid-flight, avoid raising
                # InvalidStateError from set_exception on a cancelled future.
                if fut.cancelled():
                    return
                fut.set_exception(e)

        threading.Thread(target=_bg, daemon=True).start()
    return ExecutionHandle(fut)

astream async

astream(*, batch_size=65536, streaming=None, engine_streaming=None, executor=None)

Yield dict[str, list] chunks after one full engine collect.

Async counterpart of :meth:stream for async def handlers. See :meth:stream for semantics and FastAPI usage.

Source code in python/pydantable/dataframe/_impl.py
async def astream(
    self,
    *,
    batch_size: int = 65_536,
    streaming: bool | None = None,
    engine_streaming: bool | None = None,
    executor: Executor | None = None,
) -> Any:
    """Yield ``dict[str, list]`` chunks after one full engine collect.

    Async counterpart of :meth:`stream` for ``async def`` handlers. See
    :meth:`stream` for semantics and FastAPI usage.
    """
    use_streaming = _resolve_engine_streaming(
        streaming=streaming,
        engine_streaming=engine_streaming,
        default=self._engine_streaming_default,
    )
    from pydantable.engine._capability import prefer_native_async_collect_batches

    if prefer_native_async_collect_batches(self._engine):
        pl_batches = await self._engine.async_collect_plan_batches(
            self._rust_plan,
            self._root_data,
            batch_size=batch_size,
            streaming=use_streaming,
        )
    else:
        pl_batches = await _materialize_in_thread(
            functools.partial(
                self.collect_batches,
                batch_size=batch_size,
                streaming=streaming,
                engine_streaming=engine_streaming,
            ),
            executor=executor,
        )
    try:
        importlib.import_module("polars")
    except ImportError as e:
        raise ImportError(
            "polars is required for astream() (Polars chunk type). Install with: "
            "pip install 'pydantable[polars]'"
        ) from e
    for pl_df in pl_batches:
        col = await _materialize_in_thread(
            functools.partial(pl_df.to_dict, as_series=False),
            executor=executor,
        )
        yield col

explain

explain(*, format='text', streaming=None)

Introspect the current logical plan.

  • format="text" returns a compact, stable string summary.
  • format="json" returns a JSON-serializable dict.

This is a plan view, not an execution trace; it does not materialize data.

Source code in python/pydantable/dataframe/_impl.py
def explain(
    self,
    *,
    format: Literal["text", "json"] = "text",
    streaming: bool | None = None,
) -> str | dict[str, Any]:
    """
    Introspect the current logical plan.

    - `format="text"` returns a compact, stable string summary.
    - `format="json"` returns a JSON-serializable `dict`.

    This is a *plan* view, not an execution trace; it does not materialize data.
    """
    from pydantable.plan import explain as _explain

    use_streaming = _resolve_engine_streaming(
        streaming=streaming, default=self._engine_streaming_default
    )
    root_kind = (
        "scan_file_root" if _is_scan_file_root(self._root_data) else "in_memory"
    )
    return _explain(
        self._rust_plan,
        format=format,
        engine_streaming=use_streaming,
        root_data_kind=root_kind,
    )

concat classmethod

concat(dfs, *, how='vertical', streaming=None)

Stack or otherwise combine two or more frames (see Rust how values).

Source code in python/pydantable/dataframe/_impl.py
@classmethod
def concat(
    cls,
    dfs: Sequence[DataFrame[Any]],
    *,
    how: str = "vertical",
    streaming: bool | None = None,
) -> DataFrame[Any]:
    """Stack or otherwise combine two or more frames (see Rust ``how`` values)."""
    if len(dfs) < 2:
        raise ValueError("concat() requires at least two DataFrame inputs.")
    base = dfs[0]
    out_data = base._root_data
    out_schema_type = base._current_schema_type
    merged_ft = dict(base._current_field_types)
    out_plan = base._rust_plan
    use_streaming = _resolve_engine_streaming(
        streaming=streaming, default=base._engine_streaming_default
    )
    for df in dfs[1:]:
        out_data, schema_descriptors = base._engine.execute_concat(
            out_plan,
            out_data,
            df._rust_plan,
            df._root_data,
            how,
            as_python_lists=True,
            streaming=use_streaming,
        )
        derived_fields = schema_from_descriptors(schema_descriptors)
        merged_ft = merge_field_types_preserving_identity(
            merged_ft, schema_descriptors, derived_fields
        )
        out_schema_type = make_derived_schema_type(out_schema_type, merged_ft)
        out_plan = base._engine.make_plan(field_types_for_rust(merged_ft))
    return cls._from_plan(
        root_data=out_data,
        root_schema_type=out_schema_type,
        current_schema_type=out_schema_type,
        rust_plan=out_plan,
        engine=base._engine,
    )