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

dt.total_nanoseconds and dt.total_microseconds may overflow silently #16057

Open
2 tasks done
stinodego opened this issue May 5, 2024 · 0 comments
Open
2 tasks done
Labels
A-timeseries Area: date/time functionality bug Something isn't working P-low Priority: low python Related to Python Polars

Comments

@stinodego
Copy link
Member

stinodego commented May 5, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

from datetime import timedelta
import polars as pl

s = pl.Series([timedelta(days=106752)])
print(s.dt.total_nanoseconds())

Log output

shape: (1,)
Series: '' [i64]
[
        -9223371273709551616
]

Issue description

The Series has time unit us. Getting the total number of nanoseconds requires multiplying the underlying integer by 1000, which may overflow.

We currently use unchecked multiplication for this, while we would need to use checked multiplication and set any overflowing values to null. The fix would be to implement checked arithmetic kernels and use these here.

The offending code is here:

/// Extract the nanoseconds from a `Duration`
fn nanoseconds(&self) -> Int64Chunked {
match self.time_unit() {
TimeUnit::Milliseconds => &self.0 * NANOSECONDS_IN_MILLISECOND,
TimeUnit::Microseconds => &self.0 * 1000,
TimeUnit::Nanoseconds => self.0.clone(),
}
}
}

Expected behavior

Output should be null rather than wrapping.

Installed versions

main

@stinodego stinodego added bug Something isn't working python Related to Python Polars needs triage Awaiting prioritization by a maintainer P-low Priority: low A-timeseries Area: date/time functionality and removed needs triage Awaiting prioritization by a maintainer labels May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-timeseries Area: date/time functionality bug Something isn't working P-low Priority: low python Related to Python Polars
Projects
Status: Ready
Development

No branches or pull requests

1 participant