-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathseries.ts
1882 lines (1785 loc) · 69.9 KB
/
series.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
// Copyright (c) 2020-2022, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {
Float32Buffer,
Float64Buffer,
Int16Buffer,
Int32Buffer,
Int64Buffer,
Int8Buffer,
MemoryData,
MemoryView,
Uint16Buffer,
Uint32Buffer,
Uint64Buffer,
Uint8Buffer,
Uint8ClampedBuffer,
} from '@rapidsai/cuda';
import {DeviceBuffer, MemoryResource} from '@rapidsai/rmm';
import * as arrow from 'apache-arrow';
import {JavaScriptArrayDataType} from 'apache-arrow/interfaces';
import {compareTypes} from 'apache-arrow/visitor/typecomparator';
import {Column} from './column';
import {fromArrow} from './column/from_arrow';
import {DataFrame} from './data_frame';
import {DisplayOptions} from './dataframe/print';
import {Scalar} from './scalar';
import {DISPOSER, scope} from './scope';
import {Table} from './table';
import {
Bool8,
Categorical,
DataType,
Float32,
Float64,
IndexType,
Int16,
Int32,
Int64,
Int8,
Integral,
List,
Numeric,
Struct,
TimestampDay,
TimestampMicrosecond,
TimestampMillisecond,
TimestampNanosecond,
TimestampSecond,
Uint16,
Uint32,
Uint64,
Uint8,
Utf8String,
} from './types/dtypes';
import {
DuplicateKeepOption,
NullOrder,
} from './types/enums';
import {ArrowToCUDFType, CommonType, findCommonType} from './types/mappings';
export type SeriesProps<T extends DataType = any> = {
/*
* SeriesProps *with* a `nullMask` shouldn't allow `data` to be an Array with elements and nulls:
* ```javascript
* Series.new({
* type: new Int32,
* data: [1, 0, 2, 3, 0], ///< must not include nulls
* nullMask: [true, false, true, true, false]
* })
* ```
*/
type: T;
data?: DeviceBuffer | MemoryData | arrow.Vector<T>| T['scalarType'][] | null;
offset?: number;
length?: number;
nullCount?: number;
nullMask?: DeviceBuffer | MemoryData | any[] | boolean | null;
children?: ReadonlyArray<Series>| null;
}|{
/*
* SeriesProps *without* a `nullMask` should allow `data` to be an Array with elements and nulls:
* ```javascript
* Series.new({
* type: new Int32,
* data: [1, null, 2, 3, null] ///< can include nulls
* })
* ```
*/
type: T;
data?: DeviceBuffer|MemoryData|arrow.Vector<T>|(T['scalarType'] | null | undefined)[]|null;
offset?: number;
length?: number;
nullCount?: number;
nullMask?: never;
children?: ReadonlyArray<Series>|null;
};
// clang-format off
/* eslint-disable @typescript-eslint/no-unused-vars */
class CastVisitor<T extends DataType> extends arrow.Visitor {
constructor(private series: AbstractSeries<T>, private memoryResource?: MemoryResource) { super(); }
public visitBool <T extends Bool8>(_dtype: T) { return this.series._castAsBool8(this.memoryResource); }
public visitInt8 <T extends Int8>(_dtype: T) { return this.series._castAsInt8(this.memoryResource); }
public visitInt16 <T extends Int16>(_dtype: T) { return this.series._castAsInt16(this.memoryResource); }
public visitInt32 <T extends Int32>(_dtype: T) { return this.series._castAsInt32(this.memoryResource); }
public visitInt64 <T extends Int64>(_dtype: T) { return this.series._castAsInt64(this.memoryResource); }
public visitUint8 <T extends Uint8>(_dtype: T) { return this.series._castAsUint8(this.memoryResource); }
public visitUint16 <T extends Uint16>(_dtype: T) { return this.series._castAsUint16(this.memoryResource); }
public visitUint32 <T extends Uint32>(_dtype: T) { return this.series._castAsUint32(this.memoryResource); }
public visitUint64 <T extends Uint64>(_dtype: T) { return this.series._castAsUint64(this.memoryResource); }
public visitFloat32 <T extends Float32>(_dtype: T) { return this.series._castAsFloat32(this.memoryResource); }
public visitFloat64 <T extends Float64>(_dtype: T) { return this.series._castAsFloat64(this.memoryResource); }
public visitUtf8 <T extends Utf8String>(_dtype: T) { return this.series._castAsString(this.memoryResource); }
public visitDateDay <T extends TimestampDay>(_dtype: T) { return this.series._castAsTimeStampDay(this.memoryResource); }
public visitDateMillisecond <T extends TimestampMillisecond>(_dtype: T) { return this.series._castAsTimeStampMillisecond(this.memoryResource); }
public visitTimestampSecond <T extends TimestampSecond>(_dtype: T) { return this.series._castAsTimeStampSecond(this.memoryResource); }
public visitTimestampMillisecond <T extends TimestampMillisecond>(_dtype: T) { return this.series._castAsTimeStampMillisecond(this.memoryResource); }
public visitTimestampMicrosecond <T extends TimestampMicrosecond>(_dtype: T) { return this.series._castAsTimeStampMicrosecond(this.memoryResource); }
public visitTimestampNanosecond <T extends TimestampNanosecond>(_dtype: T) { return this.series._castAsTimeStampNanosecond(this.memoryResource); }
public visitDictionary <T extends Categorical>(dtype: T) { return this.series._castAsCategorical(dtype, this.memoryResource); }
}
/* eslint-enable @typescript-eslint/no-unused-vars */
// clang-format on
export type Series<T extends arrow.DataType = any> = {
[arrow.Type.NONE]: never, // TODO
[arrow.Type.Null]: never, // TODO
[arrow.Type.Int]: never,
[arrow.Type.Int8]: Int8Series,
[arrow.Type.Int16]: Int16Series,
[arrow.Type.Int32]: Int32Series,
[arrow.Type.Int64]: Int64Series,
[arrow.Type.Uint8]: Uint8Series,
[arrow.Type.Uint16]: Uint16Series,
[arrow.Type.Uint32]: Uint32Series,
[arrow.Type.Uint64]: Uint64Series,
[arrow.Type.Float]: never,
[arrow.Type.Float16]: never,
[arrow.Type.Float32]: Float32Series,
[arrow.Type.Float64]: Float64Series,
[arrow.Type.Binary]: never,
[arrow.Type.Utf8]: StringSeries,
[arrow.Type.Bool]: Bool8Series,
[arrow.Type.Decimal]: never, // TODO
[arrow.Type.Date]: never, // TODO
[arrow.Type.DateDay]: TimestampDaySeries,
[arrow.Type.DateMillisecond]: TimestampMillisecondSeries,
[arrow.Type.Time]: never, // TODO
[arrow.Type.TimeSecond]: never, // TODO
[arrow.Type.TimeMillisecond]: never, // TODO
[arrow.Type.TimeMicrosecond]: never, // TODO
[arrow.Type.TimeNanosecond]: never, // TODO
[arrow.Type.Timestamp]: never, // TODO
[arrow.Type.TimestampSecond]: TimestampSecondSeries,
[arrow.Type.TimestampMillisecond]: TimestampMillisecondSeries,
[arrow.Type.TimestampMicrosecond]: TimestampMicrosecondSeries,
[arrow.Type.TimestampNanosecond]: TimestampNanosecondSeries,
[arrow.Type.Interval]: never, // TODO
[arrow.Type.IntervalDayTime]: never, // TODO
[arrow.Type.IntervalYearMonth]: never, // TODO
[arrow.Type.List]: ListSeries<(T extends List ? T['valueType'] : any)>,
[arrow.Type.Struct]: StructSeries<(T extends Struct ? T['dataTypes'] : any)>,
[arrow.Type.Union]: never, // TODO
[arrow.Type.DenseUnion]: never, // TODO
[arrow.Type.SparseUnion]: never, // TODO
[arrow.Type.FixedSizeBinary]: never, // TODO
[arrow.Type.FixedSizeList]: never, // TODO
[arrow.Type.Map]: never, // TODO
[arrow.Type.Dictionary]: CategoricalSeries<(T extends arrow.Dictionary ? T['valueType'] : any)>
}[T['TType']];
/**
* One-dimensional GPU array
*/
export class AbstractSeries<T extends DataType = any> {
/**
* Create a new cudf.Series from an apache arrow vector
*
* @example
* ```typescript
* import {Series, Int32} from '@rapidsai/cudf';
* import * as arrow from 'apache-arrow';
*
* const arrow_vec = arrow.vectorFromArray(new Int32Array([1,2,3,4])));
* const a = Series.new(arrow_vec); // Int32Series [1, 2, 3, 4]
*
* const arrow_vec_list = arrow.vectorFromArray(
* [[0, 1, 2], [3, 4, 5], [6, 7, 8]],
* new arrow.List(arrow.Field.new({ name: 'ints', type: new arrow.Int32 })),
* );
*
* const b = Series.new(arrow_vec_list) // ListSeries [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
*
* const arrow_vec_struct = arrow.vectorFromArray(
* [{ x: 0, y: 3 }, { x: 1, y: 4 }, { x: 2, y: 5 }],
* new arrow.Struct([
* arrow.Field.new({ name: 'x', type: new arrow.Int32 }),
* arrow.Field.new({ name: 'y', type: new arrow.Int32 })
* ]),
* );
*
* const c = Series.new(arrow_vec_struct);
* // StructSeries [{ x: 0, y: 3 }, { x: 1, y: 4 }, { x: 2, y: 5 }]
* ```
*/
static new<T extends arrow.Vector>(input: T): Series<ArrowToCUDFType<T['type']>>;
/**
* Create a new cudf.Series from SeriesProps or a cudf.Column
*
* @example
* ```typescript
* import {Series, Int32} from '@rapidsai/cudf';
*
* //using SeriesProps
* const a = Series.new({type: new Int32, data: [1, 2, 3, 4]}); // Int32Series [1, 2, 3, 4]
*
* //using underlying cudf.Column
* const b = Series.new(a._col); // Int32Series [1, 2, 3, 4]
* ```
*/
static new<T extends AbstractSeries>(input: T): T;
static new<T extends DataType>(input: Column<T>): Series<T>;
static new<T extends DataType>(input: SeriesProps<T>): Series<T>;
/**
* Create a new cudf.Int8Series
*
* @example
* ```typescript
* import {
* Series,
* Int8Series,
* Int8
* } from '@rapidsai/cudf';
*
* // Int8Series [1, 2, 3]
* const a = Series.new(new Int8Array([1, 2, 3]));
* const b = Series.new(new Int8Buffer([1, 2, 3]));
* ```
*/
static new(input: Int8Array|Int8Buffer): Series<Int8>;
/**
* Create a new cudf.Int16Series
*
* @example
* ```typescript
* import {
* Series,
* Int16Series,
* Int16
* } from '@rapidsai/cudf';
*
* // Int16Series [1, 2, 3]
* const a = Series.new(new Int16Array([1, 2, 3]));
* const b = Series.new(new Int16Buffer([1, 2, 3]));
* ```
*/
static new(input: Int16Array|Int16Buffer): Series<Int16>;
/**
* Create a new cudf.Int32Series
*
* @example
* ```typescript
* import {
* Series,
* Int32Series,
* Int32
* } from '@rapidsai/cudf';
*
* // Int32Series [1, 2, 3]
* const a = Series.new(new Int32Array([1, 2, 3]));
* const b = Series.new(new Int32Buffer([1, 2, 3]));
* ```
*/
static new(input: Int32Array|Int32Buffer): Series<Int32>;
/**
* Create a new cudf.Uint8Series
*
* @example
* ```typescript
* import {
* Series,
* Uint8Series,
* Uint8
* } from '@rapidsai/cudf';
*
* // Uint8Series [1, 2, 3]
* const a = Series.new(new Uint8Array([1, 2, 3]));
* const b = Series.new(new Uint8Buffer([1, 2, 3]));
* const c = Series.new(new Uint8ClampedArray([1, 2, 3]));
* const d = Series.new(new Uint8ClampedBuffer([1, 2, 3]));
* ```
*/
static new(input: Uint8Array|Uint8Buffer|Uint8ClampedArray|Uint8ClampedBuffer): Series<Uint8>;
/**
* Create a new cudf.Uint16Series
*
* @example
* ```typescript
* import {
* Series,
* Uint16Series,
* Uint16
* } from '@rapidsai/cudf';
*
* // Uint16Series [1, 2, 3]
* const a = Series.new(new Uint16Array([1, 2, 3]));
* const b = Series.new(new Uint16Buffer([1, 2, 3]));
* ```
*/
static new(input: Uint16Array|Uint16Buffer): Series<Uint16>;
/**
* Create a new cudf.Uint32Series
*
* @example
* ```typescript
* import {
* Series,
* Uint32Series,
* Uint32
* } from '@rapidsai/cudf';
*
* // Uint32Series [1, 2, 3]
* const a = Series.new(new Uint32Array([1, 2, 3]));
* const b = Series.new(new Uint32Buffer([1, 2, 3]));
* ```
*/
static new(input: Uint32Array|Uint32Buffer): Series<Uint32>;
/**
* Create a new cudf.Uint64Series
*
* @example
* ```typescript
* import {
* Series,
* Uint64Series,
* Uint64
* } from '@rapidsai/cudf';
*
* // Uint64Series [1n, 2n, 3n]
* const a = Series.new(new BigUint64Array([1n, 2n, 3n]));
* const b = Series.new(new Uint64Buffer([1n, 2n, 3n]));
* ```
*/
static new(input: BigUint64Array|Uint64Buffer): Series<Uint64>;
/**
* Create a new cudf.Float32Series
*
* @example
* ```typescript
* import {
* Series,
* Float32Series,
* Float32
* } from '@rapidsai/cudf';
*
* // Float32Series [1, 2, 3]
* const a = Series.new(new Float32Array([1, 2, 3]));
* const b = Series.new(new Float32Buffer([1, 2, 3]));
* ```
*/
static new(input: Float32Array|Float32Buffer): Series<Float32>;
/**
* Create a new cudf.StringSeries
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // StringSeries ["foo", "bar", "test", null]
* const a = Series.new(["foo", "bar", "test", null]);
* ```
*/
static new(input: (string|null|undefined)[]): Series<Utf8String>;
/**
* Create a new cudf.Float64Series
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Float64Series [1, 2, 3, null, 4]
* const a = Series.new([1, 2, 3, undefined, 4]);
* ```
*/
static new(input: (number|null|undefined)[]|Float64Array|Float64Buffer): Series<Float64>;
/**
* Create a new cudf.Int64Series
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Int64Series [1n, 2n, 3n, null, 4n]
* const a = Series.new([1n, 2n, 3n, undefined, 4n]);
* ```
*/
static new(input: (bigint|null|undefined)[]|BigInt64Array|Int64Buffer): Series<Int64>;
/**
* Create a new cudf.Bool8Series
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Bool8Series [true, false, null, false]
* const a = Series.new([true, false, undefined, false]);
* ```
*/
static new(input: (boolean|null|undefined)[]): Series<Bool8>;
/**
* Create a new cudf.TimestampMillisecondSeries
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // TimestampMillisecondSeries [2021-05-13T00:00:00.000Z, null, 2021-05-13T00:00:00.000Z,
* null] const a = Series.new([new Date(), undefined, new Date(), undefined]);
* ```
*/
static new(input: (Date|null|undefined)[]): Series<TimestampMillisecond>;
/**
* Create a new cudf.ListSeries that contain cudf.StringSeries elements.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // ListSeries [["foo", "bar"], ["test", null]]
* const a = Series.new([["foo", "bar"], ["test",null]]);
* a.getValue(0) // StringSeries ["foo", "bar"]
* a.getValue(1) // StringSeries ["test", null]
* ```
*/
static new(input: (string|null|undefined)[][]): Series<List<Utf8String>>;
/**
* Create a new cudf.ListSeries that contain cudf.Float64Series elements.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // ListSeries [[1, 2], [3, null, 4]]
* const a = Series.new([[1, 2], [3, undefined, 4]]);
* a.getValue(0) // Float64Series [1, 2]
* a.getValue(1) // Float64Series [3, null, 4]
* ```
*/
static new(input: (number|null|undefined)[][]): Series<List<Float64>>;
/**
* Create a new cudf.ListSeries that contain cudf.Int64Series elements.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // ListSeries [[1n, 2n], [3n, null, 4n]]
* const a = Series.new([[1n, 2n], [3n, undefined, 4n]]);
* a.getValue(0) // Int64Series [1n, 2n]
* a.getValue(1) // Int64Series [3n, null, 4n]
* ```
*/
static new(input: (bigint|null|undefined)[][]): Series<List<Int64>>;
/**
* Create a new cudf.ListSeries that contain cudf.Bool8Series elements.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // ListSeries [[true, false], [null, false]]
* const a = Series.new([[true, false], [undefined, false]]);
* a.getValue(0) // Bool8Series [true, false]
* a.getValue(1) // Bool8Series [null, false]
* ```
*/
static new(input: (boolean|null|undefined)[][]): Series<List<Bool8>>;
/**
* Create a new cudf.ListSeries that contain cudf.TimestampMillisecondSeries elements.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // ListSeries [[2021-05-13T00:00:00.000Z, null], [null, 2021-05-13T00:00:00.000Z]]
* const a = Series.new([[new Date(), undefined], [undefined, new Date()]]);
* a.getValue(0) // TimestampMillisecondSeries [2021-05-13T00:00:00.000Z, null]
* a.getValue(1) // TimestampMillisecondSeries [null, 2021-05-13T00:00:00.000Z]
* ```
*/
static new(input: (Date|null|undefined)[][]): Series<List<TimestampMillisecond>>;
static new<T extends readonly unknown[]>(input: T):
Series<ArrowToCUDFType<JavaScriptArrayDataType<T>>>;
static new<T extends DataType>(input: AbstractSeries<T>|Column<T>|SeriesProps<T>|arrow.Vector<T>|
(string|null|undefined)[]|(number|null|undefined)[]|
(bigint|null|undefined)[]|(boolean|null|undefined)[]|
(Date|null|undefined)[]|(string|null|undefined)[][]|
(number|null|undefined)[][]|(bigint|null|undefined)[][]|
(boolean|null|undefined)[][]|(Date|null|undefined)[][]): Series<T>;
static new<T extends DataType>(input: any) {
return columnToSeries(asColumn<T>(input)) as any as Series<T>;
}
/**
* Constructs a Series from a text file path.
*
* @note If delimiter is omitted, the default is ''.
*
* @param filepath Path of the input file.
* @param delimiter Optional delimiter.
*
* @returns StringSeries from the file, split by delimiter.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* const infile = Series.readText('./inputAsciiFile.txt')
* ```
*/
public static readText(filepath: string, delimiter: string): Series<Utf8String> {
return Series.new(Column.readText(filepath, delimiter ?? ''));
}
/**
* Constructs a Series with a sequence of values.
*
* @note If init is omitted, the default is 0.
* @note If step is omitted, the default is 1.
* @note If type is omitted, the default is Int32.
*
* @param opts Options for creating the sequence
* @returns Series with the sequence
*
* @example
* ```typescript
* import {Series, Int64, Float32} from '@rapidsai/cudf';
*
* Series.sequence({size: 5}).toArray() // Int32Array[0, 1, 2, 3, 4]
* Series.sequence({size: 5, init: 5}).toArray() // Int32Array[5, 6, 7, 8, 9]
* Series
* .sequence({ size: 5, init: 0, type: new Int64 })
* .toArray() // BigInt64Array[0n, 1n, 2n, 3n, 4n]
* Series
* .sequence({ size: 5, step: 2, init: 1, type: new Float32 })
* .toArray() // Float32Array[1, 3, 5, 7, 9]
* ```
*/
static sequence<U extends Numeric = Int32>(opts: {
size: number;
type?: U; //
init?: U['scalarType'];
step?: U['scalarType'];
memoryResource?: MemoryResource;
}): Series<U> {
const type = opts.type ?? new Int32;
const init = new Scalar({type, value: <any>opts.init ?? 0}) as Scalar<U>;
const step = new Scalar({type, value: <any>opts.step ?? 1}) as Scalar<U>;
return Series.new(Column.sequence<U>(opts.size, init, step, opts.memoryResource));
}
/** @ignore */
declare public _col: Column<T>;
protected constructor(col: Column<T>) { DISPOSER.add(this._col = col); }
/**
* The data type of elements in the underlying data.
*/
get type() { return this._col.type; }
/**
* The DeviceBuffer for for the validity bitmask in GPU memory.
*/
get mask() { return this._col.mask; }
/**
* The offset of elements in this Series underlying Column.
*/
get offset() { return this._col.offset; }
/**
* The number of elements in this Series.
*/
get length() { return this._col.length; }
/**
* A boolean indicating whether a validity bitmask exists.
*/
get nullable() { return this._col.nullable; }
/**
* Whether the Series contains null elements.
*/
get hasNulls() { return this._col.hasNulls; }
/**
* The number of null elements in this Series.
*/
get nullCount() { return this._col.nullCount; }
/**
* The number of child columns in this Series.
*/
get numChildren() { return this._col.numChildren; }
/**
* Casts the values to a new dtype (similar to `static_cast` in C++).
*
* @param dataType The new dtype.
* @param memoryResource The optional MemoryResource used to allocate the result Series's device
* memory.
* @returns Series of same size as the current Series containing result of the `cast` operation.
* @example
* ```typescript
* import {Series, Bool8, Int32} from '@rapidsai/cudf';
*
* const a = Series.new({type:new Int32, data: [1,0,1,0]});
*
* a.cast(new Bool8); // Bool8Series [true, false, true, false];
* ```
*/
cast<R extends DataType>(dataType: R, memoryResource?: MemoryResource): Series<R> {
return new CastVisitor(this, memoryResource).visit(dataType);
}
// clang-format off
/* eslint-disable @typescript-eslint/no-unused-vars */
_castAsBool8(_memoryResource?: MemoryResource): Series<Bool8> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Bool8 unimplemented`); }
_castAsInt8(_memoryResource?: MemoryResource): Series<Int8> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Int8 unimplemented`); }
_castAsInt16(_memoryResource?: MemoryResource): Series<Int16> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Int16 unimplemented`); }
_castAsInt32(_memoryResource?: MemoryResource): Series<Int32> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Int32 unimplemented`); }
_castAsInt64(_memoryResource?: MemoryResource): Series<Int64> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Int64 unimplemented`); }
_castAsUint8(_memoryResource?: MemoryResource): Series<Uint8> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Uint8 unimplemented`); }
_castAsUint16(_memoryResource?: MemoryResource): Series<Uint16> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Uint16 unimplemented`); }
_castAsUint32(_memoryResource?: MemoryResource): Series<Uint32> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Uint32 unimplemented`); }
_castAsUint64(_memoryResource?: MemoryResource): Series<Uint64> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Uint64 unimplemented`); }
_castAsFloat32(_memoryResource?: MemoryResource): Series<Float32> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Float32 unimplemented`); }
_castAsFloat64(_memoryResource?: MemoryResource): Series<Float64> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to Float64 unimplemented`); }
_castAsString(_memoryResource?: MemoryResource): Series<Utf8String> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to String unimplemented`); }
_castAsTimeStampDay(_memoryResource?: MemoryResource): Series<TimestampDay> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to TimeStampDay unimplemented`); }
_castAsTimeStampSecond(_memoryResource?: MemoryResource): Series<TimestampSecond> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to TimeStampSecond unimplemented`); }
_castAsTimeStampMillisecond(_memoryResource?: MemoryResource): Series<TimestampMillisecond> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to TimeStampMillisecond unimplemented`); }
_castAsTimeStampMicrosecond(_memoryResource?: MemoryResource): Series<TimestampMicrosecond> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to TimeStampMicrosecond unimplemented`); }
_castAsTimeStampNanosecond(_memoryResource?: MemoryResource): Series<TimestampNanosecond> { throw new Error(`cast from ${arrow.Type[this.type.typeId]} to TimeStampNanosecond unimplemented`); }
/* eslint-enable @typescript-eslint/no-unused-vars */
// clang-format on
_castAsCategorical<R extends Categorical>(type: R, memoryResource?: MemoryResource): Series<R> {
const categories = scope(() => {
const uniq = scope(() => {
return new DataFrame({value: this, order: Series.sequence({size: this.length})})
.groupBy({by: 'value'})
.nth(0);
});
return uniq.sortValues({order: {ascending: true}}, memoryResource).get('value');
}, [this]);
const codes = this.encodeLabels(categories, undefined, undefined, memoryResource);
return Series.new<R>({
type,
length: codes.length,
nullMask: this.mask,
children: [codes, categories.cast(type.dictionary)]
});
}
/**
* Concat a Series to the end of the caller, returning a new Series of a common dtype.
*
* @param other The Series to concat to the end of the caller.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* Series.new([1, 2, 3]).concat(Series.new([4, 5, 6])) // [1, 2, 3, 4, 5, 6]
* ```
*/
concat<R extends Series<DataType>>(other: R, memoryResource?: MemoryResource):
Series<CommonType<T, R['type']>> {
type U = typeof type;
const type = findCommonType(this.type, other.type);
const lhs = <Column<U>>(compareTypes(type, this.type) ? this._col : this.cast(type)._col);
const rhs = <Column<U>>(compareTypes(type, other.type) ? other._col : other.cast(type)._col);
return Series.new(lhs.concat(rhs, memoryResource));
}
/**
* Return the number of non-null elements in the Series.
*
* @returns The number of non-null elements
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* Series.new([1, 2, 3]).countNonNulls(); // 3
* Series.new([1, null, 3]).countNonNulls(); // 2
* ```
*/
countNonNulls(): number { return this._col.length - this._col.nullCount; }
/**
* @summary Explicitly free the device memory associated with this Series.
*/
dispose() { this._col.dispose(); }
/**
* Encode the Series values into integer labels.
*
*
* @param categories The optional Series of values to encode into integers. Defaults to the
* unique elements in this Series.
* @param type The optional integer DataType to use for the returned Series. Defaults to
* Uint32.
* @param nullSentinel The optional value used to indicate missing category. Defaults to -1.
* @param memoryResource The optional MemoryResource used to allocate the result Column's
* device memory.
* @returns A sequence of encoded integer labels with values between `0` and `n-1`
* categories, and `nullSentinel` for any null values
*/
encodeLabels<R extends Integral = Uint32>(categories: Series<T> = this.unique(true),
type: R = new Uint32 as R,
nullSentinel: R['scalarType'] = -1,
memoryResource?: MemoryResource): Series<R> {
return scope(() => {
try {
// If there is a failure casting to the current dtype, catch the exception and return
// encoded labels with all values set to `nullSentinel`, since this means the Column
// cannot contain any of the encoded categories.
if (!compareTypes(this.type, categories.type)) { categories = categories.cast(this.type); }
} catch {
return Series.sequence(
{type, init: nullSentinel, step: 0, memoryResource, size: this.length});
}
// 1. Join this Series' values with the `categories` Series to determine the index
// positions (i.e. `codes`) of the values to keep.
const codes = scope(() => {
const lhs = new DataFrame(
{value: this, order: Series.sequence({type: new Uint32, size: this.length})});
const rhs = new DataFrame(
{value: categories, codes: Series.sequence({type, size: categories.length})});
return lhs.join({on: ['value'], how: 'left', nullEquality: true, other: rhs})
.drop(['value'])
// 2. Sort the codes by the original value's position in this Series.
.sortValues({order: {ascending: true}})
.get('codes');
});
// 3. Replace missing codes with `nullSentinel`.
return codes.replaceNulls(nullSentinel, memoryResource) as Series<R>;
}, [this, categories]);
}
/**
* Fills a range of elements in a column out-of-place with a scalar value.
*
* @param begin The starting index of the fill range (inclusive).
* @param end The index of the last element in the fill range (exclusive), default
* this.length
* .
* @param value The scalar value to fill.
* @param memoryResource The optional MemoryResource used to allocate the result Column's
* device memory.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Float64Series
* Series.new([1, 2, 3]).fill(0) // [0, 0, 0]
* // StringSeries
* Series.new(["foo", "bar", "test"]).fill("rplc", 0, 1) // ["rplc", "bar", "test"]
* // Bool8Series
* Series.new([true, true, true]).fill(false, 1) // [true, false, false]
* ```
*/
fill(value: T['scalarType'], begin = 0, end = this.length, memoryResource?: MemoryResource):
Series<T> {
return this.__construct(
this._col.fill(new Scalar({type: this.type, value}), begin, end, memoryResource));
}
/**
* Repeats a Series n times, returning a new Series.
*
* @param repeats The number of times to repeat this.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Float64Series
* Series.new([1, 2, 3]).repeat(2) // [1, 2, 3, 1, 2, 3]
* // StringSeries
* Series.new(["foo", "bar", "test"]).repeat(2) // ["foo", "bar", "test", "foo", "bar", "test"]
* // Bool8Series
* Series.new([true, true, true]).repeat(2) // [true, false, false, true, false, false]
* ```
*/
repeat(repeats: T['scalarType'], memoryResource?: MemoryResource): Series<T> {
return this.__construct(this._col.repeat(repeats, memoryResource));
}
/**
* Fills a range of elements in-place in a column with a scalar value.
*
* @param begin The starting index of the fill range (inclusive)
* @param end The index of the last element in the fill range (exclusive)
* @param value The scalar value to fill
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Float64Series
* Series.new([1, 2, 3]).fillInPlace(0) // [0, 0, 0]
* // StringSeries
* Series.new(["foo", "bar", "test"]).fillInPlace("rplc", 0, 1) // ["rplc", "bar", "test"]
* // Bool8Series
* Series.new([true, true, true]).fillInPlace(false, 1) // [true, false, false]
* ```
*/
fillInPlace(value: T['scalarType'], begin = 0, end = this.length) {
this._col.fillInPlace(new Scalar({type: this.type, value}), begin, end);
return this;
}
/**
* Replace null values with a scalar value.
*
* @param value The scalar value to use in place of nulls.
* @param memoryResource The optional MemoryResource used to allocate the result Column's
* device memory.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Float64Series
* Series.new([1, null, 3]).replaceNulls(-1) // [1, -1, 3]
* // StringSeries
* Series.new(["foo", "bar", null]).replaceNulls("rplc") // ["foo", "bar", "rplc"]
* // Bool8Series
* Series.new([null, true, true]).replaceNulls(false) // [true, true, true]
* ```
*/
replaceNulls(value: T['scalarType']|any, memoryResource?: MemoryResource): Series<T>;
/**
* Replace null values with the corresponding elements from another Series.
*
* @param value The Series to use in place of nulls.
* @param memoryResource The optional MemoryResource used to allocate the result Column's
* device memory.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
* const replace = Series.new([10, 10, 10]);
* const replaceBool = Series.new([false, false, false]);
*
* // Float64Series
* Series.new([1, null, 3]).replaceNulls(replace) // [1, 10, 3]
* // StringSeries
* Series.new(["foo", "bar", null]).replaceNulls(replace) // ["foo", "bar", "10"]
* // Bool8Series
* Series.new([null, true, true]).replaceNulls(replaceBool) // [false, true, true]
* ```
*/
replaceNulls(value: Series<T>, memoryResource?: MemoryResource): Series<T>;
replaceNulls(value: any, memoryResource?: MemoryResource): Series<T> {
if (value instanceof Series) {
return this.__construct(this._col.replaceNulls(value._col, memoryResource));
} else {
return this.__construct(
this._col.replaceNulls(new Scalar({type: this.type, value}), memoryResource));
}
}
/**
* Replace null values with the non-null value following the null value in the same series.
*
* @param memoryResource The optional MemoryResource used to allocate the result Column's
* device memory.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Float64Series
* Series.new([1, null, 3]).replaceNullsFollowing() // [1, 3, 3]
* // StringSeries
* Series.new(["foo", "bar", null]).replaceNullsFollowing() // ["foo", "bar", null]
* Series.new(["foo", null, "bar"]).replaceNullsFollowing() // ["foo", "bar", "bar"]
* // Bool8Series
* Series.new([null, true, true]).replaceNullsFollowing() // [true, true, true]
* ```
*/
replaceNullsFollowing(memoryResource?: MemoryResource): Series<T> {
return this.__construct(this._col.replaceNulls(true, memoryResource));
}
/**
* Replace null values with the non-null value preceding the null value in the same series.
*
* @param memoryResource The optional MemoryResource used to allocate the result Column's
* device memory.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Float64Series
* Series.new([1, null, 3]).replaceNullsPreceding() // [1, 1, 3]
* // StringSeries
* Series.new([null, "foo", "bar"]).replaceNullsPreceding() // [null, "foo", "bar"]
* Series.new(["foo", null, "bar"]).replaceNullsPreceding() // ["foo", "foo", "bar"]
* // Bool8Series
* Series.new([true, null, false]).replaceNullsPreceding() // [true, true, false]
* ```
*/
replaceNullsPreceding(memoryResource?: MemoryResource): Series<T> {
return this.__construct(this._col.replaceNulls(false, memoryResource));
}
/**
* Returns a new series with reversed elements.
*
* @param memoryResource An optional MemoryResource used to allocate the result's device memory.
*
* @example
* ```typescript
* import {Series} from '@rapidsai/cudf';
*
* // Float64Series
* Series.new([1, 2, 3]).reverse() // [3, 2, 1]
* // StringSeries
* Series.new(["foo", "bar"]).reverse() // ["bar", "foo"]
* // Bool8Series
* Series.new([false, true]).reverse() // [true, false]
* ```
*/
reverse(memoryResource?: MemoryResource): Series<T> {
return this.gather(
Series.sequence({size: this.length, step: -1, init: this.length - 1}), false, memoryResource);
}
/**
* @summary Return sub-selection from a Series using the specified integral indices.
*
* @description Gathers the rows of the source columns according to `selection`, such that row "i"
* in the resulting Series's columns will contain row `selection[i]` from the source columns. The
* number of rows in the result series will be equal to the number of elements in selection. A
* negative value i in the selection is interpreted as i+n, where `n` is the number of rows in
* the source series.
*
* For dictionary columns, the keys column component is copied and not trimmed if the gather
* results in abandoned key elements.
*
* @param indices A Series of 8/16/32-bit signed or unsigned integer indices to gather.
* @param nullify_out_of_bounds If `true`, coerce rows that corresponds to out-of-bounds indices
* in the selection to null. If `false`, skips all bounds checking for selection values. Pass
* false if you are certain that the selection contains only valid indices for better
* performance. If `false` and there are out-of-bounds indices in the selection, the behavior
* is undefined. Defaults to `false`.
* @param memoryResource An optional MemoryResource used to allocate the result's device memory.
*
* @example