Open michael72 opened 2 days ago
issue in prophet that stems from this one: https://github.com/facebook/prophet/issues/2529
@michael72 this is a subtle issue and something that caused confusion before, if I recall correctly.
The main explanation for the behaviour is that the unit
keyword denotes the resolution of the input, and not the output (the docstring for it also says that somewhat, although reading it now it's not super clear ..), and so it is only used for numeric input.
So essentially in the example above it is just ignored, because the input is not numeric, and since it is already datetime64, the input is returned as is.
Thus, as far as I know, this is the expected behaviour. Though, I agree this is confusing (as long as we only supported ns
, that was probably OK, but now that we support multiple units, it's very logical to think that specifying unit
means the output unit ..). I think it would be nice if we could for example change the default for unit
to None (essentially make it required to specify if you want to use), so we can raise an informative error message when the user passes it in a wrong case.
If you want to converting an existing datetime64
column to a different unit, you can use the general astype
or the specific as_unit()
(available on DatetimeIndex or on Series through the .dt.
accessor)
Agreed the docs could be clarified here. Perhaps we could also rename unit
? Not sure if that'd be worth the churn. Another possibility would be to raise if unit
is provided but not used because the input is not numeric.
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
This is a rather constructed case but we had a very subtle bug reading parquet data that had the
ds
column stored asdatetime64[ms]
(previously str) and actually needed the result asdatetime64[ns]
. Whileto_datetime
works for other types it does not change the used unit, when the underlying type is alreadydatetime64
- and it does so silently.Expected Behavior
I would either expect an exception, that the type already is
datetime64
or the result to have the correct unit - here:datetime64[ns]
and not using the same as the input (here:datetime64[ms]
)Installed Versions