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

Raise error upon construction of out-of-bounds datetime? #9

Closed
spencerkclark opened this issue Feb 12, 2017 · 2 comments
Closed

Raise error upon construction of out-of-bounds datetime? #9

spencerkclark opened this issue Feb 12, 2017 · 2 comments

Comments

@spencerkclark
Copy link
Collaborator

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

cc @shoyer

@jhamman
Copy link
Collaborator

jhamman commented Feb 14, 2017

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

@jhamman
Copy link
Collaborator

jhamman commented Feb 13, 2018

closed via #12

@jhamman jhamman closed this as completed Feb 13, 2018
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

No branches or pull requests

2 participants