Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: datetime operations (e.g. .dt.year) were raising when null values were backed by out-of-range integers #15420

Merged
merged 4 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/polars-arrow/src/compute/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ where
TimeUnit::Microsecond => timestamp_us_to_datetime,
TimeUnit::Nanosecond => timestamp_ns_to_datetime,
};
Ok(unary(array, |x| op(func(x)), data_type))
Ok(PrimitiveArray::<O>::from(
array
.iter()
.map(|v| v.map(|x| op(func(*x))))
.collect::<Vec<_>>(),
))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you collect a Vec of options here and then create a PrimitiveArray. This will be a double allocation. You can create the array directly from the iterator.

},
_ => unreachable!(),
}
Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,19 @@ def test_infer_iso8601_date(iso8601_format_date: str) -> None:
assert parsed.dt.day().item() == 13


def test_year_null_backed_by_out_of_range_15313() -> None:
# Create a Series where the null value is backed by a value which would
# be out-of-range for Datetime('us')
s = pl.Series([None, 2**63 - 1])
s -= 2**63 - 1
result = s.cast(pl.Datetime).dt.year()
expected = pl.Series([None, 1970], dtype=pl.Int32)
assert_series_equal(result, expected)
result = s.cast(pl.Date).dt.year()
expected = pl.Series([None, 1970], dtype=pl.Int32)
assert_series_equal(result, expected)


def test_series_is_temporal() -> None:
for tp in TEMPORAL_DTYPES | {
pl.Datetime("ms", "UTC"),
Expand Down
Loading