This repository was archived by the owner on Feb 2, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpd_series_ext.py
More file actions
1285 lines (1021 loc) · 44.3 KB
/
Copy pathpd_series_ext.py
File metadata and controls
1285 lines (1021 loc) · 44.3 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) 2019, 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 datetime
import operator
import numpy as np
import pandas as pd
import llvmlite.llvmpy.core as lc
import numba
from numba import types, cgutils
from numba.extending import (
models,
register_model,
lower_cast,
lower_builtin,
infer_getattr,
type_callable,
infer,
overload,
make_attribute_wrapper)
from numba.typing.arraydecl import (get_array_index_type, _expand_integer, ArrayAttribute, SetItemBuffer)
from numba.typing.npydecl import (
Numpy_rules_ufunc,
NumpyRulesArrayOperator,
NumpyRulesInplaceArrayOperator,
NumpyRulesUnaryArrayOperator,
NdConstructorLike)
from numba.typing.templates import (infer_global, AbstractTemplate, signature, AttributeTemplate, bound_function)
from numba.targets.imputils import (impl_ret_new_ref, iternext_impl, RefType)
from numba.targets.arrayobj import (make_array, _getitem_array1d)
import hpat
from hpat.hiframes.pd_categorical_ext import (PDCategoricalDtype, CategoricalArray)
from hpat.hiframes.pd_timestamp_ext import (pandas_timestamp_type, datetime_date_type)
from hpat.hiframes.rolling import supported_rolling_funcs
from hpat.hiframes.split_impl import (string_array_split_view_type, GetItemStringArraySplitView)
from hpat.str_arr_ext import (
string_array_type,
iternext_str_array,
offset_typ,
char_typ,
str_arr_payload_type,
StringArrayType,
GetItemStringArray)
from hpat.str_ext import string_type, list_string_array_type
class SeriesType(types.IterableType):
"""Temporary type class for Series objects.
"""
def __init__(self, dtype, data=None, index=None, is_named=False):
# keeping data array in type since operators can make changes such
# as making array unaligned etc.
data = _get_series_array_type(dtype) if data is None else data
# convert Record to tuple (for tuple output of map)
# TODO: handle actual Record objects in Series?
self.dtype = (types.Tuple(list(dict(dtype.members).values()))
if isinstance(dtype, types.Record) else dtype)
self.data = data
if index is None:
index = types.none
self.index = index
# keep is_named in type to enable boxing
self.is_named = is_named
super(SeriesType, self).__init__(
name="series({}, {}, {}, {})".format(dtype, data, index, is_named))
def copy(self, dtype=None):
# XXX is copy necessary?
index = types.none if self.index == types.none else self.index.copy()
dtype = dtype if dtype is not None else self.dtype
data = _get_series_array_type(dtype)
return SeriesType(dtype, data, index)
@property
def key(self):
# needed?
return self.dtype, self.data, self.index, self.is_named
@property
def ndim(self):
return self.data.ndim
def unify(self, typingctx, other):
if isinstance(other, SeriesType):
new_index = types.none
if self.index != types.none and other.index != types.none:
new_index = self.index.unify(typingctx, other.index)
elif other.index != types.none:
new_index = other.index
elif self.index != types.none:
new_index = self.index
# If dtype matches or other.dtype is undefined (inferred)
if other.dtype == self.dtype or not other.dtype.is_precise():
return SeriesType(
self.dtype,
self.data.unify(typingctx, other.data),
new_index)
# XXX: unify Series/Array as Array
return super(SeriesType, self).unify(typingctx, other)
def can_convert_to(self, typingctx, other):
# same as types.Array
if (isinstance(other, SeriesType) and other.dtype == self.dtype):
# called for overload selection sometimes
# TODO: fix index
if self.index == types.none and other.index == types.none:
return self.data.can_convert_to(typingctx, other.data)
if self.index != types.none and other.index != types.none:
return max(self.data.can_convert_to(typingctx, other.data),
self.index.can_convert_to(typingctx, other.index))
def is_precise(self):
# same as types.Array
return self.dtype.is_precise()
@property
def iterator_type(self):
# TODO: fix timestamp
return SeriesIterator(self)
class SeriesIterator(types.SimpleIteratorType):
"""
Type class for iterator over dataframe series.
"""
def __init__(self, series_type):
self.series_type = series_type
self.array_type = series_type.data
name = f'iter({self.series_type.data})'
yield_type = series_type.dtype
super(SeriesIterator, self).__init__(name, yield_type)
@property
def _iternext(self):
if isinstance(self.array_type, StringArrayType):
return iternext_str_array
elif isinstance(self.array_type, types.Array):
return iternext_series_array
@register_model(SeriesIterator)
class SeriesIteratorModel(models.StructModel):
def __init__(self, dmm, fe_type):
members = [('index', types.EphemeralPointer(types.uintp)),
('array', fe_type.series_type.data)]
models.StructModel.__init__(self, dmm, fe_type, members)
def _get_series_array_type(dtype):
"""get underlying default array type of series based on its dtype
"""
# list(list(str))
if dtype == types.List(string_type):
# default data layout is list but split view is used if possible
return list_string_array_type
# string array
elif dtype == string_type:
return string_array_type
# categorical
if isinstance(dtype, PDCategoricalDtype):
return CategoricalArray(dtype)
# use recarray data layout for series of tuples
if isinstance(dtype, types.BaseTuple):
if any(not isinstance(t, types.Number) for t in dtype.types):
# TODO: support more types. what types can be in recarrays?
raise ValueError("series tuple dtype {} includes non-numerics".format(dtype))
np_dtype = np.dtype(
','.join(str(t) for t in dtype.types), align=True)
dtype = numba.numpy_support.from_dtype(np_dtype)
# TODO: other types?
# regular numpy array
return types.Array(dtype, 1, 'C')
def is_str_series_typ(t):
return isinstance(t, SeriesType) and t.dtype == string_type
def is_dt64_series_typ(t):
return isinstance(t, SeriesType) and t.dtype == types.NPDatetime('ns')
def is_timedelta64_series_typ(t):
return isinstance(t, SeriesType) and t.dtype == types.NPTimedelta('ns')
def is_datetime_date_series_typ(t):
return isinstance(t, SeriesType) and t.dtype == datetime_date_type
# register_model(SeriesType)(models.ArrayModel)
# need to define model since fix_df_array overload goes to native code
@register_model(SeriesType)
class SeriesModel(models.StructModel):
def __init__(self, dmm, fe_type):
name_typ = string_type if fe_type.is_named else types.none
members = [
('data', fe_type.data),
('index', fe_type.index),
('name', name_typ),
]
super(SeriesModel, self).__init__(dmm, fe_type, members)
make_attribute_wrapper(SeriesType, 'data', '_data')
make_attribute_wrapper(SeriesType, 'index', '_index')
make_attribute_wrapper(SeriesType, 'name', '_name')
@lower_builtin('getiter', SeriesType)
def getiter_series(context, builder, sig, args):
"""
Getting iterator for the Series type
:param context: context descriptor
:param builder: llvmlite IR Builder
:param sig: iterator signature
:param args: tuple with iterator arguments, such as instruction, operands and types
:param result: iternext result
:return: reference to iterator
"""
arraytype = sig.args[0].data
# Create instruction to get array to iterate
zero_member_pointer = context.get_constant(types.intp, 0)
zero_member = context.get_constant(types.int32, 0)
alloca = args[0].operands[0]
gep_result = builder.gep(alloca, [zero_member_pointer, zero_member])
array = builder.load(gep_result)
# TODO: call numba getiter with gep_result for array
iterobj = context.make_helper(builder, sig.return_type)
zero_index = context.get_constant(types.intp, 0)
indexptr = cgutils.alloca_once_value(builder, zero_index)
iterobj.index = indexptr
iterobj.array = array
if context.enable_nrt:
context.nrt.incref(builder, arraytype, array)
result = iterobj._getvalue()
# Note: a decref on the iterator will dereference all internal MemInfo*
out = impl_ret_new_ref(context, builder, sig.return_type, result)
return out
# TODO: call it from numba.targets.arrayobj, need separate function in numba
def iternext_series_array(context, builder, sig, args, result):
"""
Implementation of iternext() for the ArrayIterator type
:param context: context descriptor
:param builder: llvmlite IR Builder
:param sig: iterator signature
:param args: tuple with iterator arguments, such as instruction, operands and types
:param result: iternext result
"""
[iterty] = sig.args
[iter] = args
arrayty = iterty.array_type
if arrayty.ndim != 1:
raise NotImplementedError("iterating over %dD array" % arrayty.ndim)
iterobj = context.make_helper(builder, iterty, value=iter)
ary = make_array(arrayty)(context, builder, value=iterobj.array)
nitems, = cgutils.unpack_tuple(builder, ary.shape, count=1)
index = builder.load(iterobj.index)
is_valid = builder.icmp(lc.ICMP_SLT, index, nitems)
result.set_valid(is_valid)
with builder.if_then(is_valid):
value = _getitem_array1d(context, builder, arrayty, ary, index,
wraparound=False)
result.yield_(value)
nindex = cgutils.increment_index(builder, index)
builder.store(nindex, iterobj.index)
@lower_builtin('iternext', SeriesIterator)
@iternext_impl(RefType.BORROWED)
def iternext_series(context, builder, sig, args, result):
"""
Iternext implementation depending on Array type
:param context: context descriptor
:param builder: llvmlite IR Builder
:param sig: iterator signature
:param args: tuple with iterator arguments, such as instruction, operands and types
:param result: iternext result
"""
iternext_func = sig.args[0]._iternext
iternext_func(context=context, builder=builder, sig=sig, args=args, result=result)
def series_to_array_type(typ, replace_boxed=False):
return typ.data
# return _get_series_array_type(typ.dtype)
def is_series_type(typ):
return isinstance(typ, SeriesType)
def arr_to_series_type(arr):
series_type = None
if isinstance(arr, types.Array):
series_type = SeriesType(arr.dtype, arr)
elif arr == string_array_type:
# StringArray is readonly
series_type = SeriesType(string_type)
elif arr == list_string_array_type:
series_type = SeriesType(types.List(string_type))
elif arr == string_array_split_view_type:
series_type = SeriesType(types.List(string_type),
string_array_split_view_type)
return series_type
def if_series_to_array_type(typ, replace_boxed=False):
if isinstance(typ, SeriesType):
return series_to_array_type(typ, replace_boxed)
if isinstance(typ, (types.Tuple, types.UniTuple)):
return types.Tuple(
[if_series_to_array_type(t, replace_boxed) for t in typ.types])
if isinstance(typ, types.List):
return types.List(if_series_to_array_type(typ.dtype, replace_boxed))
if isinstance(typ, types.Set):
return types.Set(if_series_to_array_type(typ.dtype, replace_boxed))
# TODO: other types that can have Series inside?
return typ
def if_arr_to_series_type(typ):
if isinstance(typ, types.Array) or typ in (string_array_type,
list_string_array_type, string_array_split_view_type):
return arr_to_series_type(typ)
if isinstance(typ, (types.Tuple, types.UniTuple)):
return types.Tuple([if_arr_to_series_type(t) for t in typ.types])
if isinstance(typ, types.List):
return types.List(if_arr_to_series_type(typ.dtype))
if isinstance(typ, types.Set):
return types.Set(if_arr_to_series_type(typ.dtype))
# TODO: other types that can have Arrays inside?
return typ
# TODO remove this cast?
@lower_cast(SeriesType, string_array_type)
@lower_cast(string_array_type, SeriesType)
def cast_string_series(context, builder, fromty, toty, val):
return val
# cast Series(int8) to Series(cat) for init_series() in test_csv_cat1
# TODO: separate array type for Categorical data
@lower_cast(SeriesType, types.Array)
@lower_cast(types.Array, SeriesType)
@lower_cast(SeriesType, SeriesType)
def cast_series(context, builder, fromty, toty, val):
return val
# --------------------------------------------------------------------------- #
# --- typing similar to arrays adopted from arraydecl.py, npydecl.py -------- #
@infer_getattr
class SeriesAttribute(AttributeTemplate):
key = SeriesType
# PR135. This needs to be commented out
def resolve_values(self, ary):
return series_to_array_type(ary, True)
# PR135. This needs to be commented out
# def resolve_shape(self, ary):
# return types.Tuple((types.int64,))
# PR171. This needs to be commented out
# def resolve_index(self, ary):
# return ary.index
def resolve_str(self, ary):
assert ary.dtype in (string_type, types.List(string_type))
# TODO: add dtype to series_str_methods_type
return series_str_methods_type
def resolve_dt(self, ary):
assert ary.dtype == types.NPDatetime('ns')
return series_dt_methods_type
# PR135. This needs to be commented out
def resolve_iat(self, ary):
return SeriesIatType(ary)
# PR135. This needs to be commented out
def resolve_iloc(self, ary):
# TODO: support iat/iloc differences
return SeriesIatType(ary)
# PR135. This needs to be commented out
def resolve_loc(self, ary):
# TODO: support iat/iloc differences
return SeriesIatType(ary)
@bound_function("array.astype")
def resolve_astype(self, ary, args, kws):
# TODO: handle other types like datetime etc.
dtype, = args
if ((isinstance(dtype, types.Function) and dtype.typing_key == str)
or (isinstance(dtype, types.StringLiteral) and dtype.literal_value == 'str')):
ret_type = SeriesType(string_type)
sig = signature(ret_type, *args)
else:
resolver = ArrayAttribute.resolve_astype.__wrapped__
sig = resolver(self, ary.data, args, kws)
sig.return_type = if_arr_to_series_type(sig.return_type)
return sig
@bound_function("array.copy")
def resolve_copy(self, ary, args, kws):
# TODO: copy other types like list(str)
dtype = ary.dtype
if dtype == string_type:
ret_type = SeriesType(string_type)
sig = signature(ret_type, *args)
else:
resolver = ArrayAttribute.resolve_copy.__wrapped__
sig = resolver(self, ary.data, args, kws)
sig.return_type = if_arr_to_series_type(sig.return_type)
return sig
@bound_function("series.rolling")
def resolve_rolling(self, ary, args, kws):
return signature(SeriesRollingType(ary.dtype), *args)
@bound_function("array.argsort")
def resolve_argsort(self, ary, args, kws):
resolver = ArrayAttribute.resolve_argsort.__wrapped__
sig = resolver(self, ary.data, args, kws)
sig.return_type = if_arr_to_series_type(sig.return_type)
return sig
@bound_function("series.sort_values")
def resolve_sort_values(self, ary, args, kws):
# output will have permuted input index
out_index = ary.index
if out_index == types.none:
out_index = types.Array(types.intp, 1, 'C')
out = SeriesType(ary.dtype, ary.data, out_index)
return signature(out, *args)
# @bound_function("array.take")
# def resolve_take(self, ary, args, kws):
# resolver = ArrayAttribute.resolve_take.__wrapped__
# sig = resolver(self, ary.data, args, kws)
# sig.return_type = if_arr_to_series_type(sig.return_type)
# return sig
# @bound_function("series.quantile")
# def resolve_quantile(self, ary, args, kws):
# # TODO: fix quantile output type if not float64
# return signature(types.float64, *args)
@bound_function("series.count")
def resolve_count(self, ary, args, kws):
return signature(types.intp, *args)
# @bound_function("series.nunique")
# def resolve_nunique(self, ary, args, kws):
# return signature(types.intp, *args)
# @bound_function("series.unique")
# def resolve_unique(self, ary, args, kws):
# # unique returns ndarray for some reason
# arr_typ = series_to_array_type(ary)
# return signature(arr_typ, *args)
@bound_function("series.describe")
def resolve_describe(self, ary, args, kws):
# TODO: return namedtuple or labeled Series
return signature(string_type, *args)
@bound_function("series.fillna")
def resolve_fillna(self, ary, args, kws):
out = ary
# output is None for inplace case
if 'inplace' in kws and kws['inplace'] == types.literal(True):
out = types.none
return signature(out, *args)
@bound_function("series.dropna")
def resolve_dropna(self, ary, args, kws):
out = ary
# output is None for inplace case
if 'inplace' in kws and kws['inplace'] == types.literal(True):
out = types.none
return signature(out, *args)
@bound_function("series.shift")
def resolve_shift(self, ary, args, kws):
# TODO: support default period argument
out = ary
# integers are converted to float64 to store NaN
if isinstance(ary.dtype, types.Integer):
out = out.copy(dtype=types.float64)
return signature(out, *args)
@bound_function("series.pct_change")
def resolve_pct_change(self, ary, args, kws):
# TODO: support default period argument
out = ary
# integers are converted to float64 to store NaN
if isinstance(ary.dtype, types.Integer):
out = out.copy(dtype=types.float64)
return signature(out, *args)
def _resolve_map_func(self, ary, args, kws):
dtype = ary.dtype
# getitem returns Timestamp for dt_index and series(dt64)
if dtype == types.NPDatetime('ns'):
dtype = pandas_timestamp_type
code = args[0].literal_value.code
_globals = {'np': np}
# XXX hack in hiframes_typed to make globals available
if hasattr(args[0].literal_value, 'globals'):
# TODO: use code.co_names to find globals actually used?
_globals = args[0].literal_value.globals
f_ir = numba.ir_utils.get_ir_of_code(_globals, code)
f_typemap, f_return_type, f_calltypes = numba.compiler.type_inference_stage(
self.context, f_ir, (dtype,), None)
return signature(SeriesType(f_return_type), *args)
@bound_function("series.map")
def resolve_map(self, ary, args, kws):
return self._resolve_map_func(ary, args, kws)
@bound_function("series.apply")
def resolve_apply(self, ary, args, kws):
# TODO: handle apply differences: extra args, np ufuncs etc.
return self._resolve_map_func(ary, args, kws)
def _resolve_combine_func(self, ary, args, kws):
dtype1 = ary.dtype
# getitem returns Timestamp for dt_index and series(dt64)
if dtype1 == types.NPDatetime('ns'):
dtype1 = pandas_timestamp_type
dtype2 = args[0].dtype
if dtype2 == types.NPDatetime('ns'):
dtype2 = pandas_timestamp_type
code = args[1].literal_value.code
f_ir = numba.ir_utils.get_ir_of_code({'np': np}, code)
f_typemap, f_return_type, f_calltypes = numba.compiler.type_inference_stage(
self.context, f_ir, (dtype1, dtype2,), None)
return signature(SeriesType(f_return_type), *args)
@bound_function("series.combine")
def resolve_combine(self, ary, args, kws):
return self._resolve_combine_func(ary, args, kws)
# @bound_function("series.abs")
# def resolve_abs(self, ary, args, kws):
# # call np.abs(A) to get return type
# arr_typ = series_to_array_type(ary)
# all_args = tuple([arr_typ] + list(args))
# ret_typ = self.context.resolve_function_type(np.abs, all_args, kws).return_type
# ret_typ = if_arr_to_series_type(ret_typ)
# return signature(ret_typ, *args)
def _resolve_cov_func(self, ary, args, kws):
# array is valid since hiframes_typed calls this after type replacement
assert len(args) == 1 and isinstance(args[0], (SeriesType, types.Array))
assert isinstance(ary.dtype, types.Number)
assert isinstance(args[0].dtype, types.Number)
is_complex_op = isinstance(ary.dtype, types.Complex) or isinstance(args[0].dtype, types.Complex)
ret_typ = types.complex128 if is_complex_op else types.float64
return signature(ret_typ, *args)
@bound_function("series.cov")
def resolve_cov(self, ary, args, kws):
return self._resolve_cov_func(ary, args, kws)
@bound_function("series.corr")
def resolve_corr(self, ary, args, kws):
return self._resolve_cov_func(ary, args, kws)
# PR135. This needs to be commented out
@bound_function("series.append")
def resolve_append(self, ary, args, kws):
# TODO: ignore_index
assert not kws
arr_typ = if_series_to_array_type(ary)
other, = args
if isinstance(other, (SeriesType, types.Array)):
all_arrs = types.Tuple((arr_typ, if_series_to_array_type(other)))
elif isinstance(other, types.BaseTuple):
all_arrs = types.Tuple((arr_typ, *[if_series_to_array_type(a) for a in other.types]))
elif isinstance(other, (types.List, types.Set)):
# add only one value from the list for typing since it shouldn't
# matter for np.concatenate typing
all_arrs = types.Tuple((arr_typ, if_series_to_array_type(other.dtype)))
else:
raise ValueError("Invalid input for Series.append (Series, or tuple/list of Series expected)")
# TODO: list
# call np.concatenate to handle type promotion e.g. int, float -> float
ret_typ = self.context.resolve_function_type(np.concatenate, (all_arrs,), kws).return_type
ret_typ = if_arr_to_series_type(ret_typ)
return signature(ret_typ, *args)
@bound_function("series.isna")
def resolve_isna(self, ary, args, kws):
assert not kws
assert not args
return signature(SeriesType(types.boolean))
# alias of isna
@bound_function("series.isnull")
def resolve_isnull(self, ary, args, kws):
assert not kws
assert not args
return signature(SeriesType(types.boolean))
@bound_function("series.notna")
def resolve_notna(self, ary, args, kws):
assert not kws
assert not args
return signature(SeriesType(types.boolean))
@bound_function("series.nlargest")
def resolve_nlargest(self, ary, args, kws):
assert not kws
return signature(ary, *args)
@bound_function("series.nsmallest")
def resolve_nsmallest(self, ary, args, kws):
assert not kws
return signature(ary, *args)
@bound_function("series.head")
def resolve_head(self, ary, args, kws):
assert not kws
return signature(ary, *args)
@bound_function("series.median")
def resolve_median(self, ary, args, kws):
assert not kws
dtype = ary.dtype
# median converts integer output to float
dtype = types.float64 if isinstance(dtype, types.Integer) else dtype
return signature(dtype, *args)
@bound_function("series.idxmin")
def resolve_idxmin(self, ary, args, kws):
assert not kws
return signature(types.intp, *args)
@bound_function("series.idxmax")
def resolve_idxmax(self, ary, args, kws):
assert not kws
return signature(types.intp, *args)
# @bound_function("series.max")
# def resolve_max(self, ary, args, kws):
# assert not kws
# dtype = ary.dtype
# dtype = (pandas_timestamp_type
# if isinstance(dtype, types.NPDatetime) else dtype)
# return signature(dtype, *args)
# @bound_function("series.min")
# def resolve_min(self, ary, args, kws):
# assert not kws
# dtype = ary.dtype
# dtype = (pandas_timestamp_type
# if isinstance(dtype, types.NPDatetime) else dtype)
# return signature(dtype, *args)
@bound_function("series.value_counts")
def resolve_value_counts(self, ary, args, kws):
# output is int series with original data as index
out = SeriesType(
types.int64, types.Array(types.int64, 1, 'C'), ary.data)
return signature(out, *args)
@bound_function("series.rename")
def resolve_rename(self, ary, args, kws):
# TODO: support index rename, kws
assert len(args) == 1 and isinstance(
args[0], (types.UnicodeType, types.StringLiteral))
out = SeriesType(ary.dtype, ary.data, ary.index, True)
return signature(out, *args)
# TODO: use ops logic from pandas/core/ops.py
# # called from numba/numpy_support.py:resolve_output_type
# # similar to SmartArray (targets/smartarray.py)
# @type_callable('__array_wrap__')
# def type_series_array_wrap(context):
# def typer(input_type, result):
# if isinstance(input_type, SeriesType):
# return input_type.copy(dtype=result.dtype,
# ndim=result.ndim,
# layout=result.layout)
# return typer
str2str_methods = ('capitalize', 'lower', 'lstrip', 'rstrip',
'strip', 'swapcase', 'title', 'upper')
class SeriesStrMethodType(types.Type):
def __init__(self):
name = "SeriesStrMethodType"
super(SeriesStrMethodType, self).__init__(name)
series_str_methods_type = SeriesStrMethodType()
@infer_getattr
class SeriesStrMethodAttribute(AttributeTemplate):
key = SeriesStrMethodType
@bound_function("strmethod.contains")
def resolve_contains(self, ary, args, kws):
return signature(SeriesType(types.bool_), *args)
@bound_function("strmethod.len")
def resolve_len(self, ary, args, kws):
return signature(SeriesType(types.int64), *args)
@bound_function("strmethod.replace")
def resolve_replace(self, ary, args, kws):
return signature(SeriesType(string_type), *args)
@bound_function("strmethod.split")
def resolve_split(self, ary, args, kws):
out = SeriesType(types.List(string_type))
if (len(args) == 1 and isinstance(args[0], types.StringLiteral)
and len(args[0].literal_value) == 1):
out = SeriesType(types.List(string_type), string_array_split_view_type)
return signature(out, *args)
@bound_function("strmethod.get")
def resolve_get(self, ary, args, kws):
# XXX only list(list(str)) supported
return signature(SeriesType(string_type), *args)
def generic_resolve(self, s_str, func_name):
if func_name not in str2str_methods:
raise ValueError("Series.str.{} is not supported yet".format(
func_name))
template_key = 'strmethod.' + func_name
out_typ = SeriesType(string_type)
class MethodTemplate(AbstractTemplate):
key = template_key
def generic(self, args, kws):
return signature(out_typ, *args)
return types.BoundFunction(MethodTemplate, s_str)
class SeriesDtMethodType(types.Type):
def __init__(self):
name = "SeriesDtMethodType"
super(SeriesDtMethodType, self).__init__(name)
series_dt_methods_type = SeriesDtMethodType()
@infer_getattr
class SeriesDtMethodAttribute(AttributeTemplate):
key = SeriesDtMethodType
def resolve_date(self, ary):
return SeriesType(datetime_date_type) # TODO: name, index
# all date fields return int64 same as Timestamp fields
def resolve_date_field(self, ary):
return SeriesType(types.int64)
for field in hpat.hiframes.pd_timestamp_ext.date_fields:
setattr(SeriesDtMethodAttribute, "resolve_" + field, resolve_date_field)
class SeriesRollingType(types.Type):
def __init__(self, dtype):
self.dtype = dtype
name = "SeriesRollingType({})".format(dtype)
super(SeriesRollingType, self).__init__(name)
@infer_getattr
class SeriesRollingAttribute(AttributeTemplate):
key = SeriesRollingType
@bound_function("rolling.apply")
def resolve_apply(self, ary, args, kws):
# result is always float64 (see Pandas window.pyx:roll_generic)
return signature(SeriesType(types.float64), *args)
@bound_function("rolling.cov")
def resolve_cov(self, ary, args, kws):
return signature(SeriesType(types.float64), *args)
@bound_function("rolling.corr")
def resolve_corr(self, ary, args, kws):
return signature(SeriesType(types.float64), *args)
# similar to install_array_method in arraydecl.py
def install_rolling_method(name, generic):
my_attr = {"key": "rolling." + name, "generic": generic}
temp_class = type("Rolling_" + name, (AbstractTemplate,), my_attr)
def rolling_attribute_attachment(self, ary):
return types.BoundFunction(temp_class, ary)
setattr(SeriesRollingAttribute, "resolve_" + name, rolling_attribute_attachment)
def rolling_generic(self, args, kws):
# output is always float64
return signature(SeriesType(types.float64), *args)
for fname in supported_rolling_funcs:
install_rolling_method(fname, rolling_generic)
class SeriesIatType(types.Type):
def __init__(self, stype):
self.stype = stype
name = "SeriesIatType({})".format(stype)
super(SeriesIatType, self).__init__(name)
# PR135. This needs to be commented out
@infer_global(operator.getitem)
class GetItemSeriesIat(AbstractTemplate):
key = operator.getitem
def generic(self, args, kws):
# iat[] is the same as regular getitem
if isinstance(args[0], SeriesIatType):
return GetItemSeries.generic(self, (args[0].stype, args[1]), kws)
@infer
@infer_global(operator.eq)
@infer_global(operator.ne)
@infer_global(operator.ge)
@infer_global(operator.gt)
@infer_global(operator.le)
@infer_global(operator.lt)
class SeriesCompEqual(AbstractTemplate):
key = '=='
def generic(self, args, kws):
from hpat.str_arr_ext import is_str_arr_typ
assert not kws
[va, vb] = args
# if one of the inputs is string array
if is_str_series_typ(va) or is_str_series_typ(vb):
# inputs should be either string array or string
assert is_str_arr_typ(va) or va == string_type
assert is_str_arr_typ(vb) or vb == string_type
return signature(SeriesType(types.boolean), va, vb)
if ((is_dt64_series_typ(va) and vb == string_type)
or (is_dt64_series_typ(vb) and va == string_type)):
return signature(SeriesType(types.boolean), va, vb)
@infer
class CmpOpNEqSeries(SeriesCompEqual):
key = '!='
@infer
class CmpOpGESeries(SeriesCompEqual):
key = '>='
@infer
class CmpOpGTSeries(SeriesCompEqual):
key = '>'
@infer
class CmpOpLESeries(SeriesCompEqual):
key = '<='
@infer
class CmpOpLTSeries(SeriesCompEqual):
key = '<'
# @infer_global(operator.getitem)
# class GetItemBuffer(AbstractTemplate):
# key = operator.getitem
# def generic(self, args, kws):
# assert not kws
# [ary, idx] = args
# import pdb; pdb.set_trace()
# if not isinstance(ary, SeriesType):
# return
# out = get_array_index_type(ary, idx)
# # check result to be dt64 since it might be sliced array
# # replace result with Timestamp
# if out is not None and out.result == types.NPDatetime('ns'):
# return signature(pandas_timestamp_type, ary, out.index)
def install_array_method(name, generic):
# taken from arraydecl.py, Series instead of Array
my_attr = {"key": "array." + name, "generic": generic}
temp_class = type("Series_" + name, (AbstractTemplate,), my_attr)
def array_attribute_attachment(self, ary):
return types.BoundFunction(temp_class, ary)
setattr(SeriesAttribute, "resolve_" + name, array_attribute_attachment)
def generic_expand_cumulative_series(self, args, kws):
# taken from arraydecl.py, replaced Array with Series
assert not args
assert not kws
assert isinstance(self.this, SeriesType)
return_type = SeriesType(_expand_integer(self.this.dtype))
return signature(return_type, recvr=self.this)
# replacing cumsum/cumprod since arraydecl.py definition uses types.Array
for fname in ["cumsum", "cumprod"]:
install_array_method(fname, generic_expand_cumulative_series)
# TODO: add itemsize, strides, etc. when removed from Pandas
_not_series_array_attrs = ['flat', 'ctypes', 'itemset', 'reshape', 'sort', 'flatten',
'resolve_take', 'resolve_max', 'resolve_min', 'resolve_nunique',
'resolve_prod']
# use ArrayAttribute for attributes not defined in SeriesAttribute
for attr, func in numba.typing.arraydecl.ArrayAttribute.__dict__.items():
if (attr.startswith('resolve_')
and attr not in SeriesAttribute.__dict__
and attr not in _not_series_array_attrs):
setattr(SeriesAttribute, attr, func)