diff --git a/ibis/backends/sql/compiler.py b/ibis/backends/sql/compiler.py index e0d86918eec4c..157a4220156ed 100644 --- a/ibis/backends/sql/compiler.py +++ b/ibis/backends/sql/compiler.py @@ -787,7 +787,7 @@ def visit_DayOfWeekIndex(self, op, *, arg): return (self.f.dayofweek(arg) + 6) % 7 def visit_IsoDayOfWeekIndex(self, op, *, arg): - return self.f.dayofweek(arg) + return (self.f.dayofweek(arg) + 6) % 7+1 def visit_DayOfWeekName(self, op, *, arg): @@ -799,15 +799,6 @@ def visit_DayOfWeekName(self, op, *, arg): ifs=list(itertools.starmap(self.if_, enumerate(calendar.day_name))), ) - def visit_DayOfWeekName(self, op, *, arg): - # day of week number is 0-indexed - # Sunday == 0 - # Saturday == 6 - return sge.Case( - this=self.f.dayofweek(arg), - ifs=list(itertools.starmap(self.if_, enumerate(calendar.day_name))), - ) - def visit_IntervalFromInteger(self, op, *, arg, unit): return sge.Interval( this=sge.convert(arg), unit=sge.Var(this=unit.singular.upper()) diff --git a/ibis/expr/operations/temporal.py b/ibis/expr/operations/temporal.py index 315667ee416ce..2148a7e0c4773 100644 --- a/ibis/expr/operations/temporal.py +++ b/ibis/expr/operations/temporal.py @@ -164,12 +164,23 @@ class DayOfWeekIndex(Unary): dtype = dt.int16 +@public +class IsoDayOfWeekIndex(Unary): + arg: Value[dt.Date | dt.Timestamp] + + dtype = dt.int16 + @public class DayOfWeekName(Unary): arg: Value[dt.Date | dt.Timestamp] dtype = dt.string +@public +class IsoDayOfWeekName(Unary): + arg: Value[dt.Date | dt.Timestamp] + + dtype = dt.string @public diff --git a/ibis/expr/types/temporal.py b/ibis/expr/types/temporal.py index dd673b5ef6d97..c952520d4858a 100644 --- a/ibis/expr/types/temporal.py +++ b/ibis/expr/types/temporal.py @@ -41,10 +41,6 @@ def day(self) -> ir.IntegerValue: """Extract the day component.""" return ops.ExtractDay(self).to_expr() - def isoday_of_week(self) -> ir.IntegerValue: - """Extract the day of the week component in ISO-Format (1=Monday, 7=Sunday).""" - return ops.ExtractDayOfWeek(self).to_expr() - @property def day_of_week(self) -> DayOfWeek: """A namespace of methods for extracting day of week information. @@ -52,8 +48,33 @@ def day_of_week(self) -> DayOfWeek: Returns ------- DayOfWeek - An namespace expression containing methods to use to extract + An namespace expression containing methods to extract information. + + Examples + -------- + >>> import ibis + >>> import datetime as dt + >>> ibis.options.interactive = True + >>> t = ibis.memtable({"date" : [dt.datetime(2024,4,x) for x in [14,15,16,17,18,19,20]]}, name="t") + >>> t.mutate( + ... day_of_week=_.date.day_of_week.index(), + ... day_of_week_name=_.date.day_of_week.full_name(), + ... iso_day_of_week=_.date.day_of_week.iso_index() + ... ) + ┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓ + ┃ date ┃ day_of_week ┃ day_of_week_name ┃ iso_day_of_week ┃ + ┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩ + │ timestamp │ int16 │ string │ int16 │ + ├─────────────────────┼─────────────┼──────────────────┼─────────────────┤ + │ 2024-04-14 00:00:00 │ 6 │ Sunday │ 7 │ + │ 2024-04-15 00:00:00 │ 0 │ Monday │ 1 │ + │ 2024-04-16 00:00:00 │ 1 │ Tuesday │ 2 │ + │ 2024-04-17 00:00:00 │ 2 │ Wednesday │ 3 │ + │ 2024-04-18 00:00:00 │ 3 │ Thursday │ 4 │ + │ 2024-04-19 00:00:00 │ 4 │ Friday │ 5 │ + │ 2024-04-20 00:00:00 │ 5 │ Saturday │ 6 │ + └─────────────────────┴─────────────┴──────────────────┴─────────────────┘ """ return DayOfWeek(self) @@ -988,3 +1009,15 @@ def full_name(self): The name of the day of the week """ return ops.DayOfWeekName(self._expr).to_expr() + + + def iso_index(self): + """Get the index of the day of the week in iso-format (1=Monday, 7=Sunday). + + Returns + ------- + IntegerValue + The index of the day of the week in iso-format (1=Monday, 7=Sunday). + """ + return ops.IsoDayOfWeekIndex(self._expr).to_expr() + \ No newline at end of file