This repository was archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtest_series.py
7291 lines (5991 loc) · 293 KB
/
test_series.py
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.
# *****************************************************************************
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import platform
import pyarrow.parquet as pq
import sdc
import string
import unittest
from itertools import combinations, combinations_with_replacement, islice, permutations, product
import numba
from numba import types
from numba.core.config import IS_32BITS
from numba.core.errors import TypingError
from numba import literally
from sdc.tests.test_series_apply import TestSeries_apply
from sdc.tests.test_series_map import TestSeries_map
from sdc.tests.test_base import TestCase
from sdc.tests.test_utils import (count_array_OneDs,
count_array_REPs,
count_parfor_REPs,
get_start_end,
sdc_limitation,
skip_inline,
skip_numba_jit,
skip_parallel,
skip_sdc_jit,
create_series_from_values,
take_k_elements)
from sdc.tests.gen_test_data import ParquetGenerator
from sdc.tests.test_utils import test_global_input_data_unicode_kind1
from sdc.datatypes.common_functions import SDCLimitation
_cov_corr_series = [(pd.Series(x), pd.Series(y)) for x, y in [
(
[np.nan, -2., 3., 9.1],
[np.nan, -2., 3., 5.0],
),
# TODO(quasilyte): more intricate data for complex-typed series.
# Some arguments make assert_almost_equal fail.
# Functions that yield mismaching results:
# _column_corr_impl and _column_cov_impl.
(
[complex(-2., 1.0), complex(3.0, 1.0)],
[complex(-3., 1.0), complex(2.0, 1.0)],
),
(
[complex(-2.0, 1.0), complex(3.0, 1.0)],
[1.0, -2.0],
),
(
[1.0, -4.5],
[complex(-4.5, 1.0), complex(3.0, 1.0)],
),
]]
min_float64 = np.finfo('float64').min
max_float64 = np.finfo('float64').max
test_global_input_data_float64 = [
[11., 35.2, -24., 0., np.NZERO, np.NINF, np.PZERO, min_float64],
[1., np.nan, -1., 0., min_float64, max_float64, max_float64, min_float64],
[np.nan, np.inf, np.inf, np.nan, np.nan, np.nan, np.NINF, np.NZERO]
]
min_int64 = np.iinfo('int64').min
max_int64 = np.iinfo('int64').max
max_uint64 = np.iinfo('uint64').max
test_global_input_data_signed_integer64 = [
[1, -1, 0],
[min_int64, max_int64, max_int64, min_int64],
]
test_global_input_data_integer64 = test_global_input_data_signed_integer64 + [[max_uint64, max_uint64]]
test_global_input_data_numeric = test_global_input_data_integer64 + test_global_input_data_float64
test_global_input_data_unicode_kind4 = [
'ascii',
'12345',
'1234567890',
'¡Y tú quién te crees?',
'🐍⚡',
'大处着眼,小处着手。',
]
def gen_srand_array(size, nchars=8):
"""Generate array of strings of specified size based on [a-zA-Z] + [0-9]"""
accepted_chars = list(string.ascii_letters + string.digits)
rands_chars = np.array(accepted_chars, dtype=(np.str_, 1))
np.random.seed(100)
return np.random.choice(rands_chars, size=nchars * size).view((np.str_, nchars))
def gen_frand_array(size, min=-100, max=100, nancount=0):
"""Generate array of float of specified size based on [-100-100]"""
np.random.seed(100)
res = (max - min) * np.random.sample(size) + min
if nancount:
res[np.random.choice(np.arange(size), nancount)] = np.nan
return res
def gen_strlist(size, nchars=8, accepted_chars=None):
"""Generate list of strings of specified size based on accepted_chars"""
if not accepted_chars:
accepted_chars = string.ascii_letters + string.digits
generated_chars = islice(permutations(accepted_chars, nchars), size)
return [''.join(chars) for chars in generated_chars]
def series_values_from_argsort_result(series, argsorted):
"""
Rearranges series values according to pandas argsort result.
Used in tests to verify correct work of Series.argsort implementation for unstable sortings.
"""
argsort_indices = argsorted.values
result = np.empty_like(series.values)
# pandas argsort returns -1 in positions of NaN elements
nan_values_mask = argsort_indices == -1
if np.any(nan_values_mask):
result[nan_values_mask] = np.nan
# pandas argsort returns indexes in series values after all nans were dropped from it
# hence drop the NaN values, rearrange the rest with argsort result and assign them back to their positions
series_notna_values = series.dropna().values
result[~nan_values_mask] = series_notna_values.take(argsort_indices[~nan_values_mask])
return result
# Restores a series and checks the correct arrangement of indices,
# taking into account the same elements for unstable sortings
# Example: pd.Series([15, 3, 7, 3, 1],[2, 4, 6, 8, 10])
# Result can be pd.Series([1, 3, 3, 7, 15],[10, 4, 8, 6, 2]) or pd.Series([1, 3, 3, 7, 15],[10, 8, 4, 6, 2])
# if indices correct - return 0; wrong - return 1
def restore_series_sort_values(series, my_result_index, ascending):
value_dict = {}
nan_list = []
data = np.copy(series.data)
index = np.copy(series.index)
for value in range(len(data)):
# if np.isnan(data[value]):
if series.isna()[index[value]]:
nan_list.append(index[value])
if data[value] in value_dict:
value_dict[data[value]].append(index[value])
else:
value_dict[data[value]] = [index[value]]
na = series.isna().sum()
sort = np.argsort(data)
result = np.copy(my_result_index)
if not ascending:
sort[:len(result)-na] = sort[:len(result)-na][::-1]
for i in range(len(result)-na):
check = 0
for j in value_dict[data[sort[i]]]:
if j == result[i]:
check = 1
if check == 0:
return 1
for i in range(len(result)-na, len(result)):
check = 0
for j in nan_list:
if result[i] == j:
check = 1
if check == 0:
return 1
return 0
def _make_func_from_text(func_text, func_name='test_impl', global_vars={}):
loc_vars = {}
exec(func_text, global_vars, loc_vars)
test_impl = loc_vars[func_name]
return test_impl
def _make_func_use_binop1(operator):
func_text = "def test_impl(A, B):\n"
func_text += " return A {} B\n".format(operator)
return _make_func_from_text(func_text)
def _make_func_use_binop2(operator):
func_text = "def test_impl(A, B):\n"
func_text += " A {} B\n".format(operator)
func_text += " return A\n"
return _make_func_from_text(func_text)
def _make_func_use_method_arg1(method):
func_text = "def test_impl(A, B):\n"
func_text += " return A.{}(B)\n".format(method)
return _make_func_from_text(func_text)
def ljust_usecase(series, width):
return series.str.ljust(width)
def ljust_with_fillchar_usecase(series, width, fillchar):
return series.str.ljust(width, fillchar)
def rjust_usecase(series, width):
return series.str.rjust(width)
def rjust_with_fillchar_usecase(series, width, fillchar):
return series.str.rjust(width, fillchar)
def istitle_usecase(series):
return series.str.istitle()
def isspace_usecase(series):
return series.str.isspace()
def isalpha_usecase(series):
return series.str.isalpha()
def islower_usecase(series):
return series.str.islower()
def isalnum_usecase(series):
return series.str.isalnum()
def isnumeric_usecase(series):
return series.str.isnumeric()
def isdigit_usecase(series):
return series.str.isdigit()
def isdecimal_usecase(series):
return series.str.isdecimal()
def isupper_usecase(series):
return series.str.isupper()
def lower_usecase(series):
return series.str.lower()
def upper_usecase(series):
return series.str.upper()
def strip_usecase(series, to_strip=None):
return series.str.strip(to_strip)
def lstrip_usecase(series, to_strip=None):
return series.str.lstrip(to_strip)
def rstrip_usecase(series, to_strip=None):
return series.str.rstrip(to_strip)
def contains_usecase(series, pat, case=True, flags=0, na=None, regex=True):
return series.str.contains(pat, case, flags, na, regex)
class TestSeries(
TestSeries_apply,
TestSeries_map,
TestCase
):
@unittest.skip('Feature request: implement Series::ctor with list(list(type))')
def test_create_list_list_unicode(self):
def test_impl():
S = pd.Series([
['abc', 'defg', 'ijk'],
['lmn', 'opq', 'rstuvwxyz']
])
return S
hpat_func = self.jit(test_impl)
result_ref = test_impl()
result = hpat_func()
pd.testing.assert_series_equal(result, result_ref)
@unittest.skip('Feature request: implement Series::ctor with list(list(type))')
def test_create_list_list_integer(self):
def test_impl():
S = pd.Series([
[123, 456, -789],
[-112233, 445566, 778899]
])
return S
hpat_func = self.jit(test_impl)
result_ref = test_impl()
result = hpat_func()
pd.testing.assert_series_equal(result, result_ref)
@unittest.skip('Feature request: implement Series::ctor with list(list(type))')
def test_create_list_list_float(self):
def test_impl():
S = pd.Series([
[1.23, -4.56, 7.89],
[11.2233, 44.5566, -778.899]
])
return S
hpat_func = self.jit(test_impl)
result_ref = test_impl()
result = hpat_func()
pd.testing.assert_series_equal(result, result_ref)
def test_create_series1(self):
def test_impl():
A = pd.Series([1, 2, 3])
return A
hpat_func = self.jit(test_impl)
pd.testing.assert_series_equal(hpat_func(), test_impl())
def test_create_series_index1(self):
# create and box an indexed Series
def test_impl():
A = pd.Series([1, 2, 3], ['A', 'C', 'B'])
return A
hpat_func = self.jit(test_impl)
pd.testing.assert_series_equal(hpat_func(), test_impl())
def test_create_series_index2(self):
def test_impl():
A = pd.Series([1, 2, 3], index=[2, 1, 0])
return A
hpat_func = self.jit(test_impl)
pd.testing.assert_series_equal(hpat_func(), test_impl())
def test_create_series_index3(self):
def test_impl():
A = pd.Series([1, 2, 3], index=['A', 'C', 'B'], name='A')
return A
hpat_func = self.jit(test_impl)
pd.testing.assert_series_equal(hpat_func(), test_impl())
def test_create_series_index4(self):
def test_impl(name):
A = pd.Series([1, 2, 3], index=['A', 'C', 'B'], name=name)
return A
hpat_func = self.jit(test_impl)
pd.testing.assert_series_equal(hpat_func('A'), test_impl('A'))
@skip_numba_jit
def test_pass_series1(self):
# TODO: check to make sure it is series type
def test_impl(A):
return (A == 2).sum()
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n), name='A')
self.assertEqual(hpat_func(S), test_impl(S))
@skip_numba_jit
def test_pass_series_str(self):
def test_impl(A):
return (A == 'a').sum()
hpat_func = self.jit(test_impl)
S = pd.Series(['a', 'b', 'c'], name='A')
self.assertEqual(hpat_func(S), test_impl(S))
def test_pass_series_index1(self):
def test_impl(A):
return A
hpat_func = self.jit(test_impl)
S = pd.Series([3, 5, 6], ['a', 'b', 'c'], name='A')
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
def test_series_getattr_size(self):
def test_impl(S):
return S.size
hpat_func = self.jit(test_impl)
n = 11
for S, expected in [
(pd.Series(), 0),
(pd.Series([]), 0),
(pd.Series(np.arange(n)), n),
(pd.Series([np.nan, 1, 2]), 3),
(pd.Series(['1', '2', '3']), 3),
]:
with self.subTest(S=S, expected=expected):
self.assertEqual(hpat_func(S), expected)
self.assertEqual(hpat_func(S), test_impl(S))
def test_series_argsort1(self):
def test_impl(A):
return A.argsort()
hpat_func = self.jit(test_impl)
n = 11
np.random.seed(0)
A = pd.Series(np.random.ranf(n))
pd.testing.assert_series_equal(hpat_func(A), test_impl(A))
@skip_sdc_jit("Fails to compile with latest Numba")
def test_series_argsort2(self):
def test_impl(S):
return S.argsort()
hpat_func = self.jit(test_impl)
S = pd.Series([1, -1, 0, 2, np.nan], [1, 2, 3, 4, 5])
pd.testing.assert_series_equal(test_impl(S), hpat_func(S))
@skip_sdc_jit("Fails to compile with latest Numba")
def test_series_argsort_full(self):
def test_impl(series, kind):
return series.argsort(axis=0, kind=kind, order=None)
hpat_func = self.jit(test_impl)
all_data = test_global_input_data_numeric
for data in all_data:
S = pd.Series(data * 3)
for kind in ['quicksort', 'mergesort']:
result = test_impl(S, kind=kind)
result_ref = hpat_func(S, kind=kind)
if kind == 'mergesort':
pd.testing.assert_series_equal(result, result_ref)
else:
# for non-stable sorting check that values of restored series are equal
np.testing.assert_array_equal(
series_values_from_argsort_result(S, result),
series_values_from_argsort_result(S, result_ref)
)
@skip_sdc_jit("Fails to compile with latest Numba")
def test_series_argsort_full_idx(self):
def test_impl(series, kind):
return series.argsort(axis=0, kind=kind, order=None)
hpat_func = self.jit(test_impl)
all_data = test_global_input_data_numeric
for data in all_data:
data = data * 3
for index in [gen_srand_array(len(data)), gen_frand_array(len(data)), range(len(data))]:
S = pd.Series(data, index)
for kind in ['quicksort', 'mergesort']:
result = test_impl(S, kind=kind)
result_ref = hpat_func(S, kind=kind)
if kind == 'mergesort':
pd.testing.assert_series_equal(result, result_ref)
else:
# for non-stable sorting check that values of restored series are equal
np.testing.assert_array_equal(
series_values_from_argsort_result(S, result),
series_values_from_argsort_result(S, result_ref)
)
@skip_sdc_jit("Fails to compile with latest Numba")
def test_series_attr6(self):
def test_impl(A):
return A.take([2, 3]).values
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n)})
np.testing.assert_array_equal(hpat_func(df.A), test_impl(df.A))
def test_series_attr7(self):
def test_impl(A):
return A.astype(np.float64)
hpat_func = self.jit(test_impl)
n = 11
df = pd.DataFrame({'A': np.arange(n)})
np.testing.assert_array_equal(hpat_func(df.A), test_impl(df.A))
def test_series_getattr_ndim(self):
"""Verifies getting Series attribute ndim is supported"""
def test_impl(S):
return S.ndim
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n))
self.assertEqual(hpat_func(S), test_impl(S))
def test_series_getattr_T(self):
"""Verifies getting Series attribute T is supported"""
def test_impl(S):
return S.T
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n))
np.testing.assert_array_equal(hpat_func(S), test_impl(S))
def test_series_copy_str1(self):
def test_impl(A):
return A.copy()
hpat_func = self.jit(test_impl)
S = pd.Series(['aa', 'bb', 'cc'])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
def test_series_copy_int1(self):
def test_impl(A):
return A.copy()
hpat_func = self.jit(test_impl)
S = pd.Series([1, 2, 3])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
def test_series_copy_deep(self):
def test_impl(A, deep):
return A.copy(deep=deep)
hpat_func = self.jit(test_impl)
for S in [
pd.Series([1, 2]),
pd.Series([1, 2], index=["a", "b"]),
pd.Series([1, 2], name='A'),
pd.Series([1, 2], index=["a", "b"], name='A'),
]:
with self.subTest(S=S):
for deep in (True, False):
with self.subTest(deep=deep):
actual = hpat_func(S, deep)
expected = test_impl(S, deep)
pd.testing.assert_series_equal(actual, expected)
self.assertEqual(actual.values is S.values, expected.values is S.values)
self.assertEqual(actual.values is S.values, not deep)
# Shallow copy of index is not supported yet
if deep:
self.assertEqual(actual.index is S.index, expected.index is S.index)
self.assertEqual(actual.index is S.index, not deep)
@skip_sdc_jit('Series.corr() parameter "min_periods" unsupported')
def test_series_corr(self):
def test_series_corr_impl(s1, s2, min_periods=None):
return s1.corr(s2, min_periods=min_periods)
hpat_func = self.jit(test_series_corr_impl)
test_input_data1 = [[.2, .0, .6, .2],
[.2, .0, .6, .2, .5, .6, .7, .8],
[],
[2, 0, 6, 2],
[.2, .1, np.nan, .5, .3],
[-1, np.nan, 1, np.inf]]
test_input_data2 = [[.3, .6, .0, .1],
[.3, .6, .0, .1, .8],
[],
[3, 6, 0, 1],
[.3, .2, .9, .6, np.nan],
[np.nan, np.nan, np.inf, np.nan]]
for input_data1 in test_input_data1:
for input_data2 in test_input_data2:
s1 = pd.Series(input_data1)
s2 = pd.Series(input_data2)
for period in [None, 2, 1, 8, -4]:
result_ref = test_series_corr_impl(s1, s2, min_periods=period)
result = hpat_func(s1, s2, min_periods=period)
np.testing.assert_allclose(result, result_ref)
@skip_sdc_jit('Series.corr() parameter "min_periods" unsupported')
def test_series_corr_unsupported_dtype(self):
def test_series_corr_impl(s1, s2, min_periods=None):
return s1.corr(s2, min_periods=min_periods)
hpat_func = self.jit(test_series_corr_impl)
s1 = pd.Series([.2, .0, .6, .2])
s2 = pd.Series(['abcdefgh', 'a', 'abcdefg', 'ab', 'abcdef', 'abc'])
s3 = pd.Series(['aaaaa', 'bbbb', 'ccc', 'dd', 'e'])
s4 = pd.Series([.3, .6, .0, .1])
with self.assertRaises(TypingError) as raises:
hpat_func(s1, s2, min_periods=5)
msg = 'Method corr(). The object other.data'
self.assertIn(msg, str(raises.exception))
with self.assertRaises(TypingError) as raises:
hpat_func(s3, s4, min_periods=5)
msg = 'Method corr(). The object self.data'
self.assertIn(msg, str(raises.exception))
@skip_sdc_jit('Series.corr() parameter "min_periods" unsupported')
def test_series_corr_unsupported_period(self):
def test_series_corr_impl(s1, s2, min_periods=None):
return s1.corr(s2, min_periods=min_periods)
hpat_func = self.jit(test_series_corr_impl)
s1 = pd.Series([.2, .0, .6, .2])
s2 = pd.Series([.3, .6, .0, .1])
with self.assertRaises(TypingError) as raises:
hpat_func(s1, s2, min_periods='aaaa')
msg = 'Method corr(). The object min_periods'
self.assertIn(msg, str(raises.exception))
with self.assertRaises(TypingError) as raises:
hpat_func(s1, s2, min_periods=0.5)
msg = 'Method corr(). The object min_periods'
self.assertIn(msg, str(raises.exception))
@skip_parallel
@skip_inline
def test_series_astype_int_to_str1(self):
"""Verifies Series.astype implementation with function 'str' as argument
converts integer series to series of strings
"""
def test_impl(S):
return S.astype(str)
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n))
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@skip_parallel
@skip_inline
def test_series_astype_int_to_str2(self):
"""Verifies Series.astype implementation with a string literal dtype argument
converts integer series to series of strings
"""
def test_impl(S):
return S.astype('str')
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n))
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@skip_parallel
@skip_inline
def test_series_astype_str_to_str1(self):
"""Verifies Series.astype implementation with function 'str' as argument
handles string series not changing it
"""
def test_impl(S):
return S.astype(str)
hpat_func = self.jit(test_impl)
S = pd.Series(['aa', 'bb', 'cc'])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@skip_parallel
@skip_inline
def test_series_astype_str_to_str2(self):
"""Verifies Series.astype implementation with a string literal dtype argument
handles string series not changing it
"""
def test_impl(S):
return S.astype('str')
hpat_func = self.jit(test_impl)
S = pd.Series(['aa', 'bb', 'cc'])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@skip_parallel
@skip_inline
def test_series_astype_str_to_str_index_str(self):
"""Verifies Series.astype implementation with function 'str' as argument
handles string series not changing it
"""
def test_impl(S):
return S.astype(str)
hpat_func = self.jit(test_impl)
S = pd.Series(['aa', 'bb', 'cc'], index=['d', 'e', 'f'])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@skip_parallel
@skip_inline
def test_series_astype_str_to_str_index_int(self):
"""Verifies Series.astype implementation with function 'str' as argument
handles string series not changing it
"""
def test_impl(S):
return S.astype(str)
hpat_func = self.jit(test_impl)
S = pd.Series(['aa', 'bb', 'cc'], index=[1, 2, 3])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@unittest.skip('TODO: requires str(datetime64) support in Numba')
def test_series_astype_dt_to_str1(self):
"""Verifies Series.astype implementation with function 'str' as argument
converts datetime series to series of strings
"""
def test_impl(A):
return A.astype(str)
hpat_func = self.jit(test_impl)
S = pd.Series([pd.Timestamp('20130101 09:00:00'),
pd.Timestamp('20130101 09:00:02'),
pd.Timestamp('20130101 09:00:03')
])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@unittest.skip('AssertionError: Series are different'
'[left]: [0.000000, 1.000000, 2.000000, 3.000000, ...'
'[right]: [0.0, 1.0, 2.0, 3.0, ...'
'TODO: needs alignment to NumPy on Numba side')
def test_series_astype_float_to_str1(self):
"""Verifies Series.astype implementation with function 'str' as argument
converts float series to series of strings
"""
def test_impl(A):
return A.astype(str)
hpat_func = self.jit(test_impl)
n = 11.0
S = pd.Series(np.arange(n))
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
def test_series_astype_int32_to_int64(self):
"""Verifies Series.astype implementation with NumPy dtype argument
converts series with dtype=int32 to series with dtype=int64
"""
def test_impl(A):
return A.astype(np.int64)
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n), dtype=np.int32)
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
def test_series_astype_int_to_float64(self):
"""Verifies Series.astype implementation with NumPy dtype argument
converts named integer series to series of float
"""
def test_impl(A):
return A.astype(np.float64)
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n), name='A')
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
def test_series_astype_float_to_int32(self):
"""Verifies Series.astype implementation with NumPy dtype argument
converts float series to series of integers
"""
def test_impl(A):
return A.astype(np.int32)
hpat_func = self.jit(test_impl)
n = 11.0
S = pd.Series(np.arange(n))
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
def test_series_astype_literal_dtype1(self):
"""Verifies Series.astype implementation with a string literal dtype argument
converts float series to series of integers
"""
def test_impl(A):
return A.astype('int32')
hpat_func = self.jit(test_impl)
n = 11.0
S = pd.Series(np.arange(n))
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@unittest.skip('TODO: needs Numba astype impl support converting unicode_type to int')
def test_series_astype_str_to_int32(self):
"""Verifies Series.astype implementation with NumPy dtype argument
converts series of strings to series of integers
"""
import numba
def test_impl(A):
return A.astype(np.int32)
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series([str(x) for x in np.arange(n) - n // 2])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@unittest.skip('TODO: needs Numba astype impl support converting unicode_type to float')
def test_series_astype_str_to_float64(self):
"""Verifies Series.astype implementation with NumPy dtype argument
converts series of strings to series of float
"""
def test_impl(A):
return A.astype(np.float64)
hpat_func = self.jit(test_impl)
S = pd.Series(['3.24', '1E+05', '-1', '-1.3E-01', 'nan', 'inf'])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@skip_parallel
@skip_inline
def test_series_astype_str_index_str(self):
"""Verifies Series.astype implementation with function 'str' as argument
handles string series not changing it
"""
def test_impl(S):
return S.astype(str)
hpat_func = self.jit(test_impl)
S = pd.Series(['aa', 'bb', 'cc'], index=['a', 'b', 'c'])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@skip_parallel
@skip_inline
def test_series_astype_str_index_int(self):
"""Verifies Series.astype implementation with function 'str' as argument
handles string series not changing it
"""
def test_impl(S):
return S.astype(str)
hpat_func = self.jit(test_impl)
S = pd.Series(['aa', 'bb', 'cc'], index=[2, 3, 5])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
def test_series_astype_errors_ignore_return_self_str(self):
"""Verifies Series.astype implementation return self object on error
if errors='ignore' is passed in arguments
"""
def test_impl(S):
return S.astype(np.float64, errors='ignore')
hpat_func = self.jit(test_impl)
S = pd.Series(['aa', 'bb', 'cc'], index=[2, 3, 5])
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
@skip_numba_jit('TODO: implement np.call on Series in new-pipeline')
def test_np_call_on_series1(self):
def test_impl(A):
return np.min(A)
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n), name='A')
np.testing.assert_array_equal(hpat_func(S), test_impl(S))
def test_series_getattr_values(self):
def test_impl(A):
return A.values
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n), name='A')
np.testing.assert_array_equal(hpat_func(S), test_impl(S))
def test_series_values1(self):
def test_impl(A):
return (A == 2).values
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n), name='A')
np.testing.assert_array_equal(hpat_func(S), test_impl(S))
def test_series_getattr_shape1(self):
def test_impl(A):
return A.shape
hpat_func = self.jit(test_impl)
n = 11
S = pd.Series(np.arange(n), name='A')
self.assertEqual(hpat_func(S), test_impl(S))
def test_series_static_setitem(self):
def test_impl(A):
A[0] = 2
return (A == 2).sum()
hpat_func = self.jit(test_impl)
n = 11
S1 = pd.Series(np.arange(n), name='A')
S2 = S1.copy()
self.assertEqual(hpat_func(S1), test_impl(S2))
def test_series_setitem1(self):
def test_impl(A, i):
A[i] = 2
return (A == 2).sum()
hpat_func = self.jit(test_impl)
n, i = 11, 0
S1 = pd.Series(np.arange(n), name='A')
S2 = S1.copy()
self.assertEqual(hpat_func(S1, i), test_impl(S2, i))
def test_series_setitem2(self):
def test_impl(A, i):
A[i] = 100
hpat_func = self.jit(test_impl)
n = 11
S1 = pd.Series(np.arange(n), name='A')
S2 = S1.copy()
hpat_func(S1, 0)
test_impl(S2, 0)
pd.testing.assert_series_equal(S1, S2)
@skip_sdc_jit("enable after remove dead in hiframes is removed")
@skip_numba_jit("Assertion Error. Effects of set are not observed due to dead code elimination"
"TODO: investigate how to support this in Numba")
def test_series_setitem3(self):
def test_impl(A, i):
S = pd.Series(A)
S[i] = 100
hpat_func = self.jit(test_impl)
n = 11
A = np.arange(n)
A1 = A.copy()
A2 = A
hpat_func(A1, 0)
test_impl(A2, 0)
np.testing.assert_array_equal(A1, A2)
def test_series_setitem_with_filter1(self):
def test_impl(A):
A[A > 3] = 100
hpat_func = self.jit(test_impl)
n = 11
S1 = pd.Series(np.arange(n))
S2 = S1.copy()
hpat_func(S1)
test_impl(S2)
pd.testing.assert_series_equal(S1, S2)
@skip_sdc_jit('cannot assign slice from input of different size')
@skip_numba_jit("Series.setitem misses specialization for OptionalType")
def test_series_setitem_with_filter2(self):
def test_impl(A, B):
A[A > 3] = B[A > 3]
hpat_func = self.jit(test_impl)
n = 11
A1 = pd.Series(np.arange(n), name='A')
B = pd.Series(np.arange(n)**2, name='B')
A2 = A1.copy()
hpat_func(A1, B)
test_impl(A2, B)