Skip to content

Commit

Permalink
feat(rust, python): keep sorted flag on offset_by (pola-rs#9253)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored and c-peters committed Jul 14, 2023
1 parent 0d4a81e commit b9769c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::*;

#[cfg(feature = "date_offset")]
pub(super) fn date_offset(s: Series, offset: Duration) -> PolarsResult<Series> {
match s.dtype().clone() {
let out = match s.dtype().clone() {
DataType::Date => {
let s = s
.cast(&DataType::Datetime(TimeUnit::Milliseconds, None))
Expand Down Expand Up @@ -42,7 +42,11 @@ pub(super) fn date_offset(s: Series, offset: Duration) -> PolarsResult<Series> {
dt => polars_bail!(
ComputeError: "cannot use 'date_offset' on Series of datatype {}", dt,
),
}
};
out.map(|mut out| {
out.set_sorted_flag(s.is_sorted_flag());
out
})
}

pub(super) fn combine(s: &[Series], tu: TimeUnit) -> PolarsResult<Series> {
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/unit/namespaces/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,16 @@ def test_negative_offset_by_err_msg_8464() -> None:
pl.Series([datetime(2022, 3, 30)]).dt.offset_by("-1mo")


def test_offset_by_sorted_flag() -> None:
s = pl.Series([datetime(2001, 1, 1), datetime(2001, 1, 2)])
s = s.set_sorted()

assert s.flags["SORTED_ASC"]
s1 = s.dt.offset_by("1d")
assert s1.to_list() == [datetime(2001, 1, 2), datetime(2001, 1, 3)]
assert s1.flags["SORTED_ASC"]


@pytest.mark.parametrize(
("duration", "input_date", "expected"),
[
Expand Down

0 comments on commit b9769c8

Please sign in to comment.