-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdataframe.ts
2941 lines (2855 loc) · 95.4 KB
/
dataframe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Stream, Writable } from "node:stream";
import { concat } from "./functions";
import {
DynamicGroupBy,
type GroupBy,
RollingGroupBy,
_GroupBy,
} from "./groupby";
import { arrayToJsDataFrame } from "./internals/construction";
import pli from "./internals/polars_internal";
import { type LazyDataFrame, _LazyDataFrame } from "./lazy/dataframe";
import { Expr } from "./lazy/expr";
import { Series, _Series } from "./series";
import type {
CsvWriterOptions,
FillNullStrategy,
JoinOptions,
WriteAvroOptions,
WriteIPCOptions,
WriteParquetOptions,
} from "./types";
import { type DTypeToJs, DataType, type JsToDtype } from "./datatypes";
import {
type ColumnSelection,
type ColumnsOrExpr,
type ExprOrString,
type Simplify,
type ValueOrArray,
columnOrColumns,
columnOrColumnsStrict,
isSeriesArray,
} from "./utils";
import type {
Arithmetic,
Deserialize,
GroupByOps,
Sample,
Serialize,
} from "./shared_traits";
import { escapeHTML } from "./html";
import { col, element } from "./lazy/functions";
const inspect = Symbol.for("nodejs.util.inspect.custom");
const jupyterDisplay = Symbol.for("Jupyter.display");
/**
* Write methods for DataFrame
*/
interface WriteMethods {
/**
* __Write DataFrame to comma-separated values file (csv).__
*
* If no options are specified, it will return a new string containing the contents
* ___
* @param dest file or stream to write to
* @param options.includeBom - Whether to include UTF-8 BOM in the CSV output.
* @param options.lineTerminator - String used to end each row.
* @param options.includeHeader - Whether or not to include header in the CSV output.
* @param options.separator - Separate CSV fields with this symbol. _defaults to `,`
* @param options.quoteChar - Character to use for quoting. Default: \" Note: it will note be used when sep is used
* @param options.batchSize - Number of rows that will be processed per thread.
* @param options.datetimeFormat - A format string, with the specifiers defined by the
* `chrono <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>`_
* Rust crate. If no format specified, the default fractional-second
* precision is inferred from the maximum timeunit found in the frame's
* Datetime cols (if any).
* @param options.dateFormat - A format string, with the specifiers defined by the
* `chrono <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>`_
* Rust crate.
* @param options.timeFormat A format string, with the specifiers defined by the
* `chrono <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>`_
* Rust crate.
* @param options.floatPrecision - Number of decimal places to write, applied to both `Float32` and `Float64` datatypes.
* @param options.nullValue - A string representing null values (defaulting to the empty string).
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6, 7, 8],
* ... "ham": ['a', 'b', 'c']
* ... });
* > df.writeCSV();
* foo,bar,ham
* 1,6,a
* 2,7,b
* 3,8,c
*
* // using a file path
* > df.head(1).writeCSV("./foo.csv")
* // foo.csv
* foo,bar,ham
* 1,6,a
*
* // using a write stream
* > const writeStream = new Stream.Writable({
* ... write(chunk, encoding, callback) {
* ... console.log("writeStream: %O', chunk.toString());
* ... callback(null);
* ... }
* ... });
* > df.head(1).writeCSV(writeStream, {includeHeader: false});
* writeStream: '1,6,a'
* ```
* @category IO
*/
writeCSV(): Buffer;
writeCSV(options: CsvWriterOptions): Buffer;
writeCSV(dest: string | Writable, options?: CsvWriterOptions): void;
/**
* Write Dataframe to JSON string, file, or write stream
* @param destination file or write stream
* @param options
* @param options.format - json | lines
* @example
* ```
* > const df = pl.DataFrame({
* ... foo: [1,2,3],
* ... bar: ['a','b','c']
* ... })
*
* > df.writeJSON({format:"json"})
* `[ {"foo":1.0,"bar":"a"}, {"foo":2.0,"bar":"b"}, {"foo":3.0,"bar":"c"}]`
*
* > df.writeJSON({format:"lines"})
* `{"foo":1.0,"bar":"a"}
* {"foo":2.0,"bar":"b"}
* {"foo":3.0,"bar":"c"}`
*
* // writing to a file
* > df.writeJSON("/path/to/file.json", {format:'lines'})
* ```
* @category IO
*/
writeJSON(options?: { format: "lines" | "json" }): Buffer;
writeJSON(
destination: string | Writable,
options?: { format: "lines" | "json" },
): void;
/**
* Write to Arrow IPC feather file, either to a file path or to a write stream.
* @param destination File path to which the file should be written, or writable.
* @param options.compression Compression method *defaults to "uncompressed"*
* @category IO
*/
writeIPC(options?: WriteIPCOptions): Buffer;
writeIPC(destination: string | Writable, options?: WriteIPCOptions): void;
/**
* Write to Arrow IPC stream file, either to a file path or to a write stream.
* @param destination File path to which the file should be written, or writable.
* @param options.compression Compression method *defaults to "uncompressed"*
* @category IO
*/
writeIPCStream(options?: WriteIPCOptions): Buffer;
writeIPCStream(
destination: string | Writable,
options?: WriteIPCOptions,
): void;
/**
* Write the DataFrame disk in parquet format.
* @param destination File path to which the file should be written, or writable.
* @param options.compression Compression method *defaults to "uncompressed"*
* @category IO
*/
writeParquet(options?: WriteParquetOptions): Buffer;
writeParquet(
destination: string | Writable,
options?: WriteParquetOptions,
): void;
/**
* Write the DataFrame disk in avro format.
* @param destination File path to which the file should be written, or writable.
* @param options.compression Compression method *defaults to "uncompressed"*
* @category IO
*/
writeAvro(options?: WriteAvroOptions): Buffer;
writeAvro(destination: string | Writable, options?: WriteAvroOptions): void;
}
/**
* A DataFrame is a two-dimensional data structure that represents data as a table
* with rows and columns.
*
* @param data - Object, Array, or Series
* Two-dimensional data in various forms. object must contain Arrays.
* Array may contain Series or other Arrays.
* @param columns - Array of str, default undefined
* Column labels to use for resulting DataFrame. If specified, overrides any
* labels already present in the data. Must match data dimensions.
* @param orient - 'col' | 'row' default undefined
* Whether to interpret two-dimensional data as columns or as rows. If None,
* the orientation is inferred by matching the columns and data dimensions. If
* this does not yield conclusive results, column orientation is used.
* @example
* Constructing a DataFrame from an object :
* ```
* > const data = {'a': [1n, 2n], 'b': [3, 4]};
* > const df = pl.DataFrame(data);
* > console.log(df.toString());
* shape: (2, 2)
* ╭─────┬─────╮
* │ a ┆ b │
* │ --- ┆ --- │
* │ u64 ┆ i64 │
* ╞═════╪═════╡
* │ 1 ┆ 3 │
* ├╌╌╌╌╌┼╌╌╌╌╌┤
* │ 2 ┆ 4 │
* ╰─────┴─────╯
* ```
* Notice that the dtype is automatically inferred as a polars Int64:
* ```
* > df.dtypes
* ['UInt64', `Int64']
* ```
* In order to specify dtypes for your columns, initialize the DataFrame with a list
* of Series instead:
* ```
* > const data = [pl.Series('col1', [1, 2], pl.Float32), pl.Series('col2', [3, 4], pl.Int64)];
* > const df2 = pl.DataFrame(series);
* > console.log(df2.toString());
* shape: (2, 2)
* ╭──────┬──────╮
* │ col1 ┆ col2 │
* │ --- ┆ --- │
* │ f32 ┆ i64 │
* ╞══════╪══════╡
* │ 1 ┆ 3 │
* ├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
* │ 2 ┆ 4 │
* ╰──────┴──────╯
* ```
*
* Constructing a DataFrame from a list of lists, row orientation inferred:
* ```
* > const data = [[1, 2, 3], [4, 5, 6]];
* > const df4 = pl.DataFrame(data, ['a', 'b', 'c']);
* > console.log(df4.toString());
* shape: (2, 3)
* ╭─────┬─────┬─────╮
* │ a ┆ b ┆ c │
* │ --- ┆ --- ┆ --- │
* │ i64 ┆ i64 ┆ i64 │
* ╞═════╪═════╪═════╡
* │ 1 ┆ 2 ┆ 3 │
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
* │ 4 ┆ 5 ┆ 6 │
* ╰─────┴─────┴─────╯
* ```
*/
export interface DataFrame<T extends Record<string, Series> = any>
extends Arithmetic<DataFrame<T>>,
Sample<DataFrame<T>>,
Arithmetic<DataFrame<T>>,
WriteMethods,
Serialize,
GroupByOps<RollingGroupBy> {
/** @ignore */
_df: any;
dtypes: DataType[];
height: number;
shape: { height: number; width: number };
width: number;
get columns(): string[];
set columns(cols: string[]);
[inspect](): string;
[Symbol.iterator](): Generator<any, void, any>;
/**
* Very cheap deep clone.
*/
clone(): DataFrame<T>;
/**
* __Summary statistics for a DataFrame.__
*
* Only summarizes numeric datatypes at the moment and returns nulls for non numeric datatypes.
* ___
* Example
* ```
* > const df = pl.DataFrame({
* ... 'a': [1.0, 2.8, 3.0],
* ... 'b': [4, 5, 6],
* ... "c": [True, False, True]
* ... });
* ... df.describe()
* shape: (5, 4)
* ╭──────────┬───────┬─────┬──────╮
* │ describe ┆ a ┆ b ┆ c │
* │ --- ┆ --- ┆ --- ┆ --- │
* │ str ┆ f64 ┆ f64 ┆ f64 │
* ╞══════════╪═══════╪═════╪══════╡
* │ "mean" ┆ 2.267 ┆ 5 ┆ null │
* ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┤
* │ "std" ┆ 1.102 ┆ 1 ┆ null │
* ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┤
* │ "min" ┆ 1 ┆ 4 ┆ 0.0 │
* ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┤
* │ "max" ┆ 3 ┆ 6 ┆ 1 │
* ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┤
* │ "median" ┆ 2.8 ┆ 5 ┆ null │
* ╰──────────┴───────┴─────┴──────╯
* ```
*/
describe(): DataFrame;
/**
* __Remove column from DataFrame and return as new.__
* ___
* @param name
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6.0, 7.0, 8.0],
* ... "ham": ['a', 'b', 'c'],
* ... "apple": ['a', 'b', 'c']
* ... });
* // df: pl.DataFrame<{
* // foo: pl.Series<Float64, "foo">;
* // bar: pl.Series<Float64, "bar">;
* // ham: pl.Series<Utf8, "ham">;
* // apple: pl.Series<Utf8, "apple">;
* // }>
* > const df2 = df.drop(['ham', 'apple']);
* // df2: pl.DataFrame<{
* // foo: pl.Series<Float64, "foo">;
* // bar: pl.Series<Float64, "bar">;
* // }>
* > console.log(df2.toString());
* shape: (3, 2)
* ╭─────┬─────╮
* │ foo ┆ bar │
* │ --- ┆ --- │
* │ i64 ┆ f64 │
* ╞═════╪═════╡
* │ 1 ┆ 6 │
* ├╌╌╌╌╌┼╌╌╌╌╌┤
* │ 2 ┆ 7 │
* ├╌╌╌╌╌┼╌╌╌╌╌┤
* │ 3 ┆ 8 │
* ╰─────┴─────╯
* ```
*/
drop<U extends string>(name: U): DataFrame<Simplify<Omit<T, U>>>;
drop<const U extends string[]>(
names: U,
): DataFrame<Simplify<Omit<T, U[number]>>>;
drop<U extends string, const V extends string[]>(
name: U,
...names: V
): DataFrame<Simplify<Omit<T, U | V[number]>>>;
/**
* __Return a new DataFrame where the null values are dropped.__
*
* This method only drops nulls row-wise if any single value of the row is null.
* ___
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6, null, 8],
* ... "ham": ['a', 'b', 'c']
* ... });
* > console.log(df.dropNulls().toString());
* shape: (2, 3)
* ┌─────┬─────┬─────┐
* │ foo ┆ bar ┆ ham │
* │ --- ┆ --- ┆ --- │
* │ i64 ┆ i64 ┆ str │
* ╞═════╪═════╪═════╡
* │ 1 ┆ 6 ┆ "a" │
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
* │ 3 ┆ 8 ┆ "c" │
* └─────┴─────┴─────┘
* ```
*/
dropNulls(column: keyof T): DataFrame<T>;
dropNulls(columns: (keyof T)[]): DataFrame<T>;
dropNulls(...columns: (keyof T)[]): DataFrame<T>;
/**
* __Explode `DataFrame` to long format by exploding a column with Lists.__
* ___
* @param columns - column or columns to explode
* @example
* ```
* > const df = pl.DataFrame({
* ... "letters": ["c", "c", "a", "c", "a", "b"],
* ... "nrs": [[1, 2], [1, 3], [4, 3], [5, 5, 5], [6], [2, 1, 2]]
* ... });
* > console.log(df.toString());
* shape: (6, 2)
* ╭─────────┬────────────╮
* │ letters ┆ nrs │
* │ --- ┆ --- │
* │ str ┆ list [i64] │
* ╞═════════╪════════════╡
* │ "c" ┆ [1, 2] │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
* │ "c" ┆ [1, 3] │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
* │ "a" ┆ [4, 3] │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
* │ "c" ┆ [5, 5, 5] │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
* │ "a" ┆ [6] │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
* │ "b" ┆ [2, 1, 2] │
* ╰─────────┴────────────╯
* > df.explode("nrs")
* shape: (13, 2)
* ╭─────────┬─────╮
* │ letters ┆ nrs │
* │ --- ┆ --- │
* │ str ┆ i64 │
* ╞═════════╪═════╡
* │ "c" ┆ 1 │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ "c" ┆ 2 │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ "c" ┆ 1 │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ "c" ┆ 3 │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ ... ┆ ... │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ "c" ┆ 5 │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ "a" ┆ 6 │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ "b" ┆ 2 │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ "b" ┆ 1 │
* ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
* │ "b" ┆ 2 │
* ╰─────────┴─────╯
* ```
*/
explode(column: ExprOrString): DataFrame;
explode(columns: ExprOrString[]): DataFrame;
explode(column: ExprOrString, ...columns: ExprOrString[]): DataFrame;
/**
*
*
* __Extend the memory backed by this `DataFrame` with the values from `other`.__
* ___
Different from `vstack` which adds the chunks from `other` to the chunks of this `DataFrame`
`extent` appends the data from `other` to the underlying memory locations and thus may cause a reallocation.
If this does not cause a reallocation, the resulting data structure will not have any extra chunks
and thus will yield faster queries.
Prefer `extend` over `vstack` when you want to do a query after a single append. For instance during
online operations where you add `n` rows and rerun a query.
Prefer `vstack` over `extend` when you want to append many times before doing a query. For instance
when you read in multiple files and when to store them in a single `DataFrame`.
In the latter case, finish the sequence of `vstack` operations with a `rechunk`.
* @param other DataFrame to vertically add.
*/
extend(other: DataFrame<T>): DataFrame<T>;
/**
* Fill null/missing values by a filling strategy
*
* @param strategy - One of:
* - "backward"
* - "forward"
* - "mean"
* - "min'
* - "max"
* - "zero"
* - "one"
* @returns DataFrame with None replaced with the filling strategy.
*/
fillNull(strategy: FillNullStrategy): DataFrame<T>;
/**
* Filter the rows in the DataFrame based on a predicate expression.
* ___
* @param predicate - Expression that evaluates to a boolean Series.
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6, 7, 8],
* ... "ham": ['a', 'b', 'c']
* ... });
* // Filter on one condition
* > df.filter(pl.col("foo").lt(3))
* shape: (2, 3)
* ┌─────┬─────┬─────┐
* │ foo ┆ bar ┆ ham │
* │ --- ┆ --- ┆ --- │
* │ i64 ┆ i64 ┆ str │
* ╞═════╪═════╪═════╡
* │ 1 ┆ 6 ┆ a │
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
* │ 2 ┆ 7 ┆ b │
* └─────┴─────┴─────┘
* // Filter on multiple conditions
* > df.filter(
* ... pl.col("foo").lt(3)
* ... .and(pl.col("ham").eq(pl.lit("a")))
* ... )
* shape: (1, 3)
* ┌─────┬─────┬─────┐
* │ foo ┆ bar ┆ ham │
* │ --- ┆ --- ┆ --- │
* │ i64 ┆ i64 ┆ str │
* ╞═════╪═════╪═════╡
* │ 1 ┆ 6 ┆ a │
* └─────┴─────┴─────┘
* ```
*/
filter(predicate: any): DataFrame<T>;
/**
* Find the index of a column by name.
* ___
* @param name -Name of the column to find.
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6, 7, 8],
* ... "ham": ['a', 'b', 'c']
* ... });
* > df.findIdxByName("ham"))
* 2
* ```
*/
findIdxByName(name: keyof T): number;
/**
* __Apply a horizontal reduction on a DataFrame.__
*
* This can be used to effectively determine aggregations on a row level,
* and can be applied to any DataType that can be supercasted (casted to a similar parent type).
*
* An example of the supercast rules when applying an arithmetic operation on two DataTypes are for instance:
* - Int8 + Utf8 = Utf8
* - Float32 + Int64 = Float32
* - Float32 + Float64 = Float64
* ___
* @param operation - function that takes two `Series` and returns a `Series`.
* @returns Series
* @example
* ```
* > // A horizontal sum operation
* > let df = pl.DataFrame({
* ... "a": [2, 1, 3],
* ... "b": [1, 2, 3],
* ... "c": [1.0, 2.0, 3.0]
* ... });
* > df.fold((s1, s2) => s1.plus(s2))
* Series: 'a' [f64]
* [
* 4
* 5
* 9
* ]
* > // A horizontal minimum operation
* > df = pl.DataFrame({
* ... "a": [2, 1, 3],
* ... "b": [1, 2, 3],
* ... "c": [1.0, 2.0, 3.0]
* ... });
* > df.fold((s1, s2) => s1.zipWith(s1.lt(s2), s2))
* Series: 'a' [f64]
* [
* 1
* 1
* 3
* ]
* > // A horizontal string concatenation
* > df = pl.DataFrame({
* ... "a": ["foo", "bar", 2],
* ... "b": [1, 2, 3],
* ... "c": [1.0, 2.0, 3.0]
* ... })
* > df.fold((s1, s2) => s.plus(s2))
* Series: '' [f64]
* [
* "foo11"
* "bar22
* "233"
* ]
* ```
*/
fold(operation: (s1: Series, s2: Series) => Series): Series;
/**
* Check if DataFrame is equal to other.
* ___
* @param options
* @param options.other - DataFrame to compare.
* @param options.nullEqual Consider null values as equal.
* @example
* ```
* > const df1 = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6.0, 7.0, 8.0],
* ... "ham": ['a', 'b', 'c']
* ... })
* > const df2 = pl.DataFrame({
* ... "foo": [3, 2, 1],
* ... "bar": [8.0, 7.0, 6.0],
* ... "ham": ['c', 'b', 'a']
* ... })
* > df1.frameEqual(df1)
* true
* > df1.frameEqual(df2)
* false
* ```
*/
frameEqual(other: DataFrame): boolean;
frameEqual(other: DataFrame, nullEqual: boolean): boolean;
/**
* Get a single column as Series by name.
*
* ---
* @example
* ```
* > const df = pl.DataFrame({
* ... foo: [1, 2, 3],
* ... bar: [6, null, 8],
* ... ham: ["a", "b", "c"],
* ... });
* // df: pl.DataFrame<{
* // foo: pl.Series<Float64, "foo">;
* // bar: pl.Series<Float64, "bar">;
* // ham: pl.Series<Utf8, "ham">;
* // }>
* > const column = df.getColumn("foo");
* // column: pl.Series<Float64, "foo">
* ```
*/
getColumn<U extends keyof T>(name: U): T[U];
getColumn(name: string): Series;
/**
* Get the DataFrame as an Array of Series.
* ---
* @example
* ```
* > const df = pl.DataFrame({
* ... foo: [1, 2, 3],
* ... bar: [6, null, 8],
* ... ham: ["a", "b", "c"],
* ... });
* // df: pl.DataFrame<{
* // foo: pl.Series<Float64, "foo">;
* // bar: pl.Series<Float64, "bar">;
* // ham: pl.Series<Utf8, "ham">;
* // }>
* > const columns = df.getColumns();
* // columns: (pl.Series<Float64, "foo"> | pl.Series<Float64, "bar"> | pl.Series<Utf8, "ham">)[]
* ```
*/
getColumns(): T[keyof T][];
/**
* Start a groupby operation.
* ___
* @param by - Column(s) to group by.
*/
groupBy(...by: ColumnSelection[]): GroupBy;
/**
* Hash and combine the rows in this DataFrame. _(Hash value is UInt64)_
* @param k0 - seed parameter
* @param k1 - seed parameter
* @param k2 - seed parameter
* @param k3 - seed parameter
*/
hashRows(k0?: number, k1?: number, k2?: number, k3?: number): Series;
hashRows(options: {
k0?: number;
k1?: number;
k2?: number;
k3?: number;
}): Series;
/**
* Get first N rows as DataFrame.
* ___
* @param length - Length of the head.
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3, 4, 5],
* ... "bar": [6, 7, 8, 9, 10],
* ... "ham": ['a', 'b', 'c', 'd','e']
* ... });
* > df.head(3)
* shape: (3, 3)
* ╭─────┬─────┬─────╮
* │ foo ┆ bar ┆ ham │
* │ --- ┆ --- ┆ --- │
* │ i64 ┆ i64 ┆ str │
* ╞═════╪═════╪═════╡
* │ 1 ┆ 6 ┆ "a" │
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
* │ 2 ┆ 7 ┆ "b" │
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
* │ 3 ┆ 8 ┆ "c" │
* ╰─────┴─────┴─────╯
* ```
*/
head(length?: number): DataFrame<T>;
/**
* Return a new DataFrame grown horizontally by stacking multiple Series to it.
* @param columns - array of Series or DataFrame to stack
* @param inPlace - Modify in place
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6, 7, 8],
* ... "ham": ['a', 'b', 'c']
* ... });
* // df: pl.DataFrame<{
* // foo: pl.Series<Float64, "foo">;
* // bar: pl.Series<Float64, "bar">;
* // ham: pl.Series<Utf8, "ham">;
* // }>
* > const x = pl.Series("apple", [10, 20, 30])
* // x: pl.Series<Float64, "apple">
* > df.hstack([x])
* // pl.DataFrame<{
* // foo: pl.Series<Float64, "foo">;
* // bar: pl.Series<Float64, "bar">;
* // ham: pl.Series<Utf8, "ham">;
* // apple: pl.Series<Float64, "apple">;
* // }>
* shape: (3, 4)
* ╭─────┬─────┬─────┬───────╮
* │ foo ┆ bar ┆ ham ┆ apple │
* │ --- ┆ --- ┆ --- ┆ --- │
* │ i64 ┆ i64 ┆ str ┆ i64 │
* ╞═════╪═════╪═════╪═══════╡
* │ 1 ┆ 6 ┆ "a" ┆ 10 │
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤
* │ 2 ┆ 7 ┆ "b" ┆ 20 │
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤
* │ 3 ┆ 8 ┆ "c" ┆ 30 │
* ╰─────┴─────┴─────┴───────╯
* ```
*/
hstack<U extends Record<string, Series> = any>(
columns: DataFrame<U>,
): DataFrame<Simplify<T & U>>;
hstack<U extends Series[]>(
columns: U,
): DataFrame<Simplify<T & { [K in U[number] as K["name"]]: K }>>;
hstack(columns: Array<Series> | DataFrame): DataFrame;
hstack(columns: Array<Series> | DataFrame, inPlace?: boolean): void;
/**
* Insert a Series at a certain column index. This operation is in place.
* @param index - Column position to insert the new `Series` column.
* @param series - `Series` to insert
*/
insertAtIdx(index: number, series: Series): void;
/**
* Interpolate intermediate values. The interpolation method is linear.
*/
interpolate(): DataFrame<T>;
/**
* Get a mask of all duplicated rows in this DataFrame.
*/
isDuplicated(): Series;
/**
* Check if the dataframe is empty
*/
isEmpty(): boolean;
/**
* Get a mask of all unique rows in this DataFrame.
*/
isUnique(): Series;
/**
* __SQL like joins.__
* @param df - DataFrame to join with.
* @param options
* @param options.leftOn - Name(s) of the left join column(s).
* @param options.rightOn - Name(s) of the right join column(s).
* @param options.on - Name(s) of the join columns in both DataFrames.
* @param options.how - Join strategy
* @param options.suffix - Suffix to append to columns with a duplicate name.
* @see {@link JoinOptions}
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6.0, 7.0, 8.0],
* ... "ham": ['a', 'b', 'c']
* ... });
* > const otherDF = pl.DataFrame({
* ... "apple": ['x', 'y', 'z'],
* ... "ham": ['a', 'b', 'd']
* ... });
* > df.join(otherDF, {on: 'ham'})
* shape: (2, 4)
* ╭─────┬─────┬─────┬───────╮
* │ foo ┆ bar ┆ ham ┆ apple │
* │ --- ┆ --- ┆ --- ┆ --- │
* │ i64 ┆ f64 ┆ str ┆ str │
* ╞═════╪═════╪═════╪═══════╡
* │ 1 ┆ 6 ┆ "a" ┆ "x" │
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤
* │ 2 ┆ 7 ┆ "b" ┆ "y" │
* ╰─────┴─────┴─────┴───────╯
* ```
*/
join(
other: DataFrame,
options: { on: ValueOrArray<string> } & Omit<
JoinOptions,
"leftOn" | "rightOn"
>,
): DataFrame;
join(
other: DataFrame,
options: {
leftOn: ValueOrArray<string>;
rightOn: ValueOrArray<string>;
} & Omit<JoinOptions, "on">,
): DataFrame;
join(other: DataFrame, options: { how: "cross"; suffix?: string }): DataFrame;
/**
* Perform an asof join. This is similar to a left-join except that we
* match on nearest key rather than equal keys.
*
* Both DataFrames must be sorted by the asofJoin key.
*
* For each row in the left DataFrame:
* - A "backward" search selects the last row in the right DataFrame whose
* 'on' key is less than or equal to the left's key.
*
* - A "forward" search selects the first row in the right DataFrame whose
* 'on' key is greater than or equal to the left's key.
*
* - A "nearest" search selects the last row in the right DataFrame whose value
* is nearest to the left's key. String keys are not currently supported for a
* nearest search.
*
* The default is "backward".
*
* @param other DataFrame to join with.
* @param options.leftOn Join column of the left DataFrame.
* @param options.rightOn Join column of the right DataFrame.
* @param options.on Join column of both DataFrames. If set, `leftOn` and `rightOn` should be undefined.
* @param options.byLeft join on these columns before doing asof join
* @param options.byRight join on these columns before doing asof join
* @param options.strategy One of 'forward', 'backward', 'nearest'
* @param options.suffix Suffix to append to columns with a duplicate name.
* @param options.tolerance
* Numeric tolerance. By setting this the join will only be done if the near keys are within this distance.
* If an asof join is done on columns of dtype "Date", "Datetime" you
* use the following string language:
*
* - 1ns *(1 nanosecond)*
* - 1us *(1 microsecond)*
* - 1ms *(1 millisecond)*
* - 1s *(1 second)*
* - 1m *(1 minute)*
* - 1h *(1 hour)*
* - 1d *(1 day)*
* - 1w *(1 week)*
* - 1mo *(1 calendar month)*
* - 1y *(1 calendar year)*
* - 1i *(1 index count)*
*
* Or combine them:
* - "3d12h4m25s" # 3 days, 12 hours, 4 minutes, and 25 seconds
* @param options.allowParallel Allow the physical plan to optionally evaluate the computation of both DataFrames up to the join in parallel.
* @param options.forceParallel Force the physical plan to evaluate the computation of both DataFrames up to the join in parallel.
*
* @example
* ```
* > const gdp = pl.DataFrame({
* ... date: [
* ... new Date('2016-01-01'),
* ... new Date('2017-01-01'),
* ... new Date('2018-01-01'),
* ... new Date('2019-01-01'),
* ... ], // note record date: Jan 1st (sorted!)
* ... gdp: [4164, 4411, 4566, 4696],
* ... })
* > const population = pl.DataFrame({
* ... date: [
* ... new Date('2016-05-12'),
* ... new Date('2017-05-12'),
* ... new Date('2018-05-12'),
* ... new Date('2019-05-12'),
* ... ], // note record date: May 12th (sorted!)
* ... "population": [82.19, 82.66, 83.12, 83.52],
* ... })
* > population.joinAsof(
* ... gdp,
* ... {leftOn:"date", rightOn:"date", strategy:"backward"}
* ... )
* shape: (4, 3)
* ┌─────────────────────┬────────────┬──────┐
* │ date ┆ population ┆ gdp │
* │ --- ┆ --- ┆ --- │
* │ datetime[μs] ┆ f64 ┆ i64 │
* ╞═════════════════════╪════════════╪══════╡
* │ 2016-05-12 00:00:00 ┆ 82.19 ┆ 4164 │
* ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
* │ 2017-05-12 00:00:00 ┆ 82.66 ┆ 4411 │
* ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
* │ 2018-05-12 00:00:00 ┆ 83.12 ┆ 4566 │
* ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
* │ 2019-05-12 00:00:00 ┆ 83.52 ┆ 4696 │
* └─────────────────────┴────────────┴──────┘
* ```
*/
joinAsof(
other: DataFrame,
options: {
leftOn?: string;
rightOn?: string;
on?: string;
byLeft?: string | string[];
byRight?: string | string[];
by?: string | string[];
strategy?: "backward" | "forward" | "nearest";
suffix?: string;
tolerance?: number | string;
allowParallel?: boolean;
forceParallel?: boolean;
},
): DataFrame;
lazy(): LazyDataFrame;
/**
* Get first N rows as DataFrame.
* @see {@link head}
*/
limit(length?: number): DataFrame<T>;
map<ReturnT>(
// TODO: strong types for the mapping function
func: (row: any[], i: number, arr: any[][]) => ReturnT,
): ReturnT[];
/**
* Aggregate the columns of this DataFrame to their maximum value.
* ___
* @param axis - either 0 or 1
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6, 7, 8],
* ... "ham": ['a', 'b', 'c']
* ... });
* > df.max()
* shape: (1, 3)
* ╭─────┬─────┬──────╮
* │ foo ┆ bar ┆ ham │
* │ --- ┆ --- ┆ --- │
* │ i64 ┆ i64 ┆ str │
* ╞═════╪═════╪══════╡
* │ 3 ┆ 8 ┆ null │
* ╰─────┴─────┴──────╯
* ```
*/
max(): DataFrame<T>;
max(axis: 0): DataFrame<T>;
max(axis: 1): Series;
/**
* Aggregate the columns of this DataFrame to their mean value.
* ___
*
* @param axis - either 0 or 1
* @param nullStrategy - this argument is only used if axis == 1
*/
mean(): DataFrame<T>;
mean(axis: 0): DataFrame<T>;
mean(axis: 1): Series;
mean(axis: 1, nullStrategy?: "ignore" | "propagate"): Series;
/**
* Aggregate the columns of this DataFrame to their median value.
* ___
* @example
* ```
* > const df = pl.DataFrame({
* ... "foo": [1, 2, 3],
* ... "bar": [6, 7, 8],
* ... "ham": ['a', 'b', 'c']
* ... });
* > df.median();
* shape: (1, 3)
* ╭─────┬─────┬──────╮
* │ foo ┆ bar ┆ ham │
* │ --- ┆ --- ┆ --- │
* │ f64 ┆ f64 ┆ str │
* ╞═════╪═════╪══════╡
* │ 2 ┆ 7 ┆ null │
* ╰─────┴─────┴──────╯
* ```
*/