This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtemporal.py
More file actions
475 lines (301 loc) · 9.47 KB
/
temporal.py
File metadata and controls
475 lines (301 loc) · 9.47 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
# Contains code from https://github.com/ibis-project/ibis/blob/9.2.0/ibis/expr/operations/temporal.py
"""Temporal operations."""
from __future__ import annotations
import operator
from typing import Annotated, Optional
from bigframes_vendored.ibis.common.annotations import attribute
from bigframes_vendored.ibis.common.patterns import As, Attrs
from bigframes_vendored.ibis.common.temporal import (
DateUnit,
IntervalUnit,
TimestampUnit,
TimeUnit,
)
import bigframes_vendored.ibis.expr.datatypes as dt
from bigframes_vendored.ibis.expr.operations.core import Binary, Scalar, Unary, Value
from bigframes_vendored.ibis.expr.operations.logical import Between
import bigframes_vendored.ibis.expr.rules as rlz
from public import public
@public
class TimestampTruncate(Value):
"""Truncate a timestamp to a specified unit."""
arg: Value[dt.Timestamp]
unit: IntervalUnit
shape = rlz.shape_like("arg")
dtype = dt.timestamp
@public
class DateTruncate(Value):
"""Truncate a date to a specified unit."""
arg: Value[dt.Date]
unit: DateUnit
shape = rlz.shape_like("arg")
dtype = dt.date
@public
class TimeTruncate(Value):
"""Truncate a time to a specified unit."""
arg: Value[dt.Time]
unit: TimeUnit
shape = rlz.shape_like("arg")
dtype = dt.time
@public
class TimestampBucket(Value):
"""Bucketize a timestamp to a specified interval."""
arg: Value[dt.Timestamp]
interval: Scalar[dt.Interval]
offset: Optional[Scalar[dt.Interval]] = None
shape = rlz.shape_like("arg")
dtype = dt.timestamp
@public
class Strftime(Value):
"""Format a temporal value as a string."""
arg: Value[dt.Temporal]
format_str: Value[dt.String]
shape = rlz.shape_like("args")
dtype = dt.string
@public
class StringToTimestamp(Value):
"""Convert a string to a timestamp."""
arg: Value[dt.String]
format_str: Value[dt.String]
shape = rlz.shape_like("args")
dtype = dt.Timestamp(timezone="UTC")
@public
class StringToDate(Value):
"""Convert a string to a date."""
arg: Value[dt.String]
format_str: Value[dt.String]
shape = rlz.shape_like("args")
dtype = dt.date
@public
class ExtractTemporalField(Unary):
"""Extract a field from a temporal value."""
arg: Value[dt.Temporal]
dtype = dt.int64
@public
class ExtractDateField(ExtractTemporalField):
"""Extract a field from a date."""
arg: Value[dt.Date | dt.Timestamp]
@public
class ExtractTimeField(ExtractTemporalField):
"""Extract a field from a time."""
arg: Value[dt.Time | dt.Timestamp]
@public
class ExtractYear(ExtractDateField):
"""Extract the year from a date or timestamp."""
@public
class ExtractIsoYear(ExtractDateField):
"""Extract the ISO year from a date or timestamp."""
@public
class ExtractMonth(ExtractDateField):
"""Extract the month from a date or timestamp."""
@public
class ExtractDay(ExtractDateField):
"""Extract the day from a date or timestamp."""
@public
class ExtractDayOfYear(ExtractDateField):
"""Extract the day of the year from a date or timestamp."""
@public
class ExtractQuarter(ExtractDateField):
"""Extract the quarter from a date or timestamp."""
@public
class ExtractEpochSeconds(ExtractDateField):
"""Extract seconds since the UNIX epoch from a date or timestamp."""
@public
class ExtractWeekOfYear(ExtractDateField):
"""Extract the week of the year from a date or timestamp."""
@public
class ExtractHour(ExtractTimeField):
"""Extract the hour from a time or timestamp."""
@public
class ExtractMinute(ExtractTimeField):
"""Extract the minute from a time or timestamp."""
@public
class ExtractSecond(ExtractTimeField):
"""Extract the second from a time or timestamp."""
@public
class ExtractMillisecond(ExtractTimeField):
"""Extract milliseconds from a time or timestamp."""
@public
class ExtractMicrosecond(ExtractTimeField):
"""Extract microseconds from a time or timestamp."""
@public
class DayOfWeekIndex(Unary):
"""Extract the index of the day of the week from a date or timestamp."""
arg: Value[dt.Date | dt.Timestamp]
dtype = dt.int16
@public
class DayOfWeekName(Unary):
"""Extract the name of the day of the week from a date or timestamp."""
arg: Value[dt.Date | dt.Timestamp]
dtype = dt.string
@public
class Time(Unary):
"""Extract the time from a timestamp."""
dtype = dt.time
@public
class Date(Unary):
"""Extract the date from a timestamp."""
dtype = dt.date
@public
class DateFromYMD(Value):
"""Construct a date from year, month, and day."""
year: Value[dt.Integer]
month: Value[dt.Integer]
day: Value[dt.Integer]
dtype = dt.date
shape = rlz.shape_like("args")
@public
class TimeFromHMS(Value):
"""Construct a time from hours, minutes, and seconds."""
hours: Value[dt.Integer]
minutes: Value[dt.Integer]
seconds: Value[dt.Integer]
dtype = dt.time
shape = rlz.shape_like("args")
@public
class TimestampFromYMDHMS(Value):
"""Construct a timestamp from components."""
year: Value[dt.Integer]
month: Value[dt.Integer]
day: Value[dt.Integer]
hours: Value[dt.Integer]
minutes: Value[dt.Integer]
seconds: Value[dt.Integer]
dtype = dt.timestamp
shape = rlz.shape_like("args")
@public
class TimestampFromUNIX(Value):
"""Construct a timestamp from a UNIX timestamp."""
arg: Value
unit: TimestampUnit
dtype = dt.timestamp
shape = rlz.shape_like("arg")
TimeInterval = Annotated[dt.Interval, Attrs(unit=As(TimeUnit))]
DateInterval = Annotated[dt.Interval, Attrs(unit=As(DateUnit))]
@public
class DateAdd(Binary):
"""Add an interval to a date."""
left: Value[dt.Date]
right: Value[DateInterval]
dtype = rlz.dtype_like("left")
@public
class DateSub(Binary):
"""Subtract an interval from a date."""
left: Value[dt.Date]
right: Value[DateInterval]
dtype = rlz.dtype_like("left")
@public
class DateDiff(Binary):
"""Compute the difference between two dates."""
left: Value[dt.Date]
right: Value[dt.Date]
dtype = dt.Interval("D")
@public
class TimeAdd(Binary):
"""Add an interval to a time."""
left: Value[dt.Time]
right: Value[TimeInterval]
dtype = rlz.dtype_like("left")
@public
class TimeSub(Binary):
"""Subtract an interval from a time."""
left: Value[dt.Time]
right: Value[TimeInterval]
dtype = rlz.dtype_like("left")
@public
class TimeDiff(Binary):
"""Compute the difference between two times."""
left: Value[dt.Time]
right: Value[dt.Time]
dtype = dt.Interval("s")
@public
class TimestampAdd(Binary):
"""Add an interval to a timestamp."""
left: Value[dt.Timestamp]
right: Value[dt.Interval]
dtype = rlz.dtype_like("left")
@public
class TimestampSub(Binary):
"""Subtract an interval from a timestamp."""
left: Value[dt.Timestamp]
right: Value[dt.Interval]
dtype = rlz.dtype_like("left")
@public
class TimestampDiff(Binary):
"""Compute the difference between two timestamps."""
left: Value[dt.Timestamp]
right: Value[dt.Timestamp]
dtype = dt.Interval("s")
@public
class IntervalBinary(Binary):
"""Base class for interval binary operations."""
@attribute
def dtype(self):
interval_unit_args = [
arg.dtype.unit for arg in (self.left, self.right) if arg.dtype.is_interval()
]
unit = rlz._promote_interval_resolution(interval_unit_args)
return self.left.dtype.copy(unit=unit)
@public
class IntervalAdd(IntervalBinary):
"""Add two intervals."""
left: Value[dt.Interval]
right: Value[dt.Interval]
op = operator.add
@public
class IntervalSubtract(IntervalBinary):
"""Subtract one interval from another."""
left: Value[dt.Interval]
right: Value[dt.Interval]
op = operator.sub
@public
class IntervalMultiply(IntervalBinary):
"""Multiply an interval by a scalar."""
left: Value[dt.Interval]
right: Value[dt.Numeric | dt.Boolean]
op = operator.mul
@public
class IntervalFloorDivide(IntervalBinary):
"""Divide an interval by a scalar, rounding down."""
left: Value[dt.Interval]
right: Value[dt.Numeric | dt.Boolean]
op = operator.floordiv
@public
class IntervalFromInteger(Value):
"""Construct an interval from an integer."""
arg: Value[dt.Integer]
unit: IntervalUnit
shape = rlz.shape_like("arg")
@attribute
def dtype(self):
return dt.Interval(self.unit)
@property
def resolution(self):
return self.dtype.resolution
@public
class BetweenTime(Between):
"""Check if a time is between two bounds."""
arg: Value[dt.Time | dt.Timestamp]
lower_bound: Value[dt.Time | dt.String]
upper_bound: Value[dt.Time | dt.String]
class TemporalDelta(Value):
"""Base class for temporal delta operations."""
part: Value[dt.String]
shape = rlz.shape_like("args")
dtype = dt.int64
@public
class TimeDelta(TemporalDelta):
"""Compute the difference between two times as integer number of requested units."""
left: Value[dt.Time]
right: Value[dt.Time]
@public
class DateDelta(TemporalDelta):
"""Compute the difference between two dates as integer number of requested units."""
left: Value[dt.Date]
right: Value[dt.Date]
@public
class TimestampDelta(TemporalDelta):
"""Compute the difference between two timestamps as integer number of requested units."""
left: Value[dt.Timestamp]
right: Value[dt.Timestamp]
public(ExtractTimestampField=ExtractTemporalField)