Skip to content

Commit

Permalink
docs(examples): add temporal examples
Browse files Browse the repository at this point in the history
  • Loading branch information
IndexSeek committed Oct 24, 2024
1 parent af24c8e commit 5537a67
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions ibis/expr/types/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,42 @@ def truncate(self, unit: Literal["Y", "Q", "M", "W", "D"]) -> DateValue:
-------
DateValue
Truncated date value expression
Examples
--------
>>> from datetime import date
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
... {
... "date_col": [
... date(2020, 1, 5),
... date(2020, 4, 10),
... date(2020, 7, 15),
... date(2020, 10, 20),
... ]
... },
... )
Return date columns truncated to the start of the year, quarter, month, and
week.
>>> t.select(
... truncated_year=t.date_col.truncate("Y"),
... truncated_quarter=t.date_col.truncate("Q"),
... truncated_month=t.date_col.truncate("M"),
... truncated_week=t.date_col.truncate("W"),
... )
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓
┃ truncated_year ┃ truncated_quarter ┃ truncated_month ┃ truncated_week ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩
│ date │ date │ date │ date │
├────────────────┼───────────────────┼─────────────────┼────────────────┤
│ 2020-01-01 │ 2020-01-01 │ 2020-01-01 │ 2019-12-30 │
│ 2020-01-01 │ 2020-04-01 │ 2020-04-01 │ 2020-04-06 │
│ 2020-01-01 │ 2020-07-01 │ 2020-07-01 │ 2020-07-13 │
│ 2020-01-01 │ 2020-10-01 │ 2020-10-01 │ 2020-10-19 │
└────────────────┴───────────────────┴─────────────────┴────────────────┘
"""
return ops.DateTruncate(self, unit).to_expr()

Expand Down Expand Up @@ -651,6 +687,62 @@ def truncate(
-------
TimestampValue
Truncated timestamp expression
Examples
--------
>>> from datetime import datetime
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable(
... {
... "timestamp_col": [
... datetime(2020, 1, 5, 8, 0, 0),
... datetime(2020, 4, 10, 10, 2, 15),
... datetime(2020, 7, 15, 12, 4, 30),
... datetime(2020, 10, 20, 14, 6, 45),
... ]
... },
... )
Return timestamp columns truncated to the start of the year, quarter, month,
and week.
>>> t.select(
... truncated_year=t.timestamp_col.truncate("Y"),
... truncated_quarter=t.timestamp_col.truncate("Q"),
... truncated_month=t.timestamp_col.truncate("M"),
... truncated_week=t.timestamp_col.truncate("W"),
... )
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓
┃ truncated_year ┃ truncated_quarter ┃ truncated_month ┃ truncated_week ┃
┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━┩
│ timestamp │ timestamp │ timestamp │ timestamp │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────────────┤
│ 2020-01-01 00:00:00 │ 2020-01-01 00:00:00 │ 2020-01-01 00:00:00 │ 2019-12-30 00:00:00 │
│ 2020-01-01 00:00:00 │ 2020-04-01 00:00:00 │ 2020-04-01 00:00:00 │ 2020-04-06 00:00:00 │
│ 2020-01-01 00:00:00 │ 2020-07-01 00:00:00 │ 2020-07-01 00:00:00 │ 2020-07-13 00:00:00 │
│ 2020-01-01 00:00:00 │ 2020-10-01 00:00:00 │ 2020-10-01 00:00:00 │ 2020-10-19 00:00:00 │
└─────────────────────┴─────────────────────┴─────────────────────┴─────────────────────┘
Return timestamp columns truncated to the start of the day, hour, minute, and
second.
>>> t.select(
... truncated_day=t.timestamp_col.truncate("D"),
... truncated_hour=t.timestamp_col.truncate("h"),
... truncated_minute=t.timestamp_col.truncate("m"),
... truncated_second=t.timestamp_col.truncate("s"),
... )
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓
┃ truncated_day ┃ truncated_hour ┃ truncated_minute ┃ truncated_second ┃
┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━┩
│ timestamp │ timestamp │ timestamp │ timestamp │
├─────────────────────┼─────────────────────┼─────────────────────┼─────────────────────┤
│ 2020-01-05 00:00:00 │ 2020-01-05 08:00:00 │ 2020-01-05 08:00:00 │ 2020-01-05 08:00:00 │
│ 2020-04-10 00:00:00 │ 2020-04-10 10:00:00 │ 2020-04-10 10:02:00 │ 2020-04-10 10:02:15 │
│ 2020-07-15 00:00:00 │ 2020-07-15 12:00:00 │ 2020-07-15 12:04:00 │ 2020-07-15 12:04:30 │
│ 2020-10-20 00:00:00 │ 2020-10-20 14:00:00 │ 2020-10-20 14:06:00 │ 2020-10-20 14:06:45 │
└─────────────────────┴─────────────────────┴─────────────────────┴─────────────────────┘
"""
return ops.TimestampTruncate(self, unit).to_expr()

Expand Down

0 comments on commit 5537a67

Please sign in to comment.