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

Conversation

MarcoGorelli
Copy link
Collaborator

@MarcoGorelli MarcoGorelli commented Jan 18, 2024

closes #13801

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jan 18, 2024
Comment on lines -147 to +148
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"]
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

.into_time()
.into_series()),
TimeUnit::Nanoseconds => {
let ca = self.0.apply_values(|v| {
Copy link
Collaborator Author

@MarcoGorelli MarcoGorelli Jan 18, 2024

Choose a reason for hiding this comment

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

I timed this separately, and apply_values is faster here than apply

Copy link
Contributor

@mcrumiller mcrumiller Jan 18, 2024

Choose a reason for hiding this comment

The 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 if branch (although maybe a premature optimization). Also I think we're already at i64 so we can remove the extra .cast(&Int64).unwrap().

            #[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()
            }),

Copy link
Member

Choose a reason for hiding this comment

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

I thinks this is a nice improvement. @mcrumiller 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

@mcrumiller mcrumiller Jan 19, 2024

Choose a reason for hiding this comment

The 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 git remote add <Marco's fork url> followed by a checkout of his branch. Not sure if that's the best way to do it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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,

git checkout -b pr/marcogorelli/man-on-the-moon
git fetch marcogorelli
git reset --hard marcogorelli/man-on-the-moon
<do your changes>
git push -U marcogorelli HEAD:man-on-the-moon

Copy link
Contributor

Choose a reason for hiding this comment

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

Got that working, now trying to figure out how to do create the PR, as your fork doesn't seem to show up in git's list:

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Edit: w00t

@@ -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)
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

fix(rust): simplify impl of datetime cast
@ritchie46 ritchie46 merged commit 6eb4db5 into pola-rs:main Jan 20, 2024
22 checks passed
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 24, 2024
…-rs#13812)

Co-authored-by: Marshall Crumiller <mcrumiller@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot extract Time from DateTime before Unix epoch
3 participants