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
Parsing datehour string without zero padded hour gives wrong output #8849
Comments
Please look at the docs: https://docs.rs/chrono/latest/chrono/format/strftime/index.html Your pattern doesn't seem correct.
|
@ritchie46 I had looked at the doc. But I am not able to understand how to parse hour with single digit as shared in sample. I have also shared the expressions that I tested. Am I missing something? |
Got it, this happens because chrono accepts it: chrono::NaiveDate::parse_from_str("2020-01-01|01", "%Y-%m-%d|%H") outputs Ok(2020-01-01) I'll take a look later, I think it this should just error, as |
Isn't the issue that the hours don't have leading zero's, so the fmt doesn't fit? |
I assumed it should work like python's datetime parser but I think chrono has different behaviour.
Output:
|
that's fine, it's allowed: chrono::NaiveDateTime::parse_from_str("2020-01-01 5:35", "%Y-%m-%d %H:%M") outputs
The issue is that |
Ah, right.. That's bad indeed. |
looks like chrono would be open to raising on their side for this chronotope/chrono#1075 (comment) that might be a better solution - I'll see if I can get this in there |
Opinions in chrono seem to be differing between developers So, possible solutions could be:
I think I like the sound of 2) the most, as it's simple and predictable |
Couldn't we check in the And that |
that would be nice, I'm just a bit wary about working around chrono, I'm worried this is going to lead to subtle hard-to-debug issues later...but I'll see, maybe it's not as bad as I'm thinking |
Yes, I don't mean that we work around chrono. Maybe we can get the parsed length from chrono ad check if it consumed the whole string. If it it didn't we fail. I thought chrono had an error showing that as well. The |
I might be missing something, but I don't any way to get this from chrono: use chrono;
fn main() {
println!("{:?}", chrono::NaiveDateTime::parse_from_str("2020-01-01 17", "%Y-%m-%d %H"));
println!("{:?}", chrono::NaiveDateTime::parse_from_str("2020-01-01", "%Y-%m-%d"));
println!("{:?}", chrono::NaiveDate::parse_from_str("2020-01-01 17", "%Y-%m-%d %H"));
println!("{:?}", chrono::NaiveDate::parse_from_str("2020-01-01", "%Y-%m-%d"));
} outputs
and it's not clear how to distinguish them. I've pasted a minimal reproducer of the issue in chronotope/chrono#1075 anyway, hope there's a non-hacky and performant solution 🤞 (it looks like opinions differ among their devs though, so it may be unlikely to change) If
|
Polars version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of Polars.
Issue description
I am trying to parse datehour where hour is not zero padded and both the expressions
Expr.str.strptime
andExpr.str.to_datetime
parses hour to zero when hour has single digite.g.
2023-05-04|7
->2023-05-04 00:00:00
2023-05-04|10
->2023-05-04 10:00:00
I tried following expressions but no luck
pl.col('dateHour').str.to_datetime('%Y-%m-%d|%H')
pl.col('dateHour').str.to_datetime('%Y-%m-%d|%k')
pl.col('dateHour').str.to_datetime('%Y-%m-%d|%-H')
pl.col('dateHour').str.to_datetime('%Y-%m-%d|%-k')
Reproducible example
Expected behavior
Installed versions
The text was updated successfully, but these errors were encountered: