-
-
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
Calendar utilities #5155
Comments
|
@aaronspring Oh, I didn't think of that trick for 1, thanks! But again, this fails with numpy-backed time coordinates. With the definition of a "default" calendar, there could be a more general way. |
Thanks for opening up this discussion @aulemahal! It's great to see the boundaries being pushed on what can be done with cftime dates in xarray.
I think something like this would be cool too (see the end of #4092 (comment)). Attributes on the
Both
We actually considered something like this in the initial stages of writing Using the calendar argument, as opposed to the range of the dates, to decide what type to return is a slightly different twist than what was discussed previously. I don't know how I feel about that. On one hand I can see why it would be convenient, but on the other hand
Would that work for your use-case? |
Cool! I started a branch, will push a PR soon. I understand the "default" issue and using
|
Edited the comment above as I realized that numpy /pandas / xarray use nanoseconds by default, so the earliest date possible is 1677-09-21 00:12:43.145225. |
Great, thanks!
I might actually lean toward your original thought, i.e. having (There is of course the complication that the NumPy dates could have been decoded from a file with a calendar attribute of |
Given the increasing scope of cftime-related utilities, I wonder if it would make sense to try to eventually move the cftime-related utilities outside of Xarray into a separate package? After the current ongoing "explicit indexes" refactor, I believe we will have public APIs for all of the essential functionality. |
I agree that it kinda crowds the xarray API. My first idea was to implement these calendar utilities in |
Yeah, I think that could be something good to strive for, and it's exciting that the explicit indexes refactor could facilitate that. To be clear, are you ok with merging #5233 and waiting until after the refactor is complete to discuss future plans there? |
Is your feature request related to a problem? Please describe.
Handling cftime and numpy time coordinates can sometimes be exhausting. Here I am thinking of the following common problems:
Describe the solution you'd like
ds.time.dt.calendar
would be magic.xr.convert_calendar(ds, "new_cal")
could be nice?xr.date_range(start, stop, calendar=cal)
, same as pandas' (see context below).Describe alternatives you've considered
We have implemented all this in (xclim)[https://xclim.readthedocs.io/en/stable/api.html#calendar-handling-utilities] (and more). But it seems to make sense that some of the simplest things there could move to xarray? We had this discussion in xarray-contrib/cf-xarray#193 and suggestion was made to see what fits here before implementing this there.
Additional context
At xclim, to differentiate numpy datetime64 from cftime types, we call the former "default". This way a time coordinate using cftime's "proleptic_gregorian" calendar is distinct from one using numpy's datetime64.
cftime.datetime
objects tonp.datetime64
values throughastype
#5107.We also have an
interp_calendar
function that reinterps data on a yearly basis. This is a bit narrower, because it only makes sense on daily data (or coarser).date_range
anddate_range_like
simply chose betweenpd.date_range
andxr.cftime_range
according to the target calendar.What do you think? I have time to move whatever code makes sense to move.
The text was updated successfully, but these errors were encountered: