You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In pydata/xarray#1252 I am working on a NetCDFTimeIndex that is intended to bring some of the features of pandas's DatetimeIndex (for now namely field accessors and partial datetime string indexing) to time indexes that use netcdftime._netcdftime.datetime objects.
Currently one can construct non-sensical datetimes using netcdftime._netcdftime.datetime objects:
In [1]: from netcdftime import DatetimeNoLeap
In [2]: DatetimeNoLeap(1, 45, 45)
Out[2]: netcdftime._netcdftime.DatetimeNoLeap(1, 45, 45, 0, 0, 0, 0, -1, 1)
Would it be possible for this kind of expression to raise an error? This would be nice, because that way if one tries to index with a slice involving an out-of-bounds datetime, it would automatically cause an error, rather than behave in the fashion below:
In [1]: import xarray as xr
In [2]: from xarray.conventions.netcdftimeindex import NetCDFTimeIndex
In [3]: from netcdftime import DatetimeNoLeap
In [4]: dates = [DatetimeNoLeap(1, 1, 1), DatetimeNoLeap(1, 2, 1), DatetimeNoLeap(2, 1, 1), DatetimeNoLeap(2, 2, 1)]
In [5]: da = xr.DataArray([1, 2, 3, 4], coords=[NetCDFTimeIndex(dates)], dims=['time'])
In [6]: da.sel(time=slice(DatetimeNoLeap(1, 1, 1), DatetimeNoLeap(1, 45, 45)))
Out[6]:
<xarray.DataArray (time: 2)>
array([1, 2])
Coordinates:
* time (time) object 1-01-01 00:00:00 1-02-01 00:00:00
This should absolutely be done. We should be able to provide similar checks to what is provided in the datetime module.
In [1]: import datetime
In [2]: datetime.datetime(2000, 1, 45, -4)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-2492d881556f> in <module>()
----> 1 datetime.datetime(2000, 1, 45, -4)
ValueError: day is out of range for month
In pydata/xarray#1252 I am working on a
NetCDFTimeIndex
that is intended to bring some of the features of pandas's DatetimeIndex (for now namely field accessors and partial datetime string indexing) to time indexes that usenetcdftime._netcdftime.datetime
objects.Currently one can construct non-sensical datetimes using
netcdftime._netcdftime.datetime
objects:Would it be possible for this kind of expression to raise an error? This would be nice, because that way if one tries to index with a slice involving an out-of-bounds datetime, it would automatically cause an error, rather than behave in the fashion below:
cc @shoyer
The text was updated successfully, but these errors were encountered: