{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/xarray/issues/1252","repository_url":"https://api.github.com/repos/pydata/xarray","labels_url":"https://api.github.com/repos/pydata/xarray/issues/1252/labels{/name}","comments_url":"https://api.github.com/repos/pydata/xarray/issues/1252/comments","events_url":"https://api.github.com/repos/pydata/xarray/issues/1252/events","html_url":"https://github.com/pydata/xarray/pull/1252","id":205473898,"number":1252,"title":"WIP: NetCDFTimeIndex","user":{"login":"spencerkclark","id":6628425,"avatar_url":"https://avatars.githubusercontent.com/u/6628425?v=3","gravatar_id":"","url":"https://api.github.com/users/spencerkclark","html_url":"https://github.com/spencerkclark","followers_url":"https://api.github.com/users/spencerkclark/followers","following_url":"https://api.github.com/users/spencerkclark/following{/other_user}","gists_url":"https://api.github.com/users/spencerkclark/gists{/gist_id}","starred_url":"https://api.github.com/users/spencerkclark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spencerkclark/subscriptions","organizations_url":"https://api.github.com/users/spencerkclark/orgs","repos_url":"https://api.github.com/users/spencerkclark/repos","events_url":"https://api.github.com/users/spencerkclark/events{/privacy}","received_events_url":"https://api.github.com/users/spencerkclark/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-02-06T02:10:47Z","updated_at":"2017-02-06T14:46:36Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/pydata/xarray/pulls/1252","html_url":"https://github.com/pydata/xarray/pull/1252","diff_url":"https://github.com/pydata/xarray/pull/1252.diff","patch_url":"https://github.com/pydata/xarray/pull/1252.patch"},"body":" - [ ] closes #1084\r\n - [ ] tests added / passed\r\n - [ ] passes ``git diff upstream/master | flake8 --diff``\r\n - [ ] whatsnew entry\r\n\r\nThis work in progress PR is a start on implementing a ```NetCDFTimeIndex```, a subclass of pandas.Index, which closely mimics pandas.DatetimeIndex, but uses ```netcdftime._netcdftime.datetime``` objects.  Currently implemented in the new index are:\r\n- Partial datetime-string indexing (using strictly [ISO8601-format strings](https://en.wikipedia.org/wiki/ISO_8601), using a date parser implemented by @shoyer in https://github.com/pydata/xarray/issues/1084#issuecomment-274372547)\r\n- Field-accessors for year, month, day, hour, minute, second, and microsecond, to enable ```groupby``` operations on attributes of date objects\r\n\r\nThis index is meant as a step towards improving the handling of non-standard calendars and dates outside the range ```Timestamp('1677-09-21 00:12:43.145225')``` to ```Timestamp('2262-04-11 23:47:16.854775807')```.\r\n\r\n--------------\r\n\r\nFor now I have pushed only the code and some tests for the new index; I want to make sure the index is solid and well-tested before we consider integrating it into any of xarray's existing logic or writing any documentation.  \r\n\r\nRegarding the index, there are a couple remaining outstanding issues (that at least I'm aware of):\r\n\r\n1. Currently one can create non-sensical datetimes using ```netcdftime._netcdftime.datetime``` objects.  This means one can attempt to index with an out-of-bounds string or datetime without raising an error.  Could this possibly be addressed upstream?  For example:\r\n```\r\nIn [1]: from netcdftime import DatetimeNoLeap\r\n\r\nIn [2]: DatetimeNoLeap(2000, 45, 45)\r\nOut[2]: netcdftime._netcdftime.DatetimeNoLeap(2000, 45, 45, 0, 0, 0, 0, -1, 1)\r\n```\r\n2. I am looking to enable this index to be used in pandas.Series and pandas.DataFrame objects as well;  this requires implementing a ```get_value``` method.  I have taken @shoyer's suggested simplified approach from https://github.com/pydata/xarray/issues/1084#issuecomment-275963433, and tweaked it to also allow for slice indexing, so I think this is most of the way there.  A remaining to-do for me, however, is to implement something to allow for integer-indexing outside of ```iloc```, e.g.  if you have a pandas.Series ```series```, indexing with the syntax ```series[1]``` or ```series[1:3]```.\r\n\r\nHopefully this is a decent start; in particular I'm not an expert in writing tests so please let me know if there are improvements I can make to the structure and / or style I've used so far.  I'm happy to make changes.  I appreciate your help."},"comment":{"url":"https://api.github.com/repos/pydata/xarray/issues/comments/277703296","html_url":"https://github.com/pydata/xarray/pull/1252#issuecomment-277703296","issue_url":"https://api.github.com/repos/pydata/xarray/issues/1252","id":277703296,"user":{"login":"spencerkclark","id":6628425,"avatar_url":"https://avatars.githubusercontent.com/u/6628425?v=3","gravatar_id":"","url":"https://api.github.com/users/spencerkclark","html_url":"https://github.com/spencerkclark","followers_url":"https://api.github.com/users/spencerkclark/followers","following_url":"https://api.github.com/users/spencerkclark/following{/other_user}","gists_url":"https://api.github.com/users/spencerkclark/gists{/gist_id}","starred_url":"https://api.github.com/users/spencerkclark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spencerkclark/subscriptions","organizations_url":"https://api.github.com/users/spencerkclark/orgs","repos_url":"https://api.github.com/users/spencerkclark/repos","events_url":"https://api.github.com/users/spencerkclark/events{/privacy}","received_events_url":"https://api.github.com/users/spencerkclark/received_events","type":"User","site_admin":false},"created_at":"2017-02-06T14:46:36Z","updated_at":"2017-02-06T14:46:36Z","body":"Thanks for the quick feedback on the tests @MaximilianR.  Is this on the right track for doing things a little more idiomatically with ```pytest```?\r\n\r\n```python\r\nimport pytest\r\n\r\nimport numpy as np\r\n\r\n\r\ndef netcdftime_date_types():\r\n    pytest.importorskip('netCDF4')\r\n\r\n    from netcdftime import (\r\n        DatetimeNoLeap, DatetimeJulian, DatetimeAllLeap,\r\n        DatetimeGregorian, DatetimeProlepticGregorian, Datetime360Day)\r\n    return [DatetimeNoLeap, DatetimeJulian, DatetimeAllLeap,\r\n            DatetimeGregorian, DatetimeProlepticGregorian, Datetime360Day]\r\n\r\n\r\n@pytest.fixture(params=[])\r\ndef index(request):\r\n    from xarray.core.netcdftimeindex import NetCDFTimeIndex\r\n\r\n    date_type = request.param\r\n    dates = [date_type(1, 1, 1), date_type(1, 2, 1),\r\n             date_type(2, 1, 1), date_type(2, 2, 1)]\r\n    return NetCDFTimeIndex(dates)\r\n\r\n\r\n@pytest.mark.parametrize('index', netcdftime_date_types(), indirect=True)\r\n@pytest.mark.parametrize(('field', 'expected'), [\r\n    ('year', [1, 1, 2, 2]),\r\n    ('month', [1, 2, 1, 2]),\r\n    ('day', [1, 1, 1, 1]),\r\n    ('hour', [0, 0, 0, 0]),\r\n    ('minute', [0, 0, 0, 0]),\r\n    ('second', [0, 0, 0, 0]),\r\n    ('microsecond', [0, 0, 0, 0])\r\n], ids=['year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond'])\r\ndef test_netcdftimeindex_field_accessors(index, field, expected):\r\n    result = getattr(index, field)\r\n    np.testing.assert_array_equal(result, expected)\r\n```"}}
{"actor":{"display_login":"spencerkclark"}}