-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathseries.test.ts
1758 lines (1604 loc) · 73.9 KB
/
series.test.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 { assert, expect } from "chai";
import { describe, it } from "mocha";
import { Series } from "../../dist/danfojs-node/src";
describe("Series Functions", () => {
describe("head", function () {
it("Gets the first 2 rows in a Series", function () {
const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
const cols = ["A"];
const sf = new Series(data, { columns: cols });
assert.deepEqual(sf.head(2).values, [1, 2]);
assert.deepEqual(sf.head(5).values, [1, 2, 3, 4, 5]);
});
it("throw error when row specified is less than 0", function () {
const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
const sf = new Series(data);
assert.throws(function () { assert.deepEqual(sf.head(-1).values, data) }, Error, `ParamError: Number of rows cannot be less than 1`);
});
it("Returns all rows when DataFrame length is less than rows", function () {
const data = [ 1, 2, 5 ];
const sf = new Series(data);
assert.deepEqual(sf.head().values, data);
});
});
describe("tail", function () {
it("Prints the last n rows of a Series", function () {
const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
const sf = new Series(data);
assert.deepEqual(sf.tail(2).values, [89, 78]);
assert.deepEqual(sf.tail(4).values, [40, 39, 89, 78]);
});
it("throw error when row specified is less than 0", function () {
const data = ["Boy", "Girl", "Man", "Woman", "Tall"];
const cols = ["Items"];
const sf = new Series(data, { columns: cols });
assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data) }, Error, `ParamError: Number of rows cannot be less than 1`);
});
it("Returns all rows when DataFrame length is less than rows", function () {
const data = [ 1, 2, 5 ];
const sf = new Series(data);
assert.deepEqual(sf.tail().values, data);
});
});
describe("sample", function () {
it("Samples n number of random elements from a DataFrame", async function () {
const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
const sf = new Series(data);
assert.deepEqual((await sf.sample(7)).values.length, 7);
});
it("Return all values if n of sample -1", async function () {
const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
const sf = new Series(data);
assert.deepEqual((await sf.sample(-1)).values.length, data.length);
});
it("Throw error if n is greater than length of Series", async function () {
const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
const sf = new Series(data);
try {
await sf.sample(100);
} catch (e: any) {
expect(e.message).to.eql('Sample size n cannot be bigger than size of dataset');
}
});
});
describe("add", function () {
it("Return Addition of series with another series", function () {
const data = [1, 2, 3, 4, 5, 6];
const data2 = [30, 40, 39, 1, 2, 1];
const sf = new Series(data);
const sf2 = new Series(data2);
assert.deepEqual((sf.add(sf2)).values, [31, 42, 42, 5, 7, 7]);
});
it("Return Addition of series with a single value (Broadcasting)", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
assert.deepEqual((sf.add(1)).values, [2, 3, 4, 5, 6]);
});
it("Return Addition of series with another series inplace", function () {
const data = [1, 2, 3, 4, 5, 6];
const data2 = [30, 40, 39, 1, 2, 1];
const sf = new Series(data);
const sf2 = new Series(data2);
sf.add(sf2, { inplace: true })
assert.deepEqual(sf.values, [31, 42, 42, 5, 7, 7]);
});
it("Return Addition of series with a single value (Broadcasting) inplace", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
sf.add(1, { inplace: true })
assert.deepEqual(sf.values, [2, 3, 4, 5, 6]);
});
it("Dtype is properly updated on addition of series with a single float value inplace", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
sf.add(1.23, { inplace: true })
assert.deepEqual(sf.dtypes[0], "float32");
assert.deepEqual(sf.values, [2.23, 3.23, 4.23, 5.23, 6.23]);
});
it("Add works properly when using tfjs add function", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
sf.add(1.23, { inplace: true })
assert.deepEqual(sf.dtypes[0], "float32");
assert.deepEqual(sf.values, [2.23, 3.23, 4.23, 5.23, 6.23]);
});
it("Add works properly when using tfjs add function on Series", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
const sf2 = new Series([1.23, 1.23, 1.23, 1.23, 1.23]);
sf.add(sf2, { inplace: true })
assert.deepEqual(sf.values, [2.23, 3.23, 4.23, 5.23, 6.23]);
});
it("Throws type error on addition of string type", function () {
const data = [1, 2, 3, 4];
const data2 = ["A", "B", "C", "d"];
const sf = new Series(data);
const sf2 = new Series(data2);
assert.throws(
() => {
sf.add(sf2);
},
Error,
"DtypeError: String data type does not support add operation"
);
});
it("Throws length error if series length mixmatch", function () {
const data = [1, 2, 3, 4]
const data2 = [1, 2, 3, 4, 5, 6]
const sf = new Series(data)
const sf2 = new Series(data2)
assert.throws(() => { sf.add(sf2) }, Error, "Row length mismatch. Length of other (6), must be the same as Ndframe (4)")
})
});
describe("sub", function () {
it("Return Subtraction of series with another series", function () {
const data1 = [30, 40, 39, 1, 2, 1];
const data2 = [1, 2, 3, 4, 5, 6];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.deepEqual((sf1.sub(sf2)).values, [29, 38, 36, -3, -3, -5]);
});
it("Return Subtraction of series with a single value (Broadcasting)", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
assert.deepEqual((sf.sub(1)).values, [0, 1, 2, 3, 4]);
});
it("Throws type error on Subtraction of string type", function () {
const data = [1, 2, 3, 4];
const data2 = ["A", "B", "C", "d"];
const sf = new Series(data);
const sf2 = new Series(data2);
assert.throws(
() => {
sf.sub(sf2);
},
Error,
"DtypeError: String data type does not support sub operation"
);
});
it("Throws length error if series length mixmatch", function () {
const data = [1, 2, 3, 4]
const data2 = [1, 2, 3, 4, 5, 6]
const sf = new Series(data)
const sf2 = new Series(data2)
assert.throws(() => { sf.sub(sf2) }, Error, "Row length mismatch. Length of other (6), must be the same as Ndframe (4)")
})
});
describe("mul", function () {
it("Return multiplication of series with another series", function () {
const data1 = [30, 40, 3, 5];
const data2 = [1, 2, 3, 4];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.deepEqual((sf1.mul(sf2)).values, [30, 80, 9, 20]);
});
it("Return multiplication of series with a single value (Broadcasting)", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
assert.deepEqual((sf.mul(1)).values, [1, 2, 3, 4, 5]);
});
it("Throws type error on multiplication of string type", function () {
const data = [1, 2, 3, 4]
const data2 = ["A", "B", "C", "d"]
const sf = new Series(data)
const sf2 = new Series(data2)
assert.throws(() => { sf.mul(sf2) }, Error, "DtypeError: String data type does not support mul operation")
})
it("Throws length error if series length mixmatch", function () {
const data = [1, 2, 3, 4]
const data2 = [1, 2, 3, 4, 5, 6]
const sf = new Series(data)
const sf2 = new Series(data2)
assert.throws(() => { sf.mul(sf2) }, Error, "ParamError: Row length mismatch. Length of other (6), must be the same as Ndframe (4)")
})
});
describe("div", function () {
it("Return float division of series with another series", function () {
const data1 = [30, 40, 3, 5];
const data2 = [1, 2, 3, 4];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.deepEqual((sf1.div(sf2)).values, [30, 20, 1, 1.25]);
});
it("Return integer division of series with another series", function () {
const data1 = [30, 40, 3, 5];
const data2 = [1, 2, 3, 4];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.deepEqual((sf1.div(sf2)).values, [30, 20, 1, 1.25]);
});
it("Return division of series with a single value (Broadcasting)", function () {
const data = [10, 2, 3, 90];
const sf = new Series(data);
assert.deepEqual((sf.div(2)).values, [5, 1, 1.5, 45]);
});
it("Throws type error on division of string type", function () {
const data = [1, 2, 3, 4]
const data2 = ["A", "B", "C", "d"]
const sf = new Series(data)
const sf2 = new Series(data2)
assert.throws(() => { sf.div(sf2) }, Error, `DtypeError: String data type does not support div operation`)
})
it("Throws length error if series length mixmatch", function () {
const data = [1, 2, 3, 4]
const data2 = [1, 2, 3, 4, 5, 6]
const sf = new Series(data)
const sf2 = new Series(data2)
assert.throws(() => { sf.div(sf2) }, Error, "ParamError: Row length mismatch. Length of other (6), must be the same as Ndframe (4)")
})
});
describe("pow", function () {
it("Return Exponetial power of series with another series", function () {
const data1 = [2, 3, 4, 5];
const data2 = [1, 2, 3, 0];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.deepEqual((sf1.pow(sf2)).values, [2, 9, 64, 1]);
});
it("Return Exponetial power of series with a single value (Broadcasting)", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
assert.deepEqual((sf.pow(2)).values, [1, 4, 9, 16, 25]);
});
});
describe("mod", function () {
it("Return modulo of series with another float series", function () {
const data1 = [2, 30, 4, 5];
const data2 = [1.1, 2.2, 3.3, 2.4];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
const expected = [
0.8999999999999999,
1.3999999999999977,
0.7000000000000002,
0.20000000000000018
];
assert.deepEqual((sf1.mod(sf2)).values, expected);
});
it("Return modulo of series with another int series", function () {
const data1 = [2, 30, 4, 5];
const data2 = [1, 2, 3, 1];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.deepEqual((sf1.mod(sf2)).values, [0, 0, 1, 0]);
});
it("Return modulo power of series with a single value (Broadcasting)", function () {
const data = [1, 2, 3, 4, 5];
const sf = new Series(data);
assert.deepEqual((sf.mod(2)).values, [1, 0, 1, 0, 1]);
});
});
describe("toString", function () {
it("Prints a series to the console", async function () {
const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
const sf = new Series(data);
sf.print()
});
it("Prints a series to the console", async function () {
const data = ["This is a long text group and I want it to print in full",
"This is a long text group and I want it to print in full"];
const sf = new Series(data);
sf.print()
});
});
describe("Empty Series", function () {
it("Can successfully create an empty Series from empty array", function () {
const data: any = [];
const sf = new Series(data);
assert.deepEqual(sf.shape, [0, 0]);
assert.deepEqual(sf.columns, []);
assert.deepEqual(sf.dtypes, []);
assert.deepEqual(sf.values, []);
});
it("Can successfully create an empty Series", function () {
const sf = new Series();
assert.deepEqual(sf.shape, [0, 0]);
assert.deepEqual(sf.columns, []);
assert.deepEqual(sf.dtypes, []);
assert.deepEqual(sf.values, []);
});
});
describe("mean", function () {
it("Computes the mean of elements in a int series", function () {
const data1 = [30, 40, 3, 5, NaN];
const sf = new Series(data1);
assert.deepEqual(sf.mean(), 19.5);
});
it("Computes the mean of elements in a int series", function () {
const data1 = [30, 40, 3, 5, NaN];
const sf = new Series(data1);
assert.deepEqual(sf.mean(), 19.5);
});
it("Computes the mean of elements in a float series", function () {
const data1 = [30.1, 40.2, 3.1, 5.1];
const sf = new Series(data1);
assert.deepEqual(sf.mean(), 19.625);
});
it("Computes the mean of elements in a float series with NaN", function () {
const data1 = [30.1, 40.2, 3.1, 5.1, NaN];
const sf = new Series(data1);
assert.deepEqual(sf.mean(), 19.625);
});
it("Computes the mean of a boolean series", function () {
const data1 = [true, false, false, false, true, true, false, true];
const sf = new Series(data1);
assert.deepEqual(sf.mean(), 0.5);
});
it("Throws error if dtype is string", function () {
const data1 = ["boy", "girl", "Man"];
const sf = new Series(data1);
assert.throws(
() => {
sf.mean();
},
Error,
"DtypeError: String data type does not support mean operation"
);
});
});
describe("median", function () {
it("Computes the median value of elements across int Series", function () {
const data1 = [30, 40, 3, 5];
const sf = new Series(data1);
assert.deepEqual(sf.median(), 17.5);
});
it("Computes the median value of elements across float Series", function () {
const data1 = [30.1, 40.2, 3.1, 5.1, NaN];
const sf = new Series(data1);
assert.deepEqual(sf.median(), 17.6);
});
});
describe("sum", function () {
it("Sum values of a Int Series", function () {
const data1 = [30, 40, 3, 5, 5, 5, 5, 5, 3, 3, 3, 21, 3];
const sf = new Series(data1);
assert.deepEqual(sf.sum(), 131);
});
it("Sum values of a Float Series", function () {
const data1 = [30.1, 3.1, 40.2, 3.1, 5.1];
const sf = new Series(data1);
assert.deepEqual(sf.sum(), 81.6);
});
it("Sum values of a bool Series", function () {
const data1 = [true, true, false, false, false];
const sf = new Series(data1);
assert.deepEqual(sf.sum(), 2);
});
it("Sum values a Series with missing values", function () {
const data1 = [11, NaN, 2, 2];
const sf = new Series(data1);
assert.deepEqual(sf.sum(), 15);
});
});
describe("mode", function () {
it("Computes the multi-modal values of a Series", function () {
const data1 = [30, 40, 3, 5, 5, 5, 5, 5, 3, 3, 3, 21, 3];
const sf = new Series(data1);
assert.deepEqual(sf.mode(), [5, 3]);
});
it("Computes the modal value of a Series", function () {
const data1 = [30.1, 3.1, 40.2, 3.1, 5.1];
const sf = new Series(data1);
assert.deepEqual(sf.mode(), [3.1]);
});
});
describe("min", function () {
it("Returns the single smallest elementin a Series", function () {
const data = [30, 40, 3, 5];
const sf = new Series(data);
assert.deepEqual(sf.min(), 3);
});
it("Computes the minimum of elements across an float Series", function () {
const data1 = [30.1, 40.2, 3.12, 5.1];
const sf = new Series(data1, { dtypes: ["float32"] });
assert.deepEqual(Number(sf.min().toFixed(2)), 3.12);
});
});
describe("max", function () {
it("Computes the maximum of elements across dimensions of a Series", function () {
const data1 = [30, 40, 3, 5];
const sf = new Series(data1);
assert.deepEqual(sf.max(), 40);
});
it("Return sum of float values in a series", function () {
const data1 = [30.1, 40.21, 3.1, 5.1];
const sf = new Series(data1);
assert.deepEqual(Number(sf.max().toFixed(2)), 40.21);
});
it("Throws error on addition of string Series", function () {
const data1 = ["boy", "gitl", "woman", "man"];
const sf = new Series(data1);
assert.throws(
() => {
sf.max();
},
Error,
"DtypeError: String data type does not support max operation"
);
});
});
describe("count", function () {
it("Returns the count of non NaN values in a string Series", function () {
const data = ["boy", "gitl", "woman", NaN];
const sf = new Series(data);
assert.deepEqual(sf.count(), 3);
});
it("Returns the count of values in a string Series without NaN", function () {
const data = ["boy", "gitl", "woman", "Man"];
const sf = new Series(data);
assert.deepEqual(sf.count(), 4);
});
it("Returns the count of non NaN values in a int Series", function () {
const data = [20, 30, NaN, 2, NaN, 30, 21];
const sf = new Series(data);
assert.deepEqual(sf.count(), 5);
});
it("Returns the count of non NaN values in a float Series", function () {
const data = [20.1, 30.4, NaN, 2.1, NaN, 30.0, 21.3];
const sf = new Series(data);
assert.deepEqual(sf.count(), 5);
});
});
describe("std", function () {
it("Computes the standard of elements in a int series", function () {
const data1 = [30, 40, 3, 5];
const sf = new Series(data1);
assert.deepEqual(sf.std(), 18.375708603116962);
});
it("Computes the standard deviation of elements in a float series", function () {
const data1 = [30.1, 40.2, 3.1, 5.1];
const sf = new Series(data1);
assert.deepEqual(sf.std(), 18.412925713566906);
});
it("Computes the standard deviation of elements in a float series with missing values", function () {
const data1 = [30, 40, 3, 5, undefined];
const sf = new Series(data1);
assert.deepEqual(sf.std(), 18.375708603116962);
});
});
describe("var", function () {
it("Computes the variance of elements in a int series", function () {
const data1 = [30, 40, 3, 5];
const sf = new Series(data1);
assert.deepEqual(sf.var(), 337.6666666666667);
});
it("Computes the variance of elements in a float series", function () {
const data1 = [30.1, 40.2, 3.1, 5.1];
const sf = new Series(data1);
assert.deepEqual(sf.var(), 339.03583333333336);
});
it("Computes the variance of elements in a int series with missing values", function () {
const data1 = [30, undefined, 40, 3, 5];
const sf = new Series(data1);
assert.deepEqual(sf.var(), 337.6666666666667);
});
});
describe("round", function () {
it("Rounds elements in a Series to nearest whole number", function () {
const data1 = [30.21091, 40.190901, 3.564, 5.0212];
const sf = new Series(data1);
assert.deepEqual((sf.round()).values, [30.2, 40.2, 3.6, 5]);
});
it("Rounds elements in a Series to 1dp", function () {
const data1 = [30.21091, 40.190901, 3.564, 5.0212];
const sf = new Series(data1);
assert.deepEqual((sf.round(1)).values, [30.2, 40.2, 3.6, 5.0]);
});
it("Rounds elements in a Series to 2dp", function () {
const data1 = [30.2191, 40.190901, 3.564, 5.0212];
const sf = new Series(data1);
assert.deepEqual((sf.round(2)).values, [30.22, 40.19, 3.56, 5.02]);
});
it("Rounds elements in a Series to 2dp inplace", function () {
const data1 = [30.2191, 40.190901, 3.564, 5.0212];
const sf = new Series(data1);
sf.round(2, { inplace: true })
assert.deepEqual(sf.values, [30.22, 40.19, 3.56, 5.02]);
});
it("Rounds elements in a Series with missing values to 2dp", function () {
const data1 = [30.2191, undefined, 3.564, NaN];
const sf = new Series(data1);
sf.round(2, { inplace: true })
assert.deepEqual(sf.values as number[], [30.22, undefined, 3.56, NaN]);
});
});
describe("maximum", function () {
it("Returns the maximum of two series", function () {
const data1 = [30, 40, 3, 5];
const data2 = [10, 41, 2, 0];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.deepEqual(sf1.maximum(sf2).values, [30, 41, 3, 5]);
});
it("Returns the maximum of series and Array", function () {
const data1 = [30, 40, 3, 5];
const data2 = [10, 41, 2, 0];
const sf1 = new Series(data1);
assert.deepEqual(sf1.maximum([10, 41, 2, 0]).values, [30, 41, 3, 5]);
});
it("Returns the maximum of series and scalar", function () {
const data1 = [30, 40, 3, 5];
const sf1 = new Series(data1);
assert.deepEqual(sf1.maximum(10).values, [30, 40, 10, 10]);
});
it("Throws error on checking maximum of incompatible Series", function () {
const data1 = [30, 40, 3, 5];
const data2 = [10, 41, 2];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.throws(
() => {
sf1.maximum(sf2);
},
Error,
"ParamError: Row length mismatch. Length of other (3), must be the same as Ndframe (4)"
);
});
});
describe("minimum", function () {
it("Returns the minimum of two series", function () {
const data1 = [30, 40, 3, 5];
const data2 = [10, 41, 2, 0];
const sf1 = new Series(data1);
const sf2 = new Series(data2);
assert.deepEqual(sf1.minimum(sf2).values, [10, 40, 2, 0]);
});
it("Returns the minimum of series and array", function () {
const data1 = [30, 40, 3, 5];
const data2 = [10, 41, 2, 0];
const sf1 = new Series(data1);
assert.deepEqual(sf1.minimum(data2).values, [10, 40, 2, 0]);
});
it("Returns the minimum of two series", function () {
const data1 = [30, 40, 3, 5];
const sf1 = new Series(data1);
assert.deepEqual(sf1.minimum(10).values, [10, 10, 3, 5]);
});
});
describe("isNa", function () {
it("Return a boolean same-sized object indicating if string Series contain NaN", function () {
const data1 = [NaN, undefined, "girl", "Man"];
const sf = new Series(data1);
assert.deepEqual(sf.isNa().values, [true, true, false, false]);
});
it("Return a boolean same-sized object indicating if float Series values are NaN", function () {
const data1 = [30.21091, NaN, 3.564, undefined];
const sf = new Series(data1);
assert.deepEqual(sf.isNa().values, [false, true, false, true]);
});
it("Return a boolean same-sized object indicating if int Series values are NaN", function () {
const data1 = [30, 40, 3, 5, null, undefined];
const sf = new Series(data1);
assert.deepEqual(sf.isNa().values, [
false,
false,
false,
false,
true,
true
]);
});
});
describe("fillNa", function () {
it("replace all NaN value with specified value", function () {
const data = [NaN, 1, 2, 33, 4, NaN, 5, 6, 7, 8];
const sf = new Series(data);
const sfVal = [-999, 1, 2, 33, 4, -999, 5, 6, 7, 8];
sf.fillNa(-999, { inplace: true });
assert.deepEqual(sf.values, sfVal);
});
it("replace all NaN value in string Series with specified value", function () {
const data = [NaN, "boy", null, "hey", "Man", undefined];
const sf = new Series(data);
const sfVal = ["filled", "boy", "filled", "hey", "Man", "filled"];
const sfFill = sf.fillNa("filled");
assert.deepEqual((sfFill).values, sfVal);
});
it("Data is in right format after filling", function () {
const data = [NaN, "boy", NaN, "hey", "Man", undefined];
const sf = new Series(data);
const sfVal = ["filled", "boy", "filled", "hey", "Man", "filled"];
const sfFill = sf.fillNa("filled");
assert.deepEqual((sfFill).values, sfVal);
});
});
describe("sortValues", function () {
it("Sort values in a Series in ascending order (not inplace)", function () {
const sf = new Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
const result = [0, 1, 2, 4, 4, 20, 30, 57, 89];
const sortedSf = sf.sortValues();
assert.deepEqual((sortedSf).values, result);
});
it("confirms that sortValues in ascending order does not happen inplace", function () {
const sf = new Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
const result = [0, 1, 2, 4, 4, 20, 30, 57, 89];
const expectedIndex = [7, 2, 3, 8, 4, 0, 1, 5, 6];
sf.sortValues({ ascending: true, inplace: true });
assert.deepEqual(sf.values, result);
assert.deepEqual(sf.index, expectedIndex);
});
it("Sort values in a Series in Descending order", function () {
const sf = new Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
const result = [89, 57, 30, 20, 4, 4, 2, 1, 0];
const sortedSf = sf.sortValues({ ascending: false });
assert.deepEqual((sortedSf).values, result);
});
it("confirms that sortValues in descending order happens inplace", function () {
const sf = new Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
const result = [89, 57, 30, 20, 4, 4, 2, 1, 0];
sf.sortValues({ ascending: false, inplace: true });
assert.deepEqual(sf.values, result);
});
it("Confirms that series index is sorted in ascending order (not in inplace)", function () {
const sf = new Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
const result = [7, 2, 3, 8, 4, 0, 1, 5, 6];
const sortedSf = sf.sortValues()
assert.deepEqual(sortedSf.index, result);
});
it("Confirms that series index is sorted in descending order (not in inplace)", function () {
const sf = new Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
const result = [6, 5, 1, 0, 4, 8, 3, 2, 7];
const sortedSf = sf.sortValues({ ascending: false })
assert.deepEqual(sortedSf.index, result);
});
it("Sort string values in a Series", function () {
const sf = new Series(["boy", "zebra", "girl", "man"]);
const result = ["boy", "girl", "man", "zebra"];
const sortedSf = sf.sortValues({ ascending: false })
assert.deepEqual(sortedSf.values, result);
});
it("Index is retained after sort (ascending=true)", function () {
let index = ["apple", "banana", "orange", "grape"];
let value = [3, 6, 2, 9];
let sf = new Series(value, { index });
sf.sortValues().print();
const expectedValues = [2, 3, 6, 9];
const expectedIndex = ["orange", "apple", "banana", "grape"];
const sortedSf = sf.sortValues()
assert.deepEqual(sortedSf.values, expectedValues);
assert.deepEqual(sortedSf.index, expectedIndex);
});
it("Index is retained after sort (ascending=false)", function () {
let index = ["apple", "banana", "orange", "grape"];
let value = [3, 6, 2, 9];
let sf = new Series(value, { index });
sf.sortValues().print();
const expectedValues = [9, 6, 3, 2];
const expectedIndex = ["grape", "banana", "apple", "orange"];
const sortedSf = sf.sortValues({ ascending: false })
assert.deepEqual(sortedSf.values, expectedValues);
assert.deepEqual(sortedSf.index, expectedIndex);
});
it("Index is retained after inplace sort (ascending=false)", function () {
let index = ["apple", "banana", "orange", "grape"];
let value = [3, 6, 2, 9];
let sf = new Series(value, { index });
sf.sortValues().print();
const expectedValues = [9, 6, 3, 2];
const expectedIndex = ["grape", "banana", "apple", "orange"];
sf.sortValues({ ascending: false, inplace: true });
assert.deepEqual(sf.values, expectedValues);
assert.deepEqual(sf.index, expectedIndex);
});
});
describe("describe", function () {
it("Computes the descriptive statistics on an int Series", function () {
const data1 = [10, 45, 56, 25, 23, 20, 10];
const sf = new Series(data1)
assert.deepEqual(sf.describe().round().values, [
7,
27,
17.4,
10,
23,
56,
302
]);
});
it("Computes the descriptive statistics on a float Series", function () {
const data1 = [30.1, 40.2, 3.1, 5.1];
const sf = new Series(data1);
assert.deepEqual(sf.describe().round().values, [
4,
19.6,
18.4,
3.1,
17.6,
40.2,
339
]);
});
it("Computes the descriptive statistics on a float Series", function () {
const data1 = [30.1, 40.2, 3.1, 5.1];
const sf = new Series(data1);
assert.deepEqual(sf.describe().index, [
"count",
"mean",
"std",
"min",
"median",
"max",
"variance"
]);
});
});
describe("resetIndex", function () {
it("resets the index of a Series", function () {
const data = [
{ alpha: "A", count: 1 },
{ alpha: "B", count: 2 },
{ alpha: "C", count: 3 }
];
const df = new Series(data, { index: ["one", "two", "three"] });
const dfReset = df.resetIndex()
assert.deepEqual(dfReset.index, [0, 1, 2]);
});
it("Reset the index of a Series created from an Array", function () {
const data = [1, 2, 3, 4, 5, 6];
const df = new Series(data, { index: ["one", "two", "three", "four", "five", "six"] });
const dfNew = df.resetIndex()
assert.deepEqual(dfNew.index, [0, 1, 2, 3, 4, 5]);
});
it("checks that the original series changed after reseting new index inplace", function () {
const data = [
{ index: "A", count: 1 },
{ index: "B", count: 2 },
{ index: "C", count: 3 }
];
const df = new Series(data, { index: ["one", "two", "three"] });
df.resetIndex({ inplace: true });
assert.deepEqual(df.index, [0, 1, 2]);
});
});
describe("setIndex", function () {
it("sets the index of an Series", function () {
const data = [
{ alpha: "A", count: 1 },
{ alpha: "B", count: 2 },
{ alpha: "C", count: 3 }
];
const df = new Series(data);
const dfNew = df.setIndex(["one", "two", "three"])
assert.deepEqual(dfNew.index, ["one", "two", "three"]);
assert.notDeepEqual(df.index, dfNew.index);
});
it("checks that the original series is not modified after setting new index not-inplace", function () {
const data = [
{ alpha: "A", count: 1 },
{ alpha: "B", count: 2 },
{ alpha: "C", count: 3 }
];
const df = new Series(data);
const dfNew = df.setIndex(["one", "two", "three"])
assert.notDeepEqual(df.index, dfNew.index);
});
it("sets the index of an Series inplace", function () {
const data = [12, 2, 20, 50];
const df = new Series(data);
df.setIndex(["one", "two", "three", "four"], { inplace: true });
assert.deepEqual(df.index, ["one", "two", "three", "four"]);
});
it("Throws index not found error", function () {
const data = [12, 2, 20, 50];
const df = new Series(data);
assert.throws(() => {
// @ts-ignore:
df.setIndex()
}, Error,
"Param Error: Must specify index array"
)
});
});
describe("Map", function () {
it("map series element to object keys", function () {
const sf = new Series([1, 2, 3, 4]);
const map = { 1: "ok", 2: "okie", 3: "frit", 4: "gop" };
const rslt = ["ok", "okie", "frit", "gop"];
assert.deepEqual((sf.map(map)).values, rslt);
});
it("map series element to object keys inplace", function () {
const sf = new Series([1, 2, 3, 4]);
const map = { 1: "ok", 2: "okie", 3: "frit", 4: "gop" };
const rslt = ["ok", "okie", "frit", "gop"];
sf.map(map, { inplace: true })
assert.deepEqual(sf.values, rslt);
});
it("map series element to incomplete object keys", function () {
const sf = new Series([1, 2, 3, 4]);
const map = { 1: "ok", 2: "okie", 4: "gop" };
const rslt = ["ok", "okie", 3, "gop"];
sf.map(map, { inplace: true })
assert.deepEqual(sf.values, rslt);
});
it("map series element to a function statement", function () {
const sf = new Series([1, 2, 3, 4]);
const func_map = (x: any) => {
return x + 1;
};
const rslt = [2, 3, 4, 5];
assert.deepEqual((sf.map(func_map)).values, rslt);
});
it("map series element to a function statement inplace", function () {
const sf = new Series([1, 2, 3, 4]);
const func_map = (x: any) => {
return x + 1;
};
const rslt = [2, 3, 4, 5];
sf.map(func_map, { inplace: true })
assert.deepEqual(sf.values, rslt);
});
it("map passes along the index", function () {
const sf = new Series([1, 2, 3, 4]);
const func_map = (x: any, i: any) => {
return x + i;
};
const rslt = [1, 3, 5, 7];
sf.map(func_map, { inplace: true });
assert.deepEqual(sf.values, rslt);
});
});
describe("Apply", function () {
it("apply a function to a series element", function () {
const sf = new Series([1, 2, 3, 4, 5, 6, 7, 8]);
const applyFunc = (x: any) => {
return x + x;
};
const rslt = [2, 4, 6, 8, 10, 12, 14, 16];
assert.deepEqual((sf.apply(applyFunc)).values, rslt);
});
it("apply a function to a series element inplace", function () {
const sf = new Series([1, 2, 3, 4, 5, 6, 7, 8]);
const applyFunc = (x: any) => {
return x + x;
};
const rslt = [2, 4, 6, 8, 10, 12, 14, 16];
sf.apply(applyFunc, { inplace: true })
assert.deepEqual(sf.values, rslt);
});
});
describe("unique", function () {
it("returns the unique values in a Series of type int", function () {
const sf = new Series([1, 2, 3, 4, 5, 6, 7, 8, 1, 1, 22, 8, 5, 5, 5]);
const expected = [1, 2, 3, 4, 5, 6, 7, 8, 22];
assert.deepEqual(sf.unique().values, expected);
});
it("returns the unique values in a Series of type string", function () {
const sf = new Series(["a", "a", "b", "c", "c", "d", "e", "d", "d", "e"]);
const expected = ["a", "b", "c", "d", "e"];
assert.deepEqual(sf.unique().values, expected);
});
it("returns the unique values in a Series of type string", function () {
const sf = new Series(["a", "a", "b", "c", "c", "d", "e", "d", "d", "e"]);
const expected = ["a", "b", "c", "d", "e"];
assert.deepEqual(sf.unique().values, expected);
});
});
describe("nUnique", function () {
it("returns the number of unique values in a Series of type string", function () {
const sf = new Series(["a", "a", "b", "c", "c", "d", "e", "d", "d", "e"]);
assert.deepEqual(sf.nUnique(), 5);
});
it("returns the number of unique values in a Series of type int32", function () {
const sf = new Series([1, 2, 3, 4, 3, 4, 3, 50, 4, 4, 4, 1]);
assert.deepEqual(sf.nUnique(), 5);
});
});
describe("valueCounts", function () {
it("returns the unique values and their counts in a Series of type int", function () {
const sf = new Series([1, 2, 3, 4, 5, 6, 7, 8, 1, 1, 22, 8, 5, 5, 5]);
const expectedIndex = [1, 2, 3, 4, 5, 6, 7, 8, 22];
const expectedVals = [3, 1, 1, 1, 4, 1, 1, 2, 1];
assert.deepEqual(sf.valueCounts().values, expectedVals);
assert.deepEqual(sf.valueCounts().index, expectedIndex);
});
it("returns the unique values and their counts in a Series of type string", function () {
const sf = new Series(["a", "a", "b", "c", "c", "d", "e", "d", "d", "e"]);
const expectedVals = [2, 1, 2, 3, 2];
const expectedIndex = ["a", "b", "c", "d", "e"];
assert.deepEqual(sf.valueCounts().values, expectedVals);
assert.deepEqual(sf.valueCounts().index, expectedIndex);
});
it("returns the unique values and their counts in a Series of type boolean", function () {
const sf = new Series([true, false, false, true, true]);
const expectedVals = [3, 2];
const expectedIndex = ["true", "false"];
assert.deepEqual(sf.valueCounts().values, expectedVals);
assert.deepEqual(sf.valueCounts().index, expectedIndex);
});
});
describe("abs", function () {
it("Returns the absolute values in Series", function () {
const data1 = [-10, 45, 56, -25, 23, -20, 10];
const sf = new Series(data1);
assert.deepEqual((sf.abs()).values, [10, 45, 56, 25, 23, 20, 10]);
});
it("Computes the descriptive statistics on a float Series", function () {
const data1 = [-30.1, -40.2, -3.1, -5.1];
const sf = new Series(data1);
assert.deepEqual((sf.abs()).values, [30.1, 40.2, 3.1, 5.1]);
});
});
describe("cumSum", function () {
it("Return cumulative sum over a Series", function () {
const data1 = [10, 45, 56, 25, 23, 20, 10];
const sf = new Series(data1);
assert.deepEqual((sf.cumSum()).values, [10, 55, 111, 136, 159, 179, 189]);
});
it("Return cumulative sum over a Series inplace", function () {
const data1 = [10, 45, 56, 25, 23, 20, 10];
const sf = new Series(data1);
sf.cumSum({ inplace: true });
assert.deepEqual(sf.values, [10, 55, 111, 136, 159, 179, 189]);
});
});
describe("cumMax", function () {
it("Return cumulative maximum over a Series", function () {
const data1 = [10, 45, 56, 25, 23, 20, 10];
const sf = new Series(data1);
assert.deepEqual((sf.cumMax()).values, [10, 45, 56, 56, 56, 56, 56]);
});