Skip to content

Commit

Permalink
Merge branch 'pola-rs:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ion-elgreco authored Jul 17, 2023
2 parents e82a692 + 1a4eaa5 commit 4b3b86a
Showing 1 changed file with 84 additions and 69 deletions.
153 changes: 84 additions & 69 deletions py-polars/polars/expr/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,42 +728,47 @@ def weekday(self) -> Expr:
-------
Week day as UInt32
See Also
--------
day
ordinal_day
Examples
--------
>>> from datetime import timedelta, datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2001, 1, 9)
>>> start = datetime(2001, 12, 22)
>>> stop = datetime(2001, 12, 25)
>>> df = pl.DataFrame(
... {"date": pl.date_range(start, stop, timedelta(days=3), eager=True)}
... {"date": pl.date_range(start, stop, timedelta(days=1), eager=True)}
... )
>>> df
shape: (3, 1)
shape: (4, 1)
┌─────────────────────┐
│ date │
│ --- │
│ datetime[μs] │
╞═════════════════════╡
│ 2001-01-01 00:00:00 │
│ 2001-01-04 00:00:00 │
│ 2001-01-07 00:00:00 │
│ 2001-12-22 00:00:00 │
│ 2001-12-23 00:00:00 │
│ 2001-12-24 00:00:00 │
│ 2001-12-25 00:00:00 │
└─────────────────────┘
>>> df.select(
... [
... pl.col("date").dt.weekday().alias("weekday"),
... pl.col("date").dt.day().alias("day_of_month"),
... pl.col("date").dt.ordinal_day().alias("day_of_year"),
... ]
>>> df.with_columns(
... pl.col("date").dt.weekday().alias("weekday"),
... pl.col("date").dt.day().alias("day_of_month"),
... pl.col("date").dt.ordinal_day().alias("day_of_year"),
... )
shape: (3, 3)
┌─────────┬──────────────┬─────────────┐
│ weekday ┆ day_of_month ┆ day_of_year │
│ --- ┆ --- ┆ --- │
│ u32 ┆ u32 ┆ u32 │
╞═════════╪══════════════╪═════════════╡
│ 1 ┆ 1 ┆ 1 │
│ 4 ┆ 4 ┆ 4 │
│ 7 ┆ 7 ┆ 7 │
└─────────┴──────────────┴─────────────┘
shape: (4, 4)
┌─────────────────────┬─────────┬──────────────┬─────────────┐
│ date ┆ weekday ┆ day_of_month ┆ day_of_year │
│ --- ┆ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ u32 ┆ u32 ┆ u32 │
╞═════════════════════╪═════════╪══════════════╪═════════════╡
│ 2001-12-22 00:00:00 ┆ 6 ┆ 22 ┆ 356 │
│ 2001-12-23 00:00:00 ┆ 7 ┆ 23 ┆ 357 │
│ 2001-12-24 00:00:00 ┆ 1 ┆ 24 ┆ 358 │
│ 2001-12-25 00:00:00 ┆ 2 ┆ 25 ┆ 359 │
└─────────────────────┴─────────┴──────────────┴─────────────┘
"""
return wrap_expr(self._pyexpr.dt_weekday())
Expand All @@ -781,42 +786,47 @@ def day(self) -> Expr:
-------
Day as UInt32
See Also
--------
weekday
ordinal_day
Examples
--------
>>> from datetime import timedelta, datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2001, 1, 9)
>>> start = datetime(2001, 12, 22)
>>> stop = datetime(2001, 12, 25)
>>> df = pl.DataFrame(
... {"date": pl.date_range(start, stop, timedelta(days=3), eager=True)}
... {"date": pl.date_range(start, stop, timedelta(days=1), eager=True)}
... )
>>> df
shape: (3, 1)
shape: (4, 1)
┌─────────────────────┐
│ date │
│ --- │
│ datetime[μs] │
╞═════════════════════╡
│ 2001-01-01 00:00:00 │
│ 2001-01-04 00:00:00 │
│ 2001-01-07 00:00:00 │
│ 2001-12-22 00:00:00 │
│ 2001-12-23 00:00:00 │
│ 2001-12-24 00:00:00 │
│ 2001-12-25 00:00:00 │
└─────────────────────┘
>>> df.select(
... [
... pl.col("date").dt.weekday().alias("weekday"),
... pl.col("date").dt.day().alias("day_of_month"),
... pl.col("date").dt.ordinal_day().alias("day_of_year"),
... ]
>>> df.with_columns(
... pl.col("date").dt.weekday().alias("weekday"),
... pl.col("date").dt.day().alias("day_of_month"),
... pl.col("date").dt.ordinal_day().alias("day_of_year"),
... )
shape: (3, 3)
┌─────────┬──────────────┬─────────────┐
│ weekday ┆ day_of_month ┆ day_of_year │
│ --- ┆ --- ┆ --- │
│ u32 ┆ u32 ┆ u32 │
╞═════════╪══════════════╪═════════════╡
│ 1 ┆ 1 ┆ 1 │
│ 4 ┆ 4 ┆ 4 │
│ 7 ┆ 7 ┆ 7 │
└─────────┴──────────────┴─────────────┘
shape: (4, 4)
┌─────────────────────┬─────────┬──────────────┬─────────────┐
│ date ┆ weekday ┆ day_of_month ┆ day_of_year │
│ --- ┆ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ u32 ┆ u32 ┆ u32 │
╞═════════════════════╪═════════╪══════════════╪═════════════╡
│ 2001-12-22 00:00:00 ┆ 6 ┆ 22 ┆ 356 │
│ 2001-12-23 00:00:00 ┆ 7 ┆ 23 ┆ 357 │
│ 2001-12-24 00:00:00 ┆ 1 ┆ 24 ┆ 358 │
│ 2001-12-25 00:00:00 ┆ 2 ┆ 25 ┆ 359 │
└─────────────────────┴─────────┴──────────────┴─────────────┘
"""
return wrap_expr(self._pyexpr.dt_day())
Expand All @@ -834,42 +844,47 @@ def ordinal_day(self) -> Expr:
-------
Day as UInt32
See Also
--------
weekday
day
Examples
--------
>>> from datetime import timedelta, datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2001, 1, 9)
>>> start = datetime(2001, 12, 22)
>>> stop = datetime(2001, 12, 25)
>>> df = pl.DataFrame(
... {"date": pl.date_range(start, stop, timedelta(days=3), eager=True)}
... {"date": pl.date_range(start, stop, timedelta(days=1), eager=True)}
... )
>>> df
shape: (3, 1)
shape: (4, 1)
┌─────────────────────┐
│ date │
│ --- │
│ datetime[μs] │
╞═════════════════════╡
│ 2001-01-01 00:00:00 │
│ 2001-01-04 00:00:00 │
│ 2001-01-07 00:00:00 │
│ 2001-12-22 00:00:00 │
│ 2001-12-23 00:00:00 │
│ 2001-12-24 00:00:00 │
│ 2001-12-25 00:00:00 │
└─────────────────────┘
>>> df.select(
... [
... pl.col("date").dt.weekday().alias("weekday"),
... pl.col("date").dt.day().alias("day_of_month"),
... pl.col("date").dt.ordinal_day().alias("day_of_year"),
... ]
>>> df.with_columns(
... pl.col("date").dt.weekday().alias("weekday"),
... pl.col("date").dt.day().alias("day_of_month"),
... pl.col("date").dt.ordinal_day().alias("day_of_year"),
... )
shape: (3, 3)
┌─────────┬──────────────┬─────────────┐
│ weekday ┆ day_of_month ┆ day_of_year │
│ --- ┆ --- ┆ --- │
│ u32 ┆ u32 ┆ u32 │
╞═════════╪══════════════╪═════════════╡
│ 1 ┆ 1 ┆ 1 │
│ 4 ┆ 4 ┆ 4 │
│ 7 ┆ 7 ┆ 7 │
└─────────┴──────────────┴─────────────┘
shape: (4, 4)
┌─────────────────────┬─────────┬──────────────┬─────────────┐
│ date ┆ weekday ┆ day_of_month ┆ day_of_year │
│ --- ┆ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ u32 ┆ u32 ┆ u32 │
╞═════════════════════╪═════════╪══════════════╪═════════════╡
│ 2001-12-22 00:00:00 ┆ 6 ┆ 22 ┆ 356 │
│ 2001-12-23 00:00:00 ┆ 7 ┆ 23 ┆ 357 │
│ 2001-12-24 00:00:00 ┆ 1 ┆ 24 ┆ 358 │
│ 2001-12-25 00:00:00 ┆ 2 ┆ 25 ┆ 359 │
└─────────────────────┴─────────┴──────────────┴─────────────┘
"""
return wrap_expr(self._pyexpr.dt_ordinal_day())
Expand Down

0 comments on commit 4b3b86a

Please sign in to comment.