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: .dt.time() was panicking for datetimes prior to unix epoch #13812

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 10 additions & 16 deletions crates/polars-core/src/chunked_array/logical/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,17 @@ impl LogicalType for DatetimeChunked {
.into_series()),
},
#[cfg(feature = "dtype-time")]
(Datetime(tu, _), Time) => match tu {
TimeUnit::Nanoseconds => Ok((self.0.as_ref() % NS_IN_DAY)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the issue with the current implementation is that self.0.as_ref() % NS_IN_DAY might return a negative value

.cast(&Int64)
.unwrap()
(Datetime(tu, _), Time) => Ok({
let (modder, multiplier) = match tu {
TimeUnit::Nanoseconds => (NS_IN_DAY, 1i64),
TimeUnit::Microseconds => (US_IN_DAY, 1_000i64),
TimeUnit::Milliseconds => (MS_IN_DAY, 1_000_000i64),
};
self.0
.apply_values(|v| (v % modder * multiplier) + (NS_IN_DAY * (v < 0) as i64))
.into_time()
.into_series()),
TimeUnit::Microseconds => Ok((self.0.as_ref() % US_IN_DAY * 1_000i64)
.cast(&Int64)
.unwrap()
.into_time()
.into_series()),
TimeUnit::Milliseconds => Ok((self.0.as_ref() % MS_IN_DAY * 1_000_000i64)
.cast(&Int64)
.unwrap()
.into_time()
.into_series()),
},
.into_series()
}),
_ => self.0.cast(dtype),
}
}
Expand Down
14 changes: 12 additions & 2 deletions py-polars/tests/unit/namespaces/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,18 @@ def test_local_datetime_sortedness(time_zone: str | None, expected: bool) -> Non
def test_local_time_sortedness(time_zone: str | None) -> None:
ser = (pl.Series([datetime(2022, 1, 1, 23)]).dt.replace_time_zone(time_zone)).sort()
result = ser.dt.time()
assert result.flags["SORTED_ASC"] is False
assert result.flags["SORTED_DESC"] is False
assert result.flags["SORTED_ASC"]
assert not result.flags["SORTED_DESC"]
Comment on lines -147 to +148
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this one should've been sorted to begin with, but that's already being dealt with in #13808



@pytest.mark.parametrize("time_unit", ["ms", "us", "ns"])
def test_local_time_before_epoch(time_unit: TimeUnit) -> None:
ser = pl.Series([datetime(1969, 7, 21, 2, 56, 2, 123000)]).dt.cast_time_unit(
time_unit
)
result = ser.dt.time().item()
expected = time(2, 56, 2, 123000)
assert result == expected


@pytest.mark.parametrize(
Expand Down