Repository navigation
Expand file tree
/
Copy pathtest_dataframe.py
More file actions
2583 lines (2135 loc) · 92.4 KB
/
Copy pathtest_dataframe.py
File metadata and controls
2583 lines (2135 loc) · 92.4 KB
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, Intel Corporation All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************
import numba
import numpy as np
import pandas as pd
import platform
import random
import string
import unittest
from itertools import permutations, product
from numba import types
from numba.core.config import IS_32BITS
from numba import literal_unroll
from numba.core.errors import TypingError
from pandas.core.indexing import IndexingError
import sdc
from sdc.datatypes.common_functions import SDCLimitation
from sdc.tests.gen_test_data import ParquetGenerator
from sdc.tests.test_base import TestCase
from sdc.tests.test_utils import (check_numba_version,
count_array_OneDs,
count_array_REPs,
count_parfor_OneDs,
count_parfor_REPs,
dist_IR_contains,
gen_df, gen_df_int_cols,
get_start_end,
skip_numba_jit,
skip_sdc_jit,
test_global_input_data_float64,
test_global_input_data_unicode_kind4,
dfRefactoringNotImplemented)
@sdc.jit
def inner_get_column(df):
# df2 = df[['A', 'C']]
# df2['D'] = np.ones(3)
return df.A
COL_IND = 0
class TestDataFrame(TestCase):
# TODO: Data generator for DataFrames
@dfRefactoringNotImplemented
def test_create1(self):
def test_impl(A, B):
df = pd.DataFrame({'A': A, 'B': B})
return df
hpat_func = self.jit(test_impl)
n = 11
A = np.ones(n)
B = np.random.ranf(n)
pd.testing.assert_frame_equal(hpat_func(A, B), test_impl(A, B))
def test_create2(self):
def test_impl():
df = pd.DataFrame({'A': [1, 2, 3]})
return (df.A == 1).sum()
hpat_func = self.jit(test_impl)
self.assertEqual(hpat_func(), test_impl())
def test_create3(self):
def test_impl(n):
df = pd.DataFrame({'A': np.arange(n)})
return (df.A == 2).sum()
hpat_func = self.jit(test_impl)
n = 11
self.assertEqual(hpat_func(n), test_impl(n))
def test_create_str(self):
def test_impl():
df = pd.DataFrame({'A': ['a', 'b', 'c']})
return (df.A == 'a').sum()
hpat_func = self.jit(test_impl)
self.assertEqual(hpat_func(), test_impl())
@dfRefactoringNotImplemented
def test_create_with_series1(self):
def test_impl(n):
A = pd.Series(np.ones(n, dtype=np.int64))
B = pd.Series(np.zeros(n, dtype=np.float64))
df = pd.DataFrame({'A': A, 'B': B})
return df
hpat_func = sdc.jit(test_impl)
n = 11
pd.testing.assert_frame_equal(hpat_func(n), test_impl(n))
def test_create_with_series2(self):
# test creating dataframe from passed series
def test_impl(A):
df = pd.DataFrame({'A': A})
return (df.A == 2).sum()
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n)})
self.assertEqual(hpat_func(df.A), test_impl(df.A))
@skip_sdc_jit
@dfRefactoringNotImplemented
def test_create_string_index(self):
def test_impl(a):
data = {'A': ['a', 'b'], 'B': [2, 3]}
df = pd.DataFrame(data=data, index=['A', 'B'])
return df
hpat_func = sdc.jit(test_impl)
pd.testing.assert_frame_equal(hpat_func(True), test_impl(True))
@dfRefactoringNotImplemented
def test_create_cond1(self):
def test_impl(A, B, c):
if c:
df = pd.DataFrame({'A': A})
else:
df = pd.DataFrame({'A': B})
return df
hpat_func = self.jit(test_impl)
n = 11
A = np.ones(n)
B = np.arange(n) + 1.0
c = 0
pd.testing.assert_frame_equal(hpat_func(A, B, c), test_impl(A, B, c))
c = 2
pd.testing.assert_frame_equal(hpat_func(A, B, c), test_impl(A, B, c))
@dfRefactoringNotImplemented
def test_create_without_column_names(self):
def test_impl():
df = pd.DataFrame([100, 200, 300, 400, 200, 100])
return df
hpat_func = self.jit(test_impl)
pd.testing.assert_frame_equal(hpat_func(), test_impl())
@dfRefactoringNotImplemented
def test_pass_df1(self):
def test_impl(df):
return (df.A == 2).sum()
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n)})
self.assertEqual(hpat_func(df), test_impl(df))
@dfRefactoringNotImplemented
def test_pass_df_str(self):
def test_impl(df):
return (df.A == 'a').sum()
hpat_func = self.jit(test_impl)
df = pd.DataFrame({'A': ['a', 'b', 'c']})
self.assertEqual(hpat_func(df), test_impl(df))
@dfRefactoringNotImplemented
def test_unbox1(self):
def test_impl(df):
return df
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n), 'B': np.random.ranf(n)})
pd.testing.assert_frame_equal(hpat_func(df), test_impl(df))
@unittest.skip("needs properly refcounted dataframes")
def test_unbox2(self):
def test_impl(df, cond):
n = len(df)
if cond:
df['A'] = np.arange(n) + 2.0
return df.A
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.ones(n), 'B': np.random.ranf(n)})
pd.testing.assert_series_equal(hpat_func(df.copy(), True), test_impl(df.copy(), True))
pd.testing.assert_series_equal(hpat_func(df.copy(), False), test_impl(df.copy(), False))
@unittest.skip('Implement feature to create DataFrame without column names')
def test_unbox_without_column_names(self):
def test_impl(df):
return df
df = pd.DataFrame([100, 200, 300, 400, 200, 100])
hpat_func = self.jit(test_impl)
pd.testing.assert_frame_equal(hpat_func(df), test_impl(df))
@skip_numba_jit
def test_box1(self):
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n), 'B': np.arange(n)})
return df
hpat_func = self.jit(test_impl)
n = 11
do_check = False if platform.system() == 'Windows' and not IS_32BITS else True
pd.testing.assert_frame_equal(hpat_func(n), test_impl(n), check_dtype=do_check)
@dfRefactoringNotImplemented
def test_box2(self):
def test_impl():
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'bb', 'ccc']})
return df
hpat_func = self.jit(test_impl)
pd.testing.assert_frame_equal(hpat_func(), test_impl())
@skip_sdc_jit("pending df filter support")
@dfRefactoringNotImplemented
def test_box3(self):
def test_impl(df):
df = df[df.A != 'dd']
return df
hpat_func = self.jit(test_impl)
df = pd.DataFrame({'A': ['aa', 'bb', 'cc']})
pd.testing.assert_frame_equal(hpat_func(df), test_impl(df))
@skip_numba_jit
def test_box_categorical(self):
def test_impl(df):
df['A'] = df['A'] + 1
return df
hpat_func = self.jit(test_impl)
df = pd.DataFrame({'A': [1, 2, 3],
'B': pd.Series(['N', 'Y', 'Y'],
dtype=pd.api.types.CategoricalDtype(['N', 'Y']))})
pd.testing.assert_frame_equal(hpat_func(df.copy(deep=True)), test_impl(df))
@unittest.expectedFailure # https://github.com/numba/numba/issues/4690
def test_box_dist_return(self):
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n), 'B': np.arange(n)})
return df
hpat_func = self.jit(distributed={'df'})(test_impl)
n = 11
hres, res = hpat_func(n), test_impl(n)
self.assertEqual(count_array_OneDs(), 3)
self.assertEqual(count_parfor_OneDs(), 2)
dist_sum = self.jit(
lambda a: sdc.distributed_api.dist_reduce(
a, np.int32(sdc.distributed_api.Reduce_Type.Sum.value)))
dist_sum(1) # run to compile
np.testing.assert_allclose(dist_sum(hres.A.sum()), res.A.sum())
np.testing.assert_allclose(dist_sum(hres.B.sum()), res.B.sum())
@skip_numba_jit
def test_len1(self):
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n, np.int64), 'B': np.random.ranf(n)})
return len(df)
hpat_func = self.jit(test_impl)
n = 11
self.assertEqual(hpat_func(n), test_impl(n))
self.assertEqual(count_array_REPs(), 0)
self.assertEqual(count_parfor_REPs(), 0)
@skip_numba_jit
def test_shape1(self):
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n, np.int64), 'B': np.random.ranf(n)})
return df.shape
hpat_func = self.jit(test_impl)
n = 11
self.assertEqual(hpat_func(n), test_impl(n))
self.assertEqual(count_array_REPs(), 0)
self.assertEqual(count_parfor_REPs(), 0)
@skip_numba_jit
def test_column_getitem1(self):
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n), 'B': np.random.ranf(n)})
Ac = df['A'].values
return Ac.sum()
hpat_func = self.jit(test_impl)
n = 11
self.assertEqual(hpat_func(n), test_impl(n))
self.assertEqual(count_array_REPs(), 0)
self.assertEqual(count_parfor_REPs(), 0)
self.assertEqual(count_parfor_OneDs(), 1)
@skip_numba_jit
def test_column_list_getitem1(self):
def test_impl(df):
return df[['A', 'C']]
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame(
{'A': np.arange(n), 'B': np.ones(n), 'C': np.random.ranf(n)})
pd.testing.assert_frame_equal(hpat_func(df), test_impl(df))
def test_filter1(self):
def test_impl(n):
df = pd.DataFrame({'A': np.arange(n) + n, 'B': np.arange(n)**2})
df1 = df[df.A > .5]
return df1.B.sum()
hpat_func = self.jit(test_impl)
n = 11
self.assertEqual(hpat_func(n), test_impl(n))
self.assertEqual(count_array_REPs(), 0)
self.assertEqual(count_parfor_REPs(), 0)
@skip_numba_jit('np.sum of Series unsupported')
def test_filter2(self):
def test_impl(n):
df = pd.DataFrame({'A': np.arange(n) + n, 'B': np.arange(n)**2})
df1 = df.loc[df.A > .5]
return np.sum(df1.B)
hpat_func = self.jit(test_impl)
n = 11
self.assertEqual(hpat_func(n), test_impl(n))
self.assertEqual(count_array_REPs(), 0)
self.assertEqual(count_parfor_REPs(), 0)
@skip_numba_jit('np.sum of Series unsupported')
def test_filter3(self):
def test_impl(n):
df = pd.DataFrame({'A': np.arange(n) + n, 'B': np.arange(n)**2})
df1 = df.iloc[(df.A > .5).values]
return np.sum(df1.B)
hpat_func = self.jit(test_impl)
n = 11
self.assertEqual(hpat_func(n), test_impl(n))
self.assertEqual(count_array_REPs(), 0)
self.assertEqual(count_parfor_REPs(), 0)
@skip_numba_jit
def test_iloc1(self):
def test_impl(df, n):
return df.iloc[1:n].B.values
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n), 'B': np.arange(n)**2})
np.testing.assert_array_equal(hpat_func(df, n), test_impl(df, n))
@skip_numba_jit
def test_iloc2(self):
def test_impl(df, n):
return df.iloc[np.array([1, 4, 9])].B.values
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n), 'B': np.arange(n)**2})
np.testing.assert_array_equal(hpat_func(df, n), test_impl(df, n))
@skip_numba_jit
def test_iloc3(self):
def test_impl(df):
return df.iloc[:, 1].values
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n), 'B': np.arange(n)**2})
np.testing.assert_array_equal(hpat_func(df), test_impl(df))
@unittest.skip("TODO: support A[[1,2,3]] in Numba")
def test_iloc4(self):
def test_impl(df, n):
return df.iloc[[1, 4, 9]].B.values
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n), 'B': np.arange(n)**2})
np.testing.assert_array_equal(hpat_func(df, n), test_impl(df, n))
@skip_numba_jit
def test_iloc5(self):
# test iloc with global value
def test_impl(df):
return df.iloc[:, COL_IND].values
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n), 'B': np.arange(n)**2})
np.testing.assert_array_equal(hpat_func(df), test_impl(df))
@skip_numba_jit
def test_loc1(self):
def test_impl(df):
return df.loc[:, 'B'].values
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n), 'B': np.arange(n)**2})
np.testing.assert_array_equal(hpat_func(df), test_impl(df))
@skip_numba_jit
def test_iat1(self):
def test_impl(n):
df = pd.DataFrame({'B': np.ones(n), 'A': np.arange(n) + n})
return df.iat[3, 1]
hpat_func = self.jit(test_impl)
n = 11
self.assertEqual(hpat_func(n), test_impl(n))
@skip_numba_jit
def test_iat2(self):
def test_impl(df):
return df.iat[3, 1]
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'B': np.ones(n), 'A': np.arange(n) + n})
self.assertEqual(hpat_func(df), test_impl(df))
@skip_numba_jit
def test_iat3(self):
def test_impl(df, n):
return df.iat[n - 1, 1]
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'B': np.ones(n), 'A': np.arange(n) + n})
self.assertEqual(hpat_func(df, n), test_impl(df, n))
@skip_numba_jit
def test_iat_set1(self):
def test_impl(df, n):
df.iat[n - 1, 1] = n**2
return df.A # return the column to check column aliasing
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'B': np.ones(n), 'A': np.arange(n) + n})
df2 = df.copy()
pd.testing.assert_series_equal(hpat_func(df, n), test_impl(df2, n))
@skip_numba_jit
def test_iat_set2(self):
def test_impl(df, n):
df.iat[n - 1, 1] = n**2
return df # check df aliasing/boxing
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'B': np.ones(n), 'A': np.arange(n) + n})
df2 = df.copy()
pd.testing.assert_frame_equal(hpat_func(df, n), test_impl(df2, n))
@skip_numba_jit
def test_set_column1(self):
# set existing column
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n, np.int64), 'B': np.arange(n) + 3.0})
df['A'] = np.arange(n)
return df
hpat_func = self.jit(test_impl)
n = 11
do_check = False if platform.system() == 'Windows' and not IS_32BITS else True
pd.testing.assert_frame_equal(hpat_func(n), test_impl(n), check_dtype=do_check)
@skip_numba_jit
def test_set_column_reflect4(self):
# set existing column
def test_impl(df, n):
df['A'] = np.arange(n)
hpat_func = self.jit(test_impl)
n = 11
df1 = pd.DataFrame({'A': np.ones(n, np.int64), 'B': np.arange(n) + 3.0})
df2 = df1.copy()
hpat_func(df1, n)
test_impl(df2, n)
do_check = False if platform.system() == 'Windows' and not IS_32BITS else True
pd.testing.assert_frame_equal(df1, df2, check_dtype=do_check)
@skip_numba_jit
def test_set_column_new_type1(self):
# set existing column with a new type
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n), 'B': np.arange(n) + 3.0})
df['A'] = np.arange(n)
return df
hpat_func = self.jit(test_impl)
n = 11
do_check = False if platform.system() == 'Windows' and not IS_32BITS else True
pd.testing.assert_frame_equal(hpat_func(n), test_impl(n), check_dtype=do_check)
@skip_numba_jit
def test_set_column2(self):
# create new column
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n), 'B': np.arange(n) + 1.0})
df['C'] = np.arange(n)
return df
hpat_func = self.jit(test_impl)
n = 11
do_check = False if platform.system() == 'Windows' and not IS_32BITS else True
pd.testing.assert_frame_equal(hpat_func(n), test_impl(n), check_dtype=do_check)
@skip_numba_jit
def test_set_column_reflect3(self):
# create new column
def test_impl(df, n):
df['C'] = np.arange(n)
hpat_func = self.jit(test_impl)
n = 11
df1 = pd.DataFrame({'A': np.ones(n, np.int64), 'B': np.arange(n) + 3.0})
df2 = df1.copy()
hpat_func(df1, n)
test_impl(df2, n)
do_check = False if platform.system() == 'Windows' and not IS_32BITS else True
pd.testing.assert_frame_equal(df1, df2, check_dtype=do_check)
@skip_numba_jit
def test_set_column_bool1(self):
def test_impl(df):
df['C'] = df['A'][df['B']]
hpat_func = self.jit(test_impl)
df = pd.DataFrame({'A': [1, 2, 3], 'B': [True, False, True]})
df2 = df.copy()
test_impl(df2)
hpat_func(df)
pd.testing.assert_series_equal(df.C, df2.C)
@skip_numba_jit
def test_set_column_reflect1(self):
def test_impl(df, arr):
df['C'] = arr
return df.C.sum()
hpat_func = self.jit(test_impl)
n = 11
arr = np.random.ranf(n)
df = pd.DataFrame({'A': np.ones(n), 'B': np.random.ranf(n)})
hpat_func(df, arr)
self.assertIn('C', df)
np.testing.assert_almost_equal(df.C.values, arr)
@skip_numba_jit
def test_set_column_reflect2(self):
def test_impl(df, arr):
df['C'] = arr
return df.C.sum()
hpat_func = self.jit(test_impl)
n = 11
arr = np.random.ranf(n)
df = pd.DataFrame({'A': np.ones(n), 'B': np.random.ranf(n)})
df2 = df.copy()
np.testing.assert_almost_equal(hpat_func(df, arr), test_impl(df2, arr))
def _test_df_set_column(self, all_data, key, value):
def gen_test_impl(value, do_jit=False):
if isinstance(value, pd.Series):
def test_impl(df, key, value):
if do_jit == True: # noqa
return df._set_column(key, value.values)
else:
df[key] = value.values
else:
def test_impl(df, key, value):
if do_jit == True: # noqa
return df._set_column(key, value)
else:
df[key] = value
return test_impl
test_impl = gen_test_impl(value)
sdc_func = self.jit(gen_test_impl(value, do_jit=True))
for data in all_data:
with self.subTest(data=data):
df1 = pd.DataFrame(data)
df2 = df1.copy(deep=True)
test_impl(df1, key, value)
result_ref = df1 # in pandas setitem modifies original DF
result_jit = sdc_func(df2, key, value)
pd.testing.assert_frame_equal(result_jit, result_ref)
def _test_df_set_column_exception_invalid_length(self, df, key, value):
def test_impl(df, key, value):
return df._set_column(key, value)
sdc_func = self.jit(test_impl)
with self.assertRaises(ValueError) as raises:
sdc_func(df, key, value)
msg = 'Length of values does not match length of index'
self.assertIn(msg, str(raises.exception))
def _test_df_set_column_exception_empty_columns(self, df, key, value):
def test_impl(df, key, value):
return df._set_column(key, value)
sdc_func = self.jit(test_impl)
with self.assertRaises(SDCLimitation) as raises:
sdc_func(df, key, value)
msg = 'Could not set item for DataFrame with empty columns'
self.assertIn(msg, str(raises.exception))
@dfRefactoringNotImplemented
def test_df_add_column(self):
all_data = [{'A': [0, 1, 2], 'C': [0., np.nan, np.inf]}, {}]
key, value = 'B', np.array([1., -1., 0.])
self._test_df_set_column(all_data, key, value)
@dfRefactoringNotImplemented
def test_df_add_column_str(self):
all_data = [{'A': [0, 1, 2], 'C': [0., np.nan, np.inf]}, {}]
key, value = 'B', pd.Series(test_global_input_data_unicode_kind4)
self._test_df_set_column(all_data, key, value)
@dfRefactoringNotImplemented
def test_df_add_column_exception_invalid_length(self):
df = pd.DataFrame({'A': [0, 1, 2], 'C': [3., 4., 5.]})
key, value = 'B', np.array([1., np.nan, -1., 0.])
self._test_df_set_column_exception_invalid_length(df, key, value)
df = pd.DataFrame({'A': []})
self._test_df_set_column_exception_empty_columns(df, key, value)
@dfRefactoringNotImplemented
def test_df_replace_column(self):
all_data = [{'A': [0, 1, 2], 'C': [0., np.nan, np.inf]}]
key, value = 'A', np.array([1., -1., 0.])
self._test_df_set_column(all_data, key, value)
@dfRefactoringNotImplemented
def test_df_replace_column_str(self):
all_data = [{'A': [0, 1, 2], 'C': [0., np.nan, np.inf]}]
key, value = 'A', pd.Series(test_global_input_data_unicode_kind4)
self._test_df_set_column(all_data, key, value)
@dfRefactoringNotImplemented
def test_df_replace_column_exception_invalid_length(self):
df = pd.DataFrame({'A': [0, 1, 2], 'C': [3., 4., 5.]})
key, value = 'A', np.array([1., np.nan, -1., 0.])
self._test_df_set_column_exception_invalid_length(df, key, value)
df = pd.DataFrame({'A': []})
self._test_df_set_column_exception_empty_columns(df, key, value)
def _test_df_values_unboxing(self, df):
def test_impl(df):
return df.values
sdc_func = self.jit(test_impl)
np.testing.assert_array_equal(sdc_func(df), test_impl(df))
@dfRefactoringNotImplemented
def test_df_values_unboxing(self):
values_to_test = [[1, 2, 3, 4, 5],
[.1, .2, .3, .4, .5],
[np.nan, np.inf, .0, .1, -1.]]
n = 5
np.random.seed(0)
A = np.ones(n)
B = np.random.ranf(n)
for values in values_to_test:
with self.subTest(values=values):
df = pd.DataFrame({'A': A, 'B': B, 'C D E': values})
self._test_df_values_unboxing(df)
def test_df_values(self):
def test_impl(n, values):
df = pd.DataFrame({'A': np.ones(n), 'B': np.arange(n), 'C': values})
return df.values
sdc_func = self.jit(test_impl)
n = 5
values_to_test = [[1, 2, 3, 4, 5],
[.1, .2, .3, .4, .5],
[np.nan, np.inf, .0, .1, -1.]]
for values in values_to_test:
with self.subTest(values=values):
np.testing.assert_array_equal(sdc_func(n, values), test_impl(n, values))
@skip_numba_jit
def test_df_values_parallel1(self):
def test_impl(n):
df = pd.DataFrame({'A': np.ones(n), 'B': np.arange(n)})
return df.values.sum()
hpat_func = self.jit(test_impl)
n = 11
np.testing.assert_array_equal(hpat_func(n), test_impl(n))
self.assertEqual(count_array_REPs(), 0)
self.assertEqual(count_parfor_REPs(), 0)
def _test_df_index(self, df):
def test_impl(df):
return df.index
sdc_func = self.jit(test_impl)
np.testing.assert_array_equal(sdc_func(df), test_impl(df))
@skip_sdc_jit
@dfRefactoringNotImplemented # required re-implementing DataFrame unboxing
def test_index_attribute(self):
index_to_test = [[1, 2, 3, 4, 5],
[.1, .2, .3, .4, .5],
['a', 'b', 'c', 'd', 'e']]
n = 5
np.random.seed(0)
A = np.ones(n)
B = np.random.ranf(n)
for index in index_to_test:
with self.subTest(index=index):
df = pd.DataFrame({'A': A, 'B': B}, index=index)
self._test_df_index(df)
@skip_sdc_jit
@dfRefactoringNotImplemented # required re-implementing DataFrame unboxing
def test_index_attribute_empty(self):
n = 5
np.random.seed(0)
A = np.ones(n)
B = np.random.ranf(n)
df = pd.DataFrame({'A': A, 'B': B})
self._test_df_index(df)
@skip_sdc_jit
@dfRefactoringNotImplemented # required re-implementing DataFrame unboxing
def test_index_attribute_empty_df(self):
df = pd.DataFrame()
self._test_df_index(df)
def test_index_attribute_no_unboxing(self):
def test_impl(n, index):
np.random.seed(0)
df = pd.DataFrame({
'A': np.ones(n),
'B': np.random.ranf(n)
}, index=index)
return df.index
sdc_impl = self.jit(test_impl)
index_to_test = [
[1, 2, 3, 4, 5],
[.1, .2, .3, .4, .5],
['a', 'b', 'c', 'd', 'e']
]
for index in index_to_test:
with self.subTest(index=index):
n = len(index)
jit_result = sdc_impl(n, index)
ref_result = test_impl(n, index)
np.testing.assert_array_equal(jit_result, ref_result)
def test_index_attribute_default_no_unboxing(self):
def test_impl(n):
np.random.seed(0)
df = pd.DataFrame({
'A': np.ones(n),
'B': np.random.ranf(n)
})
return df.index
sdc_impl = self.jit(test_impl)
np.testing.assert_array_equal(sdc_impl(10), test_impl(10))
@skip_sdc_jit
@skip_numba_jit
def test_df_apply(self):
def test_impl(n):
df = pd.DataFrame({'A': np.arange(n), 'B': np.arange(n)})
B = df.apply(lambda r: r.A + r.B, axis=1)
return df.B.sum()
n = 121
hpat_func = self.jit(test_impl)
np.testing.assert_almost_equal(hpat_func(n), test_impl(n))
@skip_sdc_jit
@skip_numba_jit
def test_df_apply_branch(self):
def test_impl(n):
df = pd.DataFrame({'A': np.arange(n), 'B': np.arange(n)})
B = df.apply(lambda r: r.A < 10 and r.B > 20, axis=1)
return df.B.sum()
n = 121
hpat_func = self.jit(test_impl)
np.testing.assert_almost_equal(hpat_func(n), test_impl(n))
@skip_numba_jit
@skip_sdc_jit('Not implemented in sequential transport layer')
def test_df_describe(self):
def test_impl(n):
df = pd.DataFrame({'A': np.arange(0, n, 1, np.float32),
'B': np.arange(n)})
#df.A[0:1] = np.nan
return df.describe()
hpat_func = self.jit(test_impl)
n = 1001
hpat_func(n)
# XXX: test actual output
self.assertEqual(count_array_REPs(), 0)
self.assertEqual(count_parfor_REPs(), 0)
@skip_numba_jit
def test_sort_values(self):
def test_impl(df):
df.sort_values('A', inplace=True)
return df.B.values
n = 1211
np.random.seed(2)
df = pd.DataFrame({'A': np.random.ranf(n), 'B': np.arange(n), 'C': np.random.ranf(n)})
hpat_func = self.jit(test_impl)
np.testing.assert_almost_equal(hpat_func(df.copy()), test_impl(df))
@skip_numba_jit
def test_sort_values_copy(self):
def test_impl(df):
df2 = df.sort_values('A')
return df2.B.values
n = 1211
np.random.seed(2)
df = pd.DataFrame({'A': np.random.ranf(n), 'B': np.arange(n), 'C': np.random.ranf(n)})
hpat_func = self.jit(test_impl)
np.testing.assert_almost_equal(hpat_func(df.copy()), test_impl(df))
@skip_numba_jit
def test_sort_values_single_col(self):
def test_impl(df):
df.sort_values('A', inplace=True)
return df.A.values
n = 1211
np.random.seed(2)
df = pd.DataFrame({'A': np.random.ranf(n)})
hpat_func = self.jit(test_impl)
np.testing.assert_almost_equal(hpat_func(df.copy()), test_impl(df))
@skip_numba_jit
def test_sort_values_single_col_str(self):
def test_impl(df):
df.sort_values('A', inplace=True)
return df.A.values
n = 1211
random.seed(2)
str_vals = []
for _ in range(n):
k = random.randint(1, 30)
val = ''.join(random.choices(string.ascii_uppercase + string.digits, k=k))
str_vals.append(val)
df = pd.DataFrame({'A': str_vals})
hpat_func = self.jit(test_impl)
self.assertTrue((hpat_func(df.copy()) == test_impl(df)).all())
@skip_numba_jit
def test_sort_values_str(self):
def test_impl(df):
df.sort_values('A', inplace=True)
return df.B.values
n = 1211
random.seed(2)
str_vals = []
str_vals2 = []
for i in range(n):
k = random.randint(1, 30)
val = ''.join(random.choices(string.ascii_uppercase + string.digits, k=k))
str_vals.append(val)
val = ''.join(random.choices(string.ascii_uppercase + string.digits, k=k))
str_vals2.append(val)
df = pd.DataFrame({'A': str_vals, 'B': str_vals2})
# use mergesort for stability, in str generation equal keys are more probable
sorted_df = df.sort_values('A', inplace=False, kind='mergesort')
hpat_func = self.jit(test_impl)
self.assertTrue((hpat_func(df) == sorted_df.B.values).all())
@skip_numba_jit
def test_sort_parallel_single_col(self):
# create `kde.parquet` file
ParquetGenerator.gen_kde_pq()
# TODO: better parallel sort test
def test_impl():
df = pd.read_parquet('kde.parquet')
df.sort_values('points', inplace=True)
res = df.points.values
return res
hpat_func = self.jit(locals={'res:return': 'distributed'})(test_impl)
save_min_samples = sdc.hiframes.sort.MIN_SAMPLES
try:
sdc.hiframes.sort.MIN_SAMPLES = 10
res = hpat_func()
self.assertTrue((np.diff(res) >= 0).all())
finally:
# restore global val
sdc.hiframes.sort.MIN_SAMPLES = save_min_samples
@skip_numba_jit
def test_df_isna1(self):
'''Verify DataFrame.isna implementation for various types of data'''
def test_impl(df):
return df.isna()
hpat_func = self.jit(test_impl)
# TODO: add column with datetime values when test_series_datetime_isna1 is fixed
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0],
'B': [np.inf, 5, np.nan, 6],
'C': ['aa', 'b', None, 'ccc'],
'D': [None, 'dd', '', None]})
pd.testing.assert_frame_equal(hpat_func(df), test_impl(df))
@dfRefactoringNotImplemented
def test_df_isna(self):
def test_impl(df):
return df.isna()
sdc_func = sdc.jit(test_impl)
indexes = [[3, 4, 2, 6, 1], ['a', 'b', 'c', 'd', 'e'], None]
for idx in indexes:
df = pd.DataFrame({"A": [3.2, np.nan, 7.0, 3.3, np.nan],
"B": [3, 4, 1, 0, 222],
"C": [True, True, False, False, True],
"D": ['a', 'dd', 'c', '12', None]}, index=idx)
with self.subTest(index=idx):
pd.testing.assert_frame_equal(sdc_func(df), test_impl(df))
@dfRefactoringNotImplemented
def test_df_bool(self):
def test_impl(df):
return df.isna()
sdc_func = sdc.jit(test_impl)
df = pd.DataFrame({"bool": [True, True, False, False, True]}, index=None)
pd.testing.assert_frame_equal(sdc_func(df), test_impl(df))
@skip_numba_jit
def test_df_astype_str1(self):
'''Verifies DataFrame.astype implementation converting various types to string'''
def test_impl(df):
return df.astype(str)
hpat_func = self.jit(test_impl)
# TODO: add column with float values when test_series_astype_float_to_str1 is fixed
df = pd.DataFrame({'A': [-1, 2, 11, 5, 0, -7],
'B': ['aa', 'bb', 'cc', 'dd', '', 'fff']
})