Skip to content
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

Enable passing a CFTimedeltaCoder to decode_timedelta #9966

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

spencerkclark
Copy link
Member

I had some time this weekend so I worked on enabling passing a CFTimedeltaCoder to decode_timedelta in open_dataset et al.

@kmuehlbauer I didn't realize until this morning that you had started on this too in your fork (branch)—I was able to make a simplification to the dtype inference code I originally had based on looking at your initial work. I'm posting this here, since it seems a little further along than your branch, but feel free to supersede this PR with one of your own if you have un-pushed progress. I listed this PR in the same what's new entry as #9618, since it seemed natural, and gives us both credit.

It looks like we were fairly aligned though in terms of inheriting the time_unit from decode_times if nothing was explicitly passed to decode_timedelta, which is good.

  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst

@kmuehlbauer
Copy link
Contributor

Yes, I was just playing around a bit. Please go ahead, @spencerkclark. I'll catch up tomorrow.

Copy link
Contributor

@kmuehlbauer kmuehlbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @spencerkclark! One suggestion for adding a FutureWarning and explicit mention of decode_times=False in the docs.

.. ipython:: python

coder = xr.coders.CFDatetimeCoder(time_unit="s")
xr.open_dataset("test-timedeltas2.nc", decode_times=coder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still some unresolved issue, where we should think about a way forward. At least we could document it, like this:

Suggested change
xr.open_dataset("test-timedeltas2.nc", decode_times=coder)
xr.open_dataset("test-timedeltas2.nc", decode_times=coder)
To opt-out of timedelta decoding (see issue `Undesired decoding to timedelta64 <https://github.com/pydata/xarray/)issues/1621>`_) pass ``False`` to ``decode_timedelta``:
.. ipython:: python
xr.open_dataset("test-timedeltas2.nc", decode_times=False)

I'm not sure if it would be a good idea to change the default to decode_times=False in this cycle of changes (as discussed in #1621), but we could at least add a future warning when decode_timedelta=None. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that is a good idea—thanks for reminding me about that issue. I'll work on an update when I get a chance.

@@ -171,7 +173,10 @@ def decode_cf_variable(
original_dtype = var.dtype

if decode_timedelta is None:
decode_timedelta = True if decode_times else False
if isinstance(decode_times, CFDatetimeCoder):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we could add a FutureWarning, that decode_timedelta=None will default to decode_timedelta=False. See #1621.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants