Skip to content

Commit

Permalink
added example on how to use the day_of_week namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
kaijennissen committed Apr 19, 2024
1 parent 16af6e8 commit b18d4a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
11 changes: 1 addition & 10 deletions ibis/backends/sql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions ibis/expr/operations/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions ibis/expr/types/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,3 +988,24 @@ 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()

def iso_full_name(self):
"""Get the name of the day of the week.
Returns
-------
StringValue
The name of the day of the week
"""
return ops.IsoDayOfWeekName(self._expr).to_expr()

0 comments on commit b18d4a1

Please sign in to comment.