Skip to content

invalid timestamps in the future #975

@mathause

Description

@mathause

If I have a netCDF file that has invalid timesteps from the 'future', it is wrongly converted to datetime64[ns].

import netCDF4 as nc
import numpy as np
import xarray as xr

# create netCDF file
ncf = nc.Dataset('test_future.nc', 'w')
ncf.createDimension('time')
ncf.createVariable('time', np.int, dimensions=('time'))
ncf.variables['time'].units = 'days since 1850-01-01 00:00:00'
ncf.variables['time'].calendar = 'standard'
ncf.variables['time'][:] = np.arange(850) * 365
ncf.close()

# open with xr
ds = xr.open_dataset('test_future.nc')
# this works
ds
# ds.time is a datetime64[ns] object
# this fails
ds.time

If I choose chalendar='noleap' the dates wrap around!

ncf = nc.Dataset('test_future_noleap.nc', 'w')
ncf.createDimension('time')
ncf.createVariable('time', np.int, dimensions=('time'))
ncf.variables['time'].units = 'days since 1850-01-01 00:00:00'
ncf.variables['time'].calendar = 'noleap'
ncf.variables['time'][:] = np.arange(850) * 365
ncf.close()

# open with xr
ds = xr.open_dataset('test_future_noleap.nc')
# after 2262 they go back to 1678!
ds.time

If my 'invalid' time is from the 'past' it works as expected:

ncf = nc.Dataset('test_past.nc', 'w')
ncf.createDimension('time')
ncf.createVariable('time', np.int, dimensions=('time'))
ncf.variables['time'].units = 'days since 1000-01-01 00:00:00'
ncf.variables['time'].calendar = 'standard'
ncf.variables['time'][:] = np.arange(850) * 365
ncf.close()

# open with xr
ds = xr.open_dataset('test_past.nc')
# this works
ds
# ds.time is a object
ds.time

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions