Skip to content

Allow null values in conversion to datetime object#999

Merged
sandorkertesz merged 1 commit into
developfrom
fix/dtlike-aux-coord-with-holes
May 20, 2026
Merged

Allow null values in conversion to datetime object#999
sandorkertesz merged 1 commit into
developfrom
fix/dtlike-aux-coord-with-holes

Conversation

@pawel-wolff
Copy link
Copy Markdown
Contributor

Description

This PR addresses the following issue:

import earthkit.data as ekd

fl = ekd.from_source("sample", "pl.grib").to_fieldlist()
fl2 = fl.sel({"parameter.variable": "t", "vertical.level": 500})[:3]

# len(fl2) == 3
# fl2.unique(["time.base_datetime", "time.step"]) == {
#     'time.base_datetime': (datetime.datetime(2024, 6, 3, 0, 0), datetime.datetime(2024, 6, 3, 12, 0)),
#     'time.step': (datetime.timedelta(0), datetime.timedelta(seconds=21600))
# }
# so a field for base_datetime=2024-6-3T12 and step=6h is missing

ds = fl2.to_xarray(allow_holes=True, add_valid_time_coord=True)

The above script crashes in

return to_datetime(dt.to_datetime())
with
AttributeError: 'FloatData' object has no attribute 'to_datetime'. The expected behaviour is to have an xarray Dataset in ds with valid_time auxiliary coordinate variable having 3 values + one 'NaT'.

To this end, the current PR extends the conversion to datetime object implemented in

allowing for null values.

Contributor Declaration

By opening this pull request, I affirm the following:

  • All authors agree to the Contributor License Agreement.
  • The code follows the project's coding standards.
  • I have performed self-review and added comments where needed.
  • I have added or updated tests to verify that my changes are effective and functional.
  • I have run all existing tests and confirmed they pass.

…for valid_time aux coord having NaT values wherever there are holes.
@pawel-wolff pawel-wolff requested a review from sandorkertesz May 18, 2026 17:17
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.48%. Comparing base (03ca2ad) to head (d3eecba).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #999      +/-   ##
===========================================
+ Coverage    81.46%   81.48%   +0.01%     
===========================================
  Files          236      236              
  Lines        15432    15444      +12     
  Branches       754      754              
===========================================
+ Hits         12572    12584      +12     
  Misses        2634     2634              
  Partials       226      226              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sandorkertesz sandorkertesz merged commit df0cc0b into develop May 20, 2026
144 checks passed
@sandorkertesz
Copy link
Copy Markdown
Collaborator

@pawel-wolff, thanks for this fix. As safety measure, I suggest you add tests for these new to_datatime() cases to tests/utils/test_dates.py in a separate PR.

@sandorkertesz sandorkertesz deleted the fix/dtlike-aux-coord-with-holes branch May 20, 2026 17:04
@pawel-wolff
Copy link
Copy Markdown
Contributor Author

Will do!

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.

3 participants