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

Update time coding tests to assert exact equality #9961

Merged
merged 2 commits into from
Jan 22, 2025

Conversation

spencerkclark
Copy link
Member

Per #9618 (comment), this PR updates relevant time coding tests to assert exact equality rather than equality with a tolerance, since xarray's minimum supported version of cftime has been greater than 1.2.1 for a while now.

cc: @kmuehlbauer

  • Internal changes are documented in whats-new.rst

Comment on lines +146 to +150
if np.timedelta64(1, time_unit) > np.timedelta64(1, minimum_resolution):
expected_unit = minimum_resolution
else:
expected_unit = time_unit
expected = cftime_to_nptime(expected, time_unit=expected_unit)
Copy link
Member Author

Choose a reason for hiding this comment

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

I needed to add this logic specifically for this test:

   (12300 + np.arange(5), "hour since 1680-01-01 00:00:00.500000", "us"),

This is because the reference date required the dates be decoded to microsecond resolution (really only millisecond resolution would be required, but we follow pandas's lead here). Otherwise for time_unit="s" we would truncate precision when converting the cftime objects to np.datetime64 values, which prevented asserting exact equality.

@@ -220,7 +220,7 @@ def test_decode_standard_calendar_inside_timestamp_range(
) -> None:
import cftime

units = "days since 0001-01-01"
units = "hours since 0001-01-01"
Copy link
Member Author

Choose a reason for hiding this comment

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

Using encoding units of "days" unnecessarily led the times to be encoded with floats, which prevented asserting exact equality in this test. Testing floating point time decoding was not the point of this test, so I changed to encoding the times with units of "hours" instead.

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, this is looking good.

@kmuehlbauer
Copy link
Contributor

@spencerkclark Adding the plan-to-merge label. Is there anything you want to add here? Otherwise this is good to merge AFAICT.

@kmuehlbauer kmuehlbauer added the plan to merge Final call for comments label Jan 21, 2025
@spencerkclark
Copy link
Member Author

Thanks for the review @kmuehlbauer—indeed I think this should be ready to go. I will go ahead and merge.

@spencerkclark spencerkclark merged commit 4ccb048 into pydata:main Jan 22, 2025
29 of 35 checks passed
@spencerkclark spencerkclark deleted the cftime-assert-equal branch January 22, 2025 00:27
dcherian added a commit that referenced this pull request Jan 30, 2025
* main: (79 commits)
  fix mean for datetime-like using the respective time resolution unit (#9977)
  Add `time_unit` argument to `CFTimeIndex.to_datetimeindex` (#9965)
  remove gate and add a test (#9958)
  Remove repetitive that (replace it with the) (#9994)
  add shxarray to the xarray ecosystem list (#9995)
  Add `shards` to `valid_encodings` to enable sharded Zarr writing (#9948)
  Use flox for grouped first, last (#9986)
  Bump the actions group with 2 updates (#9989)
  Fix some typing (#9988)
  Remove unnecessary a article (#9980)
  Fix test_doc_example on big-endian systems (#9949)
  fix weighted polyfit for arrays with more than 2 dimensions (#9974)
  Use zarr-fixture to prevent thread leakage errors (#9967)
  remove dask-expr from CI runs, fix related tests (#9971)
  Update time coding tests to assert exact equality (#9961)
  cast type to PDDatetimeUnitOptions (#9963)
  Suggest the correct name when no key matches in the dataset (#9943)
  fix upstream dev issues (#9953)
  Relax nanosecond datetime restriction in CF time decoding (#9618)
  Remove outdated quantile test. (#9945)
  ...
dcherian added a commit that referenced this pull request Jan 30, 2025
* main: (79 commits)
  fix mean for datetime-like using the respective time resolution unit (#9977)
  Add `time_unit` argument to `CFTimeIndex.to_datetimeindex` (#9965)
  remove gate and add a test (#9958)
  Remove repetitive that (replace it with the) (#9994)
  add shxarray to the xarray ecosystem list (#9995)
  Add `shards` to `valid_encodings` to enable sharded Zarr writing (#9948)
  Use flox for grouped first, last (#9986)
  Bump the actions group with 2 updates (#9989)
  Fix some typing (#9988)
  Remove unnecessary a article (#9980)
  Fix test_doc_example on big-endian systems (#9949)
  fix weighted polyfit for arrays with more than 2 dimensions (#9974)
  Use zarr-fixture to prevent thread leakage errors (#9967)
  remove dask-expr from CI runs, fix related tests (#9971)
  Update time coding tests to assert exact equality (#9961)
  cast type to PDDatetimeUnitOptions (#9963)
  Suggest the correct name when no key matches in the dataset (#9943)
  fix upstream dev issues (#9953)
  Relax nanosecond datetime restriction in CF time decoding (#9618)
  Remove outdated quantile test. (#9945)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants