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

Fix false timedelta decoding SerializationWarning and improve warning message #10072

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

spencerkclark
Copy link
Member

As reported in #10071, false warnings for resolution changes can be emitted when decoding timedeltas encoded with floating point values. In addition, the warning message could be clearer when resolution changes do occur. This PR fixes both issues.

The warning message in the event of a resolution change now reads:

>>> import xarray as xr
>>> var = xr.Variable(["time"], [0, 0.1], attrs={"units": "seconds"})
>>> xr.conventions.decode_cf_variable("foo", var, decode_timedelta=xr.coders.CFTimedeltaCoder(time_unit="s")).compute()
<stdin>:1: SerializationWarning: Can't decode floating point timedeltas to 's' without precision loss; decoding to 'ms' instead. To silence this warning pass decode_timedelta=CFTimedeltaCoder(time_unit='ms') to your opening function.
<xarray.Variable (time: 2)> Size: 16B
array([  0, 100], dtype='timedelta64[ms]')

cc: @kmuehlbauer

Copy link
Collaborator

@mathause mathause left a comment

Choose a reason for hiding this comment

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

Looks good!

@@ -582,6 +582,7 @@ def _numbers_to_timedelta(
time_unit: NPDatetimeUnitOptions,
ref_unit: PDDatetimeUnitOptions,
datatype: str,
target_unit: Optional[PDDatetimeUnitOptions] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
target_unit: Optional[PDDatetimeUnitOptions] = None,
target_unit: PDDatetimeUnitOptions | None = None,

?

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.

I'm fully on-board with this change! This is much more informative now. Thanks @spencerkclark!

Comment on lines +618 to +624
formatted_kwarg = f"{kwarg}={coder}(time_unit={new_time_unit!r})"
message = (
f"Can't decode floating point {datatype} to {time_unit!r} "
f"without precision loss; decoding to {new_time_unit!r} "
f"instead. To silence this warning pass {formatted_kwarg} "
f"to your opening function."
)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

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

Successfully merging this pull request may close these issues.

Invalid conversion of generic time coordinates
3 participants