Skip to content

Commit

Permalink
refactor(rust, python)!: iso weekday (pola-rs#5598)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored and zundertj committed Jan 7, 2023
1 parent f2019b4 commit 3e282a6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions polars/polars-time/src/chunkedarray/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ pub trait DatetimeMethods: AsDatetime {
cast_and_apply(self.as_datetime(), temporal::month)
}

/// Extract weekday from underlying NaiveDateTime representation.
/// Returns the weekday number where monday = 0 and sunday = 6
/// Extract ISO weekday from underlying NaiveDateTime representation.
/// Returns the weekday number where monday = 1 and sunday = 7
fn weekday(&self) -> UInt32Chunked {
let ca = self.as_datetime();
let f = match ca.time_unit() {
Expand Down
4 changes: 2 additions & 2 deletions polars/polars-time/src/chunkedarray/kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait PolarsIso {

impl PolarsIso for NaiveDateTime {
fn p_weekday(&self) -> u32 {
self.weekday() as u32
self.weekday() as u32 + 1
}
fn week(&self) -> u32 {
self.iso_week().week()
Expand All @@ -32,7 +32,7 @@ impl PolarsIso for NaiveDateTime {

impl PolarsIso for NaiveDate {
fn p_weekday(&self) -> u32 {
self.weekday() as u32
self.weekday() as u32 + 1
}
fn week(&self) -> u32 {
self.iso_week().week()
Expand Down
20 changes: 10 additions & 10 deletions py-polars/polars/internals/expr/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def weekday(self) -> pli.Expr:
Applies to Date and Datetime columns.
Returns the weekday number where monday = 0 and sunday = 6
Returns the ISO weekday number where monday = 1 and sunday = 7
Returns
-------
Expand Down Expand Up @@ -555,11 +555,11 @@ def weekday(self) -> pli.Expr:
│ --- ┆ --- ┆ --- │
│ u32 ┆ u32 ┆ u32 │
╞═════════╪══════════════╪═════════════╡
0 ┆ 1 ┆ 1 │
1 ┆ 1 ┆ 1 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
3 ┆ 4 ┆ 4 │
4 ┆ 4 ┆ 4 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
6 ┆ 7 ┆ 7 │
7 ┆ 7 ┆ 7 │
└─────────┴──────────────┴─────────────┘
"""
Expand Down Expand Up @@ -610,11 +610,11 @@ def day(self) -> pli.Expr:
│ --- ┆ --- ┆ --- │
│ u32 ┆ u32 ┆ u32 │
╞═════════╪══════════════╪═════════════╡
0 ┆ 1 ┆ 1 │
1 ┆ 1 ┆ 1 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
3 ┆ 4 ┆ 4 │
4 ┆ 4 ┆ 4 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
6 ┆ 7 ┆ 7 │
7 ┆ 7 ┆ 7 │
└─────────┴──────────────┴─────────────┘
"""
Expand Down Expand Up @@ -665,11 +665,11 @@ def ordinal_day(self) -> pli.Expr:
│ --- ┆ --- ┆ --- │
│ u32 ┆ u32 ┆ u32 │
╞═════════╪══════════════╪═════════════╡
0 ┆ 1 ┆ 1 │
1 ┆ 1 ┆ 1 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
3 ┆ 4 ┆ 4 │
4 ┆ 4 ┆ 4 │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
6 ┆ 7 ┆ 7 │
7 ┆ 7 ┆ 7 │
└─────────┴──────────────┴─────────────┘
"""
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/internals/series/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def weekday(self) -> pli.Series:
Applies to Date and Datetime columns.
Returns the weekday number where monday = 0 and sunday = 6
Returns the ISO weekday number where monday = 1 and sunday = 7
Returns
-------
Expand Down Expand Up @@ -370,13 +370,13 @@ def weekday(self) -> pli.Series:
shape: (7,)
Series: '' [u32]
[
0
1
2
3
4
5
6
7
]
"""
Expand Down
4 changes: 2 additions & 2 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,9 +1606,9 @@ def test_weekday() -> None:

time_units: list[TimeUnit] = ["ns", "us", "ms"]
for tu in time_units:
assert s.dt.cast_time_unit(tu).dt.weekday()[0] == 0
assert s.dt.cast_time_unit(tu).dt.weekday()[0] == 1

assert s.cast(pl.Date).dt.weekday()[0] == 0
assert s.cast(pl.Date).dt.weekday()[0] == 1


@pytest.mark.skip(reason="from_dicts cannot yet infer timezones")
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ def test_dt_year_month_week_day_ordinal_day() -> None:
verify_series_and_expr_api(a, exp, "dt.year")

verify_series_and_expr_api(a, pl.Series("a", [5, 10, 2], dtype=UInt32), "dt.month")
verify_series_and_expr_api(a, pl.Series("a", [0, 4, 1], dtype=UInt32), "dt.weekday")
verify_series_and_expr_api(a, pl.Series("a", [1, 5, 2], dtype=UInt32), "dt.weekday")
verify_series_and_expr_api(a, pl.Series("a", [21, 40, 8], dtype=UInt32), "dt.week")
verify_series_and_expr_api(a, pl.Series("a", [19, 4, 20], dtype=UInt32), "dt.day")
verify_series_and_expr_api(
Expand Down

0 comments on commit 3e282a6

Please sign in to comment.