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 b1672f8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 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
43 changes: 38 additions & 5 deletions ibis/expr/types/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,40 @@ 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.
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)

Expand Down Expand Up @@ -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()

0 comments on commit b1672f8

Please sign in to comment.