Open tritemio opened 3 months ago
Thanks For the report, I'm surprised this didn't work, will check
This might be a "bug" or design decision by chrono (the rust parser) BUT
"%Y-%m-%d %H:%M:%S%.6f0"
💥
"%Y-%m-%d %H:%M:%S.%6f0"
🍀
.
leftTo me this looks like it might be a bug in Chrono:
use chrono::NaiveDateTime;
fn main() {
let result = NaiveDateTime::parse_from_str(
"2024-06-03 20:02:48.6800000",
"%Y-%m-%d %H:%M:%S%.6f0",
);
println!("{:?}", result);
let result = NaiveDateTime::parse_from_str(
"2024-06-03 20:02:48.680000",
"%Y-%m-%d %H:%M:%S%.6f",
);
println!("{:?}", result);
}
prints out
Err(ParseError(TooShort))
Ok(2024-06-03T20:02:48.680)
For reference I have created a pytest file that tests several format strings on datetime.strptime
, pandas.to_datetime
and polars to_datetime
. Results shows the valid format string is different across the 3, with datetime and pandas being similar, while polars requires a different format.
I found that polars converts the string even without the trailing 0 in the format when using either %.f
or %.6f
. This should be the right format for chrono (although different from the python conventions).
Full results:
pytest file:
@tritemio correct, python datetime and chrono have different behaviour for some details.
you can check out the documentation
python
chrono
date = "2020-01-01 10:00:00.1234"
"%Y-%m-%d %H:%M:%S%.f"
-> 2020-01-01 10:00:00.123400
"%Y-%m-%d %H:%M:%S.%f"
-> 2020-01-01 10:00:00.000001234
Checks
Reproducible example
Log output
Issue description
Converting from string to datetime with a format string should allow to decode custom formats.
In this example the input has 7 digits for fractional seconds after the decimal dot. However the last digit is always zero and should be ignored because there is a trailing 0 in the format string.
Instead, polars gives the above error during conversion.
Stripping the extra zero from the string before attempting the conversion works correctly in polars:
Pandas accepts the original format and convert the string correctly, as does python's
datetime
Expected behavior
The column should be converted to datetime without error, as done in pandas and datetime from python standard lib.
Installed versions