/
event_set_ops.py
5007 lines (4076 loc) Β· 160 KB
/
event_set_ops.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 2021 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=import-outside-toplevel
from __future__ import annotations
from datetime import datetime
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Union
from temporian.core.data.duration import Duration
if TYPE_CHECKING:
from temporian.core.operators.map import MapFunction
from temporian.core.typing import (
EventSetOrNode,
IndexKeyList,
TargetDtypes,
WindowLength,
)
T_SCALAR = (int, float)
class EventSetOperations:
"""Mixin class for EventSet-like classes.
Defines the methods that can be called on both EventSets and EventSetNodes
interchangeably.
"""
@property
def _clsname(self) -> str:
"""Shortcut that returns the class' name."""
return self.__class__.__name__
#################
# MAGIC METHODS #
#################
def __getitem__(self, feature_names: Union[str, List[str]]):
"""Creates an EventSet with a subset of the features."""
from temporian.core.operators.select import select
return select(self, feature_names)
def __setitem__(self, feature_names: Any, value: Any) -> None:
"""Fails, features cannot be assigned."""
raise TypeError(
f"Cannot assign features to an existing {self._clsname}. New"
f" {self._clsname}s should be created instead. Check out the"
" `tp.glue()` operator to combine features from several"
f" {self._clsname}s."
)
def __bool__(self) -> None:
"""Catches bool evaluation with an error message."""
# Called on "if node" or "if evset" conditions
# TODO: modify to similar numpy msg if we implement .any() or .all()
raise ValueError(
f"The truth value of a {self._clsname} is ambiguous. Check"
f" condition element-wise or use the `{self._clsname}.cast()`"
" operator to convert to boolean."
)
def _raise_error(
self, op_name: str, other: Any, allowed_types: str
) -> None:
"""Raises an error message.
This utility method is used in operator implementations, e.g., +, - *.
"""
raise ValueError(
f"Cannot use operator '{op_name}' on {self._clsname} and"
f" {type(other)} objects. Only {self._clsname} or values of type"
f" ({allowed_types}) are supported."
)
def __ne__(self: EventSetOrNode, other: Any) -> EventSetOrNode:
"""Computes not equal (`self != other`) element-wise with another
[`EventSet`][temporian.EventSet] or a scalar value.
If an EventSet, each feature in `self` is compared element-wise to
the feature in `other` in the same position. `self` and `other`
must have the same sampling and the same number of features.
If a scalar value, each item in each feature in `self` is compared to
`other`.
Note that it will always return True on NaNs (even if both are).
Example with EventSet:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f2": [-10, 100, 5]},
... same_sampling_as=a
... )
>>> c = a != b
>>> c
indexes: []
features: [('f1', bool_)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [ True False True]
...
```
Example with scalar value:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200], "f2": [-10, 100, 5]}
... )
>>> b = a != 100
>>> b
indexes: []
features: [('f1', bool_), ('f2', bool_)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [ True False True]
'f2': [ True False True]
...
```
Args:
other: EventSet or scalar value.
Returns:
Result of the comparison.
"""
if isinstance(other, self.__class__):
from temporian.core.operators.binary import not_equal
return not_equal(input_1=self, input_2=other)
if isinstance(other, T_SCALAR + (bool, str)):
from temporian.core.operators.scalar import not_equal_scalar
return not_equal_scalar(input=self, value=other)
self._raise_error("ne", other, "int,float,bool,str")
assert False
def __add__(self: EventSetOrNode, other: Any) -> EventSetOrNode:
"""Adds an [`EventSet`][temporian.EventSet] or a scalar value to
`self` element-wise.
If an EventSet, each feature in `self` is added to the feature in
`other` in the same position. `self` and `other` must have the same
sampling, index, number of features and dtype for the features in the
same positions.
If a scalar, `other` is added to each item in each feature in `self`.
Example with EventSet:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200], "f2": [10, -10, 5]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f3": [-1, 1, 2], "f4": [1, -1, 5]},
... same_sampling_as=a
... )
>>> c = a + b
>>> c
indexes: []
features: [('f1', int64), ('f2', int64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [ -1 101 202]
'f2': [ 11 -11 10]
...
```
Example with scalar value:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200], "f2": [10, -10, 5]}
... )
>>> b = a + 3
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 3 103 203]
'f2': [13 -7 8]
...
>>> b = 3 + a
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 3 103 203]
'f2': [13 -7 8]
...
```
Cast dtypes example:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200], "f2": [10., -10., 5.]}
... )
>>> # Cannot add: f1 is int64 but f2 is float64
>>> c = a["f1"] + a["f2"]
Traceback (most recent call last):
...
ValueError: ... corresponding features should have the same dtype. ...
>>> # Cast f1 to float
>>> c = a["f1"].cast(tp.float64) + a["f2"]
>>> c
indexes: []
features: [('f1', float64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [ 10. 90. 205.]
...
```
Resample example:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"fa": [1, 2, 3]},
... )
>>> b = tp.event_set(
... timestamps=[-1, 1.5, 3, 5],
... features={"fb": [-10, 15, 30, 50]},
... )
>>> # Cannot add different samplings
>>> c = a + b
Traceback (most recent call last):
...
ValueError: ... should have the same sampling. ...
>>> # Resample a to match b timestamps
>>> c = a.resample(b) + b
>>> c
indexes: []
features: [('fa', int64)]
events:
(4 events):
timestamps: [-1. 1.5 3. 5. ]
'fa': [-10 16 33 53]
...
```
Reindex example:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3, 4],
... features={
... "cat": [1, 1, 2, 2],
... "M": [10, 20, 30, 40]
... },
... indexes=["cat"]
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3, 4],
... features={
... "cat": [1, 1, 2, 2],
... "N": [10, 20, 30, 40]
... },
... )
>>> # Cannot add with different index (only 'a' is indexed by 'cat')
>>> c = a + b
Traceback (most recent call last):
...
ValueError: Arguments don't have the same index. ...
>>> # Add index 'cat' to b
>>> b = b.add_index("cat")
>>> # Make explicit same samplings and add
>>> c = a + b.resample(a)
>>> c
indexes: [('cat', int64)]
features: [('M', int64)]
events:
cat=1 (2 events):
timestamps: [1. 2.]
'M': [20 40]
cat=2 (2 events):
timestamps: [3. 4.]
'M': [60 80]
...
```
Args:
other: EventSet or scalar value.
Returns:
Result of the operation.
"""
# TODO: In this and other operants, factor code and add support for
# swapping operators (e.g. a+1, a+b, 1+a).
if isinstance(other, self.__class__):
from temporian.core.operators.binary import add
return add(input_1=self, input_2=other)
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import add_scalar
return add_scalar(input=self, value=other)
self._raise_error("add", other, "int,float")
assert False
def __radd__(self, other: Any):
return self.__add__(other)
def __sub__(self: EventSetOrNode, other: Any) -> EventSetOrNode:
"""Subtracts an [`EventSet`][temporian.EventSet] or a scalar value from
`self` element-wise.
If an EventSet, each feature in `self` is subtracted from the feature in
`other` in the same position. `self` and `other` must have the same
sampling, index, number of features and dtype for the features in the
same positions.
If a scalar, `other` is subtracted from each item in each feature in
`self`.
See examples in [`EventSet.__add__()`][temporian.EventSet.__add__] to
see how to match samplings, dtypes and index, in order to apply
arithmetic operators in different EventSets.
Example with EventSet:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f2": [10, 20, -5]},
... same_sampling_as=a
... )
>>> c = a - b
>>> c
indexes: []
features: [('f1', int64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [-10 80 205]
...
```
Example with scalar value:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200], "f2": [10, -10, 5]}
... )
>>> b = a - 3
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ -3 97 197]
'f2': [ 7 -13 2]
...
>>> c = 3 - a
>>> c
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 3 -97 -197]
'f2': [-7 13 -2]
...
```
Args:
other: EventSet or scalar value.
Returns:
Result of the operation.
"""
if isinstance(other, self.__class__):
from temporian.core.operators.binary import subtract
return subtract(input_1=self, input_2=other)
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import subtract_scalar
return subtract_scalar(minuend=self, subtrahend=other)
self._raise_error("subtract", other, "int,float")
assert False
def __rsub__(self, other: Any):
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import subtract_scalar
return subtract_scalar(minuend=other, subtrahend=self)
self._raise_error("subtract", other, "int,float")
assert False
def __mul__(self: EventSetOrNode, other: Any) -> EventSetOrNode:
"""Multiplies an [`EventSet`][temporian.EventSet] or a scalar value with
`self` element-wise.
If an EventSet, each feature in `self` is multiplied with the feature in
`other` in the same position. `self` and `other` must have the same
sampling, index, number of features and dtype for the features in the
same positions.
If a scalar, each item in each feature in `self` is multiplied with
`other`.
See examples in [`EventSet.__add__()`][temporian.EventSet.__add__] to
see how to match samplings, dtypes and index, in order to apply
arithmetic operators in different EventSets.
Example with EventSet:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f2": [10, 3, 2]},
... same_sampling_as=a
... )
>>> c = a * b
>>> c
indexes: []
features: [('f1', int64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [ 0 300 400]
...
```
Example with scalar value:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200], "f2": [10, -10, 5]}
... )
>>> b = a * 2
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 0 200 400]
'f2': [ 20 -20 10]
...
>>> b = 2 * a
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 0 200 400]
'f2': [ 20 -20 10]
...
```
Args:
other: EventSet or scalar value.
Returns:
Result of the operation.
"""
if isinstance(other, self.__class__):
from temporian.core.operators.binary import multiply
return multiply(input_1=self, input_2=other)
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import multiply_scalar
return multiply_scalar(input=self, value=other)
self._raise_error("multiply", other, "int,float")
assert False
def __rmul__(self, other: Any):
return self.__mul__(other)
def __neg__(self: EventSetOrNode) -> EventSetOrNode:
"""Negates an [`EventSet`][temporian.EventSet] element-wise.
Example:
```python
>>> a = tp.event_set(
... timestamps=[1, 2],
... features={"M": [1, -5], "N": [-1.0, 5.5]},
... )
>>> -a
indexes: ...
'M': [-1 5]
'N': [ 1. -5.5]
...
```
Returns:
Negated EventSet.
"""
from temporian.core.operators.scalar import multiply_scalar
return multiply_scalar(input=self, value=-1)
def __invert__(self: EventSetOrNode) -> EventSetOrNode:
"""Inverts a boolean [`EventSet`][temporian.EventSet] element-wise.
Swaps False <-> True.
Does not work on integers, they should be cast to
[`tp.bool_`][temporian.bool_] beforehand, using
[`EventSet.cast()`][temporian.EventSet.cast].
Example:
```python
>>> a = tp.event_set(
... timestamps=[1, 2],
... features={"M": [1, 5], "N": [1.0, 5.5]},
... )
>>> # Boolean EventSet
>>> b = a < 2
>>> b
indexes: ...
'M': [ True False]
'N': [ True False]
...
>>> # Inverted EventSet
>>> c = ~b
>>> c
indexes: ...
'M': [False True]
'N': [False True]
...
```
Returns:
Inverted EventSet.
"""
from temporian.core.operators.unary import invert
return invert(input=self)
def __abs__(self):
from temporian.core.operators.unary import abs
return abs(input=self)
def __truediv__(self: EventSetOrNode, other: Any) -> EventSetOrNode:
"""Divides `self` by an [`EventSet`][temporian.EventSet] or a scalar
value element-wise.
If an EventSet, each feature in `self` is divided by the feature in
`other` in the same position. `self` and `other` must have the same
sampling, index, number of features and dtype for the features in the
same positions.
If a scalar, each item in each feature in `self` is divided by `other`.
This operator cannot be used in features with dtypes `int32` or `int64`.
Cast to float before (see example) or use
[`EventSet.__floordiv__()`][temporian.EventSet.__floordiv__] instead.
See examples in [`EventSet.__add__()`][temporian.EventSet.__add__] to
see how to match samplings, dtypes and index, in order to apply
arithmetic operators in different EventSets.
Example with EventSet:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0.0, 100.0, 200.0]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f2": [10.0, 20.0, 50.0]},
... same_sampling_as=a
... )
>>> c = a / b
>>> c
indexes: []
features: [('f1', float64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [0. 5. 4.]
...
```
Example casting integer features:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f2": [10, 20, 50]},
... same_sampling_as=a
... )
>>> # Cannot divide int64 features
>>> c = a / b
Traceback (most recent call last):
...
ValueError: Cannot use the divide operator on feature f1 of type int64. ...
>>> # Cast to tp.float64 or tp.float32 before
>>> c = a.cast(tp.float64) / b.cast(tp.float64)
>>> c
indexes: []
features: [('f1', float64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [0. 5. 4.]
...
```
Example with scalar value:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0., 100., 200.], "f2": [10., -10., 5.]}
... )
>>> b = a / 2
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 0. 50. 100.]
'f2': [ 5. -5. 2.5]
...
>>> c = 1000 / a
>>> c
indexes: ...
timestamps: [1. 2. 3.]
'f1': [inf 10. 5.]
'f2': [ 100. -100. 200.]
...
```
Args:
other: EventSet or scalar value.
Returns:
Result of the operation.
"""
if isinstance(other, self.__class__):
from temporian.core.operators.binary import divide
return divide(numerator=self, denominator=other)
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import divide_scalar
return divide_scalar(numerator=self, denominator=other)
self._raise_error("divide", other, "(int,float)")
assert False
def __rtruediv__(self, other: Any):
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import divide_scalar
return divide_scalar(numerator=other, denominator=self)
self._raise_error("divide", other, "(int,float)")
assert False
def __floordiv__(self: EventSetOrNode, other: Any) -> EventSetOrNode:
"""Divides `self` by an [`EventSet`][temporian.EventSet] or a scalar
value and takes the floor of the result, element-wise.
If an EventSet, each feature in `self` is divided by the feature in
`other` in the same position. `self` and `other` must have the same
sampling, index, number of features and dtype for the features in the
same positions.
If a scalar, each item in each feature in `self` is divided by `other`.
See examples in [`EventSet.__add__()`][temporian.EventSet.__add__] to
see how to match samplings, dtypes and index, in order to apply
arithmetic operators in different EventSets.
Example with EventSet:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 100, 200]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f2": [10, 3, 150]},
... same_sampling_as=a
... )
>>> c = a // b
>>> c
indexes: []
features: [('f1', int64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [ 0 33 1]
...
```
Example with scalar value:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [1, 100, 200], "f2": [10., -10., 5.]}
... )
>>> b = a // 3
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 0 33 66]
'f2': [ 3. -4. 1.]
...
>>> c = 300 // a
>>> c
indexes: ...
timestamps: [1. 2. 3.]
'f1': [300 3 1]
'f2': [ 30. -30. 60.]
...
```
Args:
other: EventSet or scalar value.
Returns:
Result of the operation.
"""
if isinstance(other, self.__class__):
from temporian.core.operators.binary import floordiv
return floordiv(numerator=self, denominator=other)
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import floordiv_scalar
return floordiv_scalar(numerator=self, denominator=other)
self._raise_error("floor_divide", other, "(int,float)")
assert False
def __rfloordiv__(self, other: Any):
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import floordiv_scalar
return floordiv_scalar(numerator=other, denominator=self)
self._raise_error("floor_divide", other, "(int,float)")
assert False
def __pow__(self: EventSetOrNode, other: Any) -> EventSetOrNode:
"""Computes power with another
[`EventSet`][temporian.EventSet] or a scalar value element-wise.
If an EventSet, each feature in `self` is raised to the feature in
`other` in the same position. `self` and `other` must have the same
sampling, index, number of features and dtype for the features in the
same positions.
If a scalar, each item in each feature in `self` is raised to
`other`.
See examples in [`EventSet.__add__()`][temporian.EventSet.__add__] to
see how to match samplings, dtypes and index, in order to apply
arithmetic operators in different EventSets.
Example with EventSet:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [5, 2, 4]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f2": [0, 3, 2]},
... same_sampling_as=a
... )
>>> c = a ** b
>>> c
indexes: []
features: [('f1', int64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [ 1 8 16]
...
```
Example with scalar value:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 2, 3], "f2": [1., 2., 3.]}
... )
>>> b = a ** 3
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 0 8 27]
'f2': [ 1. 8. 27.]
...
>>> c = 3 ** a
>>> c
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 1 9 27]
'f2': [ 3. 9. 27.]
...
```
Args:
other: EventSet or scalar value.
Returns:
Result of the operation.
"""
if isinstance(other, self.__class__):
from temporian.core.operators.binary import power
return power(base=self, exponent=other)
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import power_scalar
return power_scalar(base=self, exponent=other)
self._raise_error("exponentiate", other, "(int,float)")
assert False
def __rpow__(self, other: Any):
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import power_scalar
return power_scalar(base=other, exponent=self)
self._raise_error("exponentiate", other, "(int,float)")
assert False
def __mod__(self: EventSetOrNode, other: Any) -> EventSetOrNode:
"""Computes modulo or remainder of division with another
[`EventSet`][temporian.EventSet] or a scalar value.
If an EventSet, each feature in `self` is reduced modulo the feature in
`other` in the same position. `self` and `other` must have the same
sampling, index, number of features and dtype for the features in the
same positions.
If a scalar, each item in each feature in `self` is reduced modulo
`other`.
See examples in [`EventSet.__add__()`][temporian.EventSet.__add__] to
see how to match samplings, dtypes and index, in order to apply
arithmetic operators in different EventSets.
Example with EventSet:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [0, 7, 200]}
... )
>>> b = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f2": [10, 5, 150]},
... same_sampling_as=a
... )
>>> c = a % b
>>> c
indexes: []
features: [('f1', int64)]
events:
(3 events):
timestamps: [1. 2. 3.]
'f1': [ 0 2 50]
...
```
Example with scalar value:
```python
>>> a = tp.event_set(
... timestamps=[1, 2, 3],
... features={"f1": [1, 100, 200], "f2": [10., -10., 5.]}
... )
>>> b = a % 3
>>> b
indexes: ...
timestamps: [1. 2. 3.]
'f1': [1 1 2]
'f2': [1. 2. 2.]
...
>>> c = 300 % a
>>> c
indexes: ...
timestamps: [1. 2. 3.]
'f1': [ 0 0 100]
'f2': [ 0. -0. 0.]
...
```
Args:
other: EventSet or scalar value.
Returns:
Result of the operation.
"""
if isinstance(other, self.__class__):
from temporian.core.operators.binary import modulo
return modulo(numerator=self, denominator=other)
if isinstance(other, T_SCALAR):
from temporian.core.operators.scalar import modulo_scalar
return modulo_scalar(numerator=self, denominator=other)
self._raise_error("compute modulo (%)", other, "(int,float)")
assert False
def __rmod__(self, other: Any):
if isinstance(other, T_SCALAR):