Skip to content

Commit

Permalink
ensure weekday starts at 0 (#4220)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 2, 2022
1 parent af35ce0 commit ec4f7e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion polars/polars-time/src/chunkedarray/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ pub trait DatetimeMethods: AsDatetime {
/// Extract weekday from underlying NaiveDateTime representation.
/// Returns the weekday number where monday = 0 and sunday = 6
fn weekday(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::weekday)
let ca = self.as_datetime();
let f = match ca.time_unit() {
TimeUnit::Nanoseconds => datetime_to_weekday_ns,
TimeUnit::Microseconds => datetime_to_weekday_us,
TimeUnit::Milliseconds => datetime_to_weekday_ms,
};
ca.apply_kernel_cast::<UInt32Type>(&f)
}

/// Returns the ISO week number starting from 1.
Expand Down
26 changes: 26 additions & 0 deletions polars/polars-time/src/chunkedarray/kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,29 @@ to_temporal_unit!(
i64,
ArrowDataType::UInt32
);

#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_weekday_ns,
p_weekday,
timestamp_ns_to_datetime,
i64,
ArrowDataType::UInt32
);

#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_weekday_ms,
p_weekday,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_weekday_us,
p_weekday,
timestamp_us_to_datetime,
i64,
ArrowDataType::UInt32
);
10 changes: 10 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,3 +1252,13 @@ def test_supertype_timezones_4174() -> None:
# test if this runs without error
date_to_fill = df["dt_London"][0]
df["dt_London"] = df["dt_London"].shift_and_fill(1, date_to_fill)


def test_weekday() -> None:
# monday
s = pl.Series([datetime(2020, 1, 6)])

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

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

0 comments on commit ec4f7e8

Please sign in to comment.