-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,21 +81,39 @@ impl LogicalType for DatetimeChunked { | |
}, | ||
#[cfg(feature = "dtype-time")] | ||
(Datetime(tu, _), Time) => match tu { | ||
TimeUnit::Nanoseconds => Ok((self.0.as_ref() % NS_IN_DAY) | ||
.cast(&Int64) | ||
.unwrap() | ||
.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()), | ||
TimeUnit::Nanoseconds => { | ||
let ca = self.0.apply_values(|v| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I timed this separately, and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit more concise (I'm always looking to reduce code length) and possibly faster since it removes the #[cfg(feature = "dtype-time")]
(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()
}), There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thinks this is a nice improvement. @mcrumiller 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup - @mcrumiller fancy opening a PR to my PR, so you're included in the commit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure (although I don't care about the cred)--need to figure out how to check out a branch on another fork first. Edit: I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good learning experience then :) but yes, that's how I'd have done it alternatively,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Edit: w00t |
||
let v = v % NS_IN_DAY; | ||
if v < 0 { | ||
v + NS_IN_DAY | ||
} else { | ||
v | ||
} | ||
}); | ||
Ok(ca.cast(&Int64).unwrap().into_time().into_series()) | ||
}, | ||
TimeUnit::Microseconds => { | ||
let ca = self.0.apply_values(|v| { | ||
let v = v % US_IN_DAY * 1_000i64; | ||
if v < 0 { | ||
v + NS_IN_DAY | ||
} else { | ||
v | ||
} | ||
}); | ||
Ok(ca.cast(&Int64).unwrap().into_time().into_series()) | ||
}, | ||
TimeUnit::Milliseconds => { | ||
let ca = self.0.apply_values(|v| { | ||
let v = v % MS_IN_DAY * 1_000_000i64; | ||
if v < 0 { | ||
v + NS_IN_DAY | ||
} else { | ||
v | ||
} | ||
}); | ||
Ok(ca.cast(&Int64).unwrap().into_time().into_series()) | ||
}, | ||
}, | ||
_ => self.0.cast(dtype), | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
There was a problem hiding this comment.
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