cftime.DatetimeNoLeap incorrectly decoded from netCDF file
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
I have been given a netCDF file (I think it's netCDF3) which when I open it does not decode the time variable in the way I expected it to. The time coordinate created is a numpy object array
What did you expect to happen?
I expected it to automatically create a coordinate backed by a CFTimeIndex object, not a CFTimeIndex object wrapped inside another array type.
Minimal Complete Verifiable Example
The original problematic file is 455MB (I can share it if necessary), but I can create a small netCDF file that displays the same issue.
import cftime
time_values = [cftime.DatetimeNoLeap(347, 2, 1, 0, 0, 0, 0, has_year_zero=True)]
time_ds = xr.Dataset(coords={'time': (['time'], time_values)})
print(time_ds)
time_ds.to_netcdf('time_mwe.nc')
<xarray.Dataset>
Dimensions: (time: 1)
Coordinates:
* time (time) object 0347-02-01 00:00:00
Data variables:
*empty*
ds = xr.open_dataset('time_mwe.nc', engine='netcdf4', decode_times=True, use_cftime=True)
print(ds)
<xarray.Dataset>
Dimensions: (time: 1)
Coordinates:
* time (time) object 0347-02-01 00:00:00
Data variables:
*empty*
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
No response
Anything else we need to know?
No response
Environment
cftime 1.6.2
netcdf4 1.6.4
xarray 2023.8.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided cftime.DatetimeNoLeap example and the xarray.open_dataset call using engine="netcdf4", decode_times=True, and use_cftime=True. Trace how the time coordinate is decoded and compare its type with the expected CFTimeIndex; done means the coordinate is not a CFTimeIndex wrapped inside another object array.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100