-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Relax nanosecond datetime restriction in CF time decoding #9618
base: main
Are you sure you want to change the base?
Conversation
Nice, mypy 1.12 is out and breaks our typing, 😭. |
Can we pin it in the CI temporarily? |
Yes, 1.11.2 was the last version. |
ca5050d
to
f7396cf
Compare
This is now ready for a first round of review. I think this is already in a quite usable state. But no rush, this should be thoroughly tested. |
Sounds good @kmuehlbauer! I’ll try and take an initial look this weekend. |
…ore/variable.py to use any-precision datetime/timedelta with autmatic inferring of resolution
…ocessing, raise now early
…t resolution, fix code and tests to allow this
Not to throw too much of a wrench in the works here -- so feel free to disregard, but there's an issue I've faced with (single precision) float time encoding: Folks (carelessly :-( ) sometimes encode times as "days since ..." using a single precision float. The problem here is not unnecessary precision, as you get with double, but too little -- if you go more than a few years out , you lose seconds precision. (the key problem is that float time -- its precision is a function of the magnitude -- not good for this use case) The end result is that I get things like model timesteps that are supposed to be hourly, reporting as, e.g. 12:00:18, rather than 12:00:00 One way I've dealt with this is rounding to the minute, or even to hours (if I know the output is hourly), or perhaps to 10 minutes. Could / should xarray provide a facility for doing this? maybe? I guess what I'm proposing is that there be some way to tell xarray to store / save a time variable with e.g. second precision, but to round it to something more coarse when decoding. maybe this could even be automatic / inferred: if a time is in float days since -- it almost certainly is NOT millisecond precision, or even second precision -- and you could even look at the values (the first one?) and see what the minimum precision is for that timespan. If I've done my math right, a float can only store second precision for a little over three years. So if the values are greater than three years, you don't have second precision. Anyway, maybe way too much magic, but it would be nice for my use cases :-) Example:
Ouch! so what were 15 minute timesteps is now off by about one minute -- and what's too bad is that rounding to the minute wouldn't be right either -- you'd need to round to maybe 5 minutes? Anyway, maybe this simply isn't xarray's problem to solve -- data providers shouldn't make such mistakes :-( |
@ChrisBarker-NOAA yeah, I agree this kind of situation is annoying, but my feeling is that trying to fix this automatically would be too much magic. Xarray has convenient functionality for rounding times, which can be used to correct this explicitly—that would be my preference. E.g. for your example it would look like:
|
Oh, nice: I had missed that! -- you're probably right, too much magic to do for people. |
@spencerkclark @ChrisBarker-NOAA I've implemented automated decoding of floating point data to the needed resolution, even when the wanted resolution does not apply. Unfortunately the above outlined behaviour is too much involved to be put into the decoder. Nevertheless maybe we can distill some best practices from your vast experience with data @ChrisBarker-NOAA and create a nice example how to handle these difficulties? |
Sure -- where would be a good home for that? |
Not sure, but https://docs.xarray.dev/en/stable/user-guide/time-series.html could have a dedicated floating point date section. |
I've added a kwarg But instead of adding that kwarg we could slightly overload the This would have the positive effect, that we wouldn't need the additional kwarg and have to distribute it through the backends.
We could guard This methodology would be fully backwards compatible. It advertises the change via DeprecationWarning in normal operation and also if issues appear in the decoding steps. If this is something which makes sense @shoyer, @dcherian, @spencerkclark, I'd add the needed changes to this PR. |
Alternatively, we could make small progress on #4490 and have from xarray.coding import DatetimeCoder
ds = xr.open_mfdataset(..., decode_times=DatetimeCoder(units="ms")) In the long term, it seems nice to have the default use the "natural" units i.e.
|
This took a while to sink in 😉 Yes, that's a neat move. I'll incorporate this suggestion.
As long as we use |
+1 for fewer arguments to Indeed for those practical reasons I do not think it is worth trying to match the on-disk units of integer data any more closely. Second precision already allows for a time span of roughly +/- 290 billion years (many times older than the Earth), which I think is plenty for most applications :). Monthly or yearly units are also somewhat awkward to deal with due to their different (albeit often violated) definition in the CF conventions. |
whats-new.rst
This is another attempt to resolve #7493. This goes a step further than #9580.
The idea of this PR is to automatically infer the needed resolutions for decoding/
encodingand only keep the constraints pandas imposes ("s" - lowest resolution, "ns" - highest resolution). There is still the idea of adefault resolution
, but this should only take precedence if it doesn't clash with the automatic inference. This can be discussed, though. Update: I've implementedtime-unit
-kwarga first try to have default resolutionon decode, which will override the current inferred resolution only to higher resolution (eg.'s'
->'ns'
).For sanity checking, and also for my own good, I've created a documentation page on time-coding in the internal dev section. Any suggestions (especially grammar) or ideas for enhancements are much appreciated.
There still might be room for consolidation of functions/methods (mostly in coding/times.py), but I have to leave it alone for some days. I went down that rabbit hole and need to relax, too 😬.
Looking forward to get your insights here, @spencerkclark, @ChrisBarker-NOAA, @pydata/xarray.
Todo:
time_units
(where appropriate)