-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
YYYY week into a date #6796
Comments
Hi @Bidek56 Looks like this doesn't quite work in chrono: use chrono::{NaiveDate};
fn main() {
let date_str = "201901";
let naive_date = NaiveDate::parse_from_str(date_str, "%Y%U").unwrap();
println!("{:?}", naive_date);
} thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseError(NotEnough)', src/main.rs:5:66
note: [run with `RUST_BACKTRACE=1` environment variable to display a backtrace](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#) I think you need to also pass the day of the week, e.g. let date_str = "201901Mon";
let naive_date = NaiveDate::parse_from_str(date_str, "%Y%U%a").unwrap(); |
Does the code below work for you? b/c "%Y%U%a" works but not "%Y%W%a" but the docs says that it should.
|
I think YYYY-WW is just not a precise date. For example, take the ISO week date definition
That's probably why you need to specify the day of the week - although one might think that with the week number only, it should default to a certain day of week (but which one?!). So I'd conclude this is not a bug but a feature to avoid ambiguity. Only the chrono docs could be a bit more specific, IMHO. |
Hmmm, I wonder why |
@Bidek56 sorry, that was a bit unclear. In Python, both from datetime import datetime
print(datetime.strptime("2019-01-0", "%Y-%U-%w"))
# 2019-01-06 00:00:00
print(datetime.strptime("2019-01-0", "%Y-%W-%w"))
# 2019-01-13 00:00:00
print(datetime.strptime("2019-01-1", "%Y-%U-%w"))
# 2019-01-07 00:00:00
print(datetime.strptime("2019-01-1", "%Y-%W-%w"))
# 2019-01-07 00:00:00 It seem something strange is going on here; chrono errors out, and Python gives results that are unexpected for me. ISO week date parsing directives ( |
Returns: Error: ParseError(Impossible) But
Returns: 2020-01-12 |
Exactly. This seems somehow related to year 2019. But this polars issue #6796 is the wrong place to discuss I think. We should open an issue at chrono or a question on stackoverflow. |
@FObersteiner I will close this ticket and submit a Chrono issue instead. Thanks for your help. |
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
Attempting to parse YYYY week into date results in an error:
ComputeError: strict conversion to dates failed, maybe set strict=False
According to the Rust Chrono specifiers, %U is supported.
Pandas can read it:
df['date'] = pd.to_datetime(df.week.astype(str) + '0', format='%Y%W%w')
Thanks to SO, I have found a workaround but I think it's a bug.
Reproducible example
Expected behavior
date
2019-01-01
2019-01-08
2019-01-15
2019-10-15
2019-10-22
2019-10-29
Installed versions
The text was updated successfully, but these errors were encountered: