Skip to content

Commit 72c14d7

Browse files
committed
refactor(api)!: standardize TemporalValue APIs
1 parent 5b29be3 commit 72c14d7

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

ibis/expr/types/temporal.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def between(
267267
self,
268268
lower: str | datetime.time | TimeValue,
269269
upper: str | datetime.time | TimeValue,
270+
*,
270271
timezone: str | None = None,
271272
) -> ir.BooleanValue:
272273
"""Check if the expr falls between `lower` and `upper`, inclusive.
@@ -326,7 +327,7 @@ def between(
326327

327328
@public
328329
class TimeValue(_TimeComponentMixin, Value):
329-
def strftime(self, format_str: str) -> ir.StringValue:
330+
def strftime(self, format_str: str, /) -> ir.StringValue:
330331
"""Format a time according to `format_str`.
331332
332333
Format string may depend on the backend, but we try to conform to ANSI
@@ -344,7 +345,7 @@ def strftime(self, format_str: str) -> ir.StringValue:
344345
"""
345346
return ops.Strftime(self, format_str).to_expr()
346347

347-
def truncate(self, unit: Literal["h", "m", "s", "ms", "us", "ns"]) -> TimeValue:
348+
def truncate(self, unit: Literal["h", "m", "s", "ms", "us", "ns"], /) -> TimeValue:
348349
"""Truncate the expression to a time expression in units of `unit`.
349350
350351
Commonly used for time series resampling.
@@ -421,7 +422,9 @@ def __rsub__(self, other: ops.Value[dt.Interval | dt.Time, ds.Any]):
421422
def delta(
422423
self,
423424
other: datetime.time | Value[dt.Time],
424-
part: Literal[
425+
/,
426+
*,
427+
unit: Literal[
425428
"hour", "minute", "second", "millisecond", "microsecond", "nanosecond"
426429
]
427430
| Value[dt.String],
@@ -438,7 +441,7 @@ def delta(
438441
----------
439442
other
440443
A time expression
441-
part
444+
unit
442445
The unit of time to compute the difference in
443446
444447
Returns
@@ -452,7 +455,7 @@ def delta(
452455
>>> ibis.options.interactive = True
453456
>>> start = ibis.time("01:58:00")
454457
>>> end = ibis.time("23:59:59")
455-
>>> end.delta(start, "hour")
458+
>>> end.delta(start, unit="hour")
456459
┌────┐
457460
│ 22 │
458461
└────┘
@@ -467,7 +470,7 @@ def delta(
467470
>>> taxi = ibis.read_csv("/tmp/triptimes.csv")
468471
>>> ride_duration = (
469472
... taxi.tpep_dropoff_datetime.time()
470-
... .delta(taxi.tpep_pickup_datetime.time(), "minute")
473+
... .delta(taxi.tpep_pickup_datetime.time(), unit="minute")
471474
... .name("ride_minutes")
472475
... )
473476
>>> ride_duration
@@ -483,7 +486,7 @@ def delta(
483486
│ 5 │
484487
└──────────────┘
485488
"""
486-
return ops.TimeDelta(left=self, right=other, part=part).to_expr()
489+
return ops.TimeDelta(left=self, right=other, part=unit).to_expr()
487490

488491

489492
@public
@@ -498,7 +501,7 @@ class TimeColumn(Column, TimeValue):
498501

499502
@public
500503
class DateValue(Value, _DateComponentMixin):
501-
def strftime(self, format_str: str) -> ir.StringValue:
504+
def strftime(self, format_str: str, /) -> ir.StringValue:
502505
"""Format a date according to `format_str`.
503506
504507
Format string may depend on the backend, but we try to conform to ANSI
@@ -557,7 +560,7 @@ def strftime(self, format_str: str) -> ir.StringValue:
557560
"""
558561
return ops.Strftime(self, format_str).to_expr()
559562

560-
def truncate(self, unit: Literal["Y", "Q", "M", "W", "D"]) -> DateValue:
563+
def truncate(self, unit: Literal["Y", "Q", "M", "W", "D"], /) -> DateValue:
561564
"""Truncate date expression to units of `unit`.
562565
563566
Parameters
@@ -668,7 +671,9 @@ def __rsub__(self, other: ops.Value[dt.Date | dt.Interval, ds.Any]):
668671
def delta(
669672
self,
670673
other: datetime.date | Value[dt.Date],
671-
part: Literal["year", "quarter", "month", "week", "day"] | Value[dt.String],
674+
/,
675+
*,
676+
unit: Literal["year", "quarter", "month", "week", "day"] | Value[dt.String],
672677
) -> ir.IntegerValue:
673678
"""Compute the number of `part`s between two dates.
674679
@@ -682,7 +687,7 @@ def delta(
682687
----------
683688
other
684689
A date expression
685-
part
690+
unit
686691
The unit of time to compute the difference in
687692
688693
Returns
@@ -696,14 +701,14 @@ def delta(
696701
>>> ibis.options.interactive = True
697702
>>> start = ibis.date("1992-09-30")
698703
>>> end = ibis.date("1992-10-01")
699-
>>> end.delta(start, "day")
704+
>>> end.delta(start, unit="day")
700705
┌───┐
701706
│ 1 │
702707
└───┘
703708
>>> prez = ibis.examples.presidential.fetch()
704709
>>> prez.mutate(
705-
... years_in_office=prez.end.delta(prez.start, "year"),
706-
... hours_in_office=prez.end.delta(prez.start, "hour"),
710+
... years_in_office=prez.end.delta(prez.start, unit="year"),
711+
... hours_in_office=prez.end.delta(prez.start, unit="hour"),
707712
... ).drop("party")
708713
┏━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
709714
┃ name ┃ start ┃ end ┃ years_in_office ┃ hours_in_office ┃
@@ -723,7 +728,7 @@ def delta(
723728
│ … │ … │ … │ … │ … │
724729
└────────────┴────────────┴────────────┴─────────────────┴─────────────────┘
725730
"""
726-
return ops.DateDelta(left=self, right=other, part=part).to_expr()
731+
return ops.DateDelta(left=self, right=other, part=unit).to_expr()
727732

728733
def epoch_days(self) -> ir.IntegerValue:
729734
"""Return the number of days since the UNIX epoch date.
@@ -759,7 +764,7 @@ def epoch_days(self) -> ir.IntegerValue:
759764
│ 2020-01-01 │ 18262 │
760765
└────────────┴───────┘
761766
"""
762-
return self.delta(ibis.date(1970, 1, 1), "day")
767+
return self.delta(ibis.date(1970, 1, 1), unit="day")
763768

764769

765770
@public
@@ -774,7 +779,7 @@ class DateColumn(Column, DateValue):
774779

775780
@public
776781
class TimestampValue(_DateComponentMixin, _TimeComponentMixin, Value):
777-
def strftime(self, format_str: str) -> ir.StringValue:
782+
def strftime(self, format_str: str, /) -> ir.StringValue:
778783
"""Format a timestamp according to `format_str`.
779784
780785
Format string may depend on the backend, but we try to conform to ANSI
@@ -847,8 +852,7 @@ def strftime(self, format_str: str) -> ir.StringValue:
847852
return ops.Strftime(self, format_str).to_expr()
848853

849854
def truncate(
850-
self,
851-
unit: Literal["Y", "Q", "M", "W", "D", "h", "m", "s", "ms", "us", "ns"],
855+
self, unit: Literal["Y", "Q", "M", "W", "D", "h", "m", "s", "ms", "us", "ns"], /
852856
) -> TimestampValue:
853857
"""Truncate timestamp expression to units of `unit`.
854858
@@ -935,6 +939,7 @@ def truncate(
935939
def bucket(
936940
self,
937941
interval: Any = None,
942+
/,
938943
*,
939944
years: int | None = None,
940945
quarters: int | None = None,
@@ -1146,7 +1151,9 @@ def __rsub__(self, other: ops.Value[dt.Timestamp | dt.Interval, ds.Any]):
11461151
def delta(
11471152
self,
11481153
other: datetime.datetime | Value[dt.Timestamp],
1149-
part: Literal[
1154+
/,
1155+
*,
1156+
unit: Literal[
11501157
"year",
11511158
"quarter",
11521159
"month",
@@ -1173,7 +1180,7 @@ def delta(
11731180
----------
11741181
other
11751182
A timestamp expression
1176-
part
1183+
unit
11771184
The unit of time to compute the difference in
11781185
11791186
Returns
@@ -1187,7 +1194,7 @@ def delta(
11871194
>>> ibis.options.interactive = True
11881195
>>> start = ibis.time("01:58:00")
11891196
>>> end = ibis.time("23:59:59")
1190-
>>> end.delta(start, "hour")
1197+
>>> end.delta(start, unit="hour")
11911198
┌────┐
11921199
│ 22 │
11931200
└────┘
@@ -1201,7 +1208,7 @@ def delta(
12011208
... nbytes = f.write(data) # nbytes is unused
12021209
>>> taxi = ibis.read_csv("/tmp/triptimes.csv")
12031210
>>> ride_duration = taxi.tpep_dropoff_datetime.delta(
1204-
... taxi.tpep_pickup_datetime, "minute"
1211+
... taxi.tpep_pickup_datetime, unit="minute"
12051212
... ).name("ride_minutes")
12061213
>>> ride_duration
12071214
┏━━━━━━━━━━━━━━┓
@@ -1216,7 +1223,7 @@ def delta(
12161223
│ 5 │
12171224
└──────────────┘
12181225
"""
1219-
return ops.TimestampDelta(left=self, right=other, part=part).to_expr()
1226+
return ops.TimestampDelta(left=self, right=other, part=unit).to_expr()
12201227

12211228

12221229
@public
@@ -1231,7 +1238,7 @@ class TimestampColumn(Column, TimestampValue):
12311238

12321239
@public
12331240
class IntervalValue(Value):
1234-
def as_unit(self, target_unit: str) -> IntervalValue:
1241+
def as_unit(self, target_unit: str, /) -> IntervalValue:
12351242
"""Convert this interval to units of `target_unit`."""
12361243
# TODO(kszucs): should use a separate operation for unit conversion
12371244
# which we can rewrite/simplify to integer multiplication/division
@@ -1251,8 +1258,8 @@ def as_unit(self, target_unit: str) -> IntervalValue:
12511258
return value.as_interval(target_unit)
12521259

12531260
@deprecated(as_of="10.0", instead="use as_unit() instead")
1254-
def to_unit(self, target_unit: str) -> IntervalValue:
1255-
return self.as_unit(target_unit=target_unit)
1261+
def to_unit(self, target_unit: str, /) -> IntervalValue:
1262+
return self.as_unit(target_unit)
12561263

12571264
@property
12581265
def years(self) -> ir.IntegerValue:

0 commit comments

Comments
 (0)