{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/xarray/issues/1046","repository_url":"https://api.github.com/repos/pydata/xarray","labels_url":"https://api.github.com/repos/pydata/xarray/issues/1046/labels{/name}","comments_url":"https://api.github.com/repos/pydata/xarray/issues/1046/comments","events_url":"https://api.github.com/repos/pydata/xarray/issues/1046/events","html_url":"https://github.com/pydata/xarray/issues/1046","id":182667672,"number":1046,"title":"center=True for xarray.DataArray.rolling()","user":{"login":"chunweiyuan","id":5572303,"avatar_url":"https://avatars.githubusercontent.com/u/5572303?v=3","gravatar_id":"","url":"https://api.github.com/users/chunweiyuan","html_url":"https://github.com/chunweiyuan","followers_url":"https://api.github.com/users/chunweiyuan/followers","following_url":"https://api.github.com/users/chunweiyuan/following{/other_user}","gists_url":"https://api.github.com/users/chunweiyuan/gists{/gist_id}","starred_url":"https://api.github.com/users/chunweiyuan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chunweiyuan/subscriptions","organizations_url":"https://api.github.com/users/chunweiyuan/orgs","repos_url":"https://api.github.com/users/chunweiyuan/repos","events_url":"https://api.github.com/users/chunweiyuan/events{/privacy}","received_events_url":"https://api.github.com/users/chunweiyuan/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/pydata/xarray/labels/design%20question","name":"design question","color":"cc317c"},{"url":"https://api.github.com/repos/pydata/xarray/labels/pandas-like","name":"pandas-like","color":"5319e7"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2016-10-13T00:37:25Z","updated_at":"2016-10-20T22:32:23Z","closed_at":null,"body":"The logic behind setting center=True confuses me.  Say window size = 3.  The default behavior (center=False) sets the window to go from i-2 to i, so I would've expected center=True to set the window from i-1 to i+1.  But that's not what I see.\r\n\r\nFor example, this is what data looks like:\r\n\r\n    >>>data\r\n    xarray.DataArray (x: 3, y: 3, z: 3),\r\n    array([[[ 0,  1,  2],\r\n            [ 3,  4,  5],\r\n            [ 6,  7,  8]],\r\n\r\n           [[ 9, 10, 11],\r\n            [12, 13, 14],\r\n            [15, 16, 17]],\r\n\r\n           [[18, 19, 20],\r\n            [21, 22, 23],\r\n            [24, 25, 26]]])\r\n    Coordinates:\r\n      * x        (x) |S1 'a' 'b' 'c'\r\n      * y        (y) int64 -2 0 2\r\n      * z        (z) int64 0 1 2\r\n\r\nNow, if I set y-window size = 3, center = False, min # of entries = 1, I get\r\n\r\n    >>> r = data.rolling(y=3, center=False, min_periods=1)\r\n    >>> r.mean()\r\n    <xarray.DataArray (x: 3, y: 3, z: 3)>\r\n    array([[[  0. ,   1. ,   2. ],\r\n            [  1.5,   2.5,   3.5],\r\n            [  3. ,   4. ,   5. ]],\r\n\r\n           [[  9. ,  10. ,  11. ],\r\n            [ 10.5,  11.5,  12.5],\r\n            [ 12. ,  13. ,  14. ]],\r\n\r\n           [[ 18. ,  19. ,  20. ],\r\n            [ 19.5,  20.5,  21.5],\r\n            [ 21. ,  22. ,  23. ]]])\r\n    Coordinates:\r\n      * x        (x) |S1 'a' 'b' 'c'\r\n      * y        (y) int64 -2 0 2\r\n      * z        (z) int64 0 1 2\r\n\r\nWhich essentially gives me a \"trailing window\" of size 3, meaning the window goes from i-2 to i.  This is not explained in the doc but can be understood empirically.\r\n\r\nOn the other hand, setting center = True gives\r\n\r\n    >>> r = data.rolling(y=3, center=True, min_periods=1)\r\n    >>> r.mean()\r\n    <xarray.DataArray (x: 3, y: 3, z: 3)>\r\n    array([[[  1.5,   2.5,   3.5],\r\n            [  3. ,   4. ,   5. ],\r\n            [  nan,   nan,   nan]],\r\n\r\n           [[ 10.5,  11.5,  12.5],\r\n            [ 12. ,  13. ,  14. ],\r\n            [  nan,   nan,   nan]],\r\n\r\n           [[ 19.5,  20.5,  21.5],\r\n            [ 21. ,  22. ,  23. ],\r\n            [  nan,   nan,   nan]]])\r\n    Coordinates:\r\n      * x        (x) |S1 'a' 'b' 'c'\r\n      * y        (y) int64 -2 0 2\r\n      * z        (z) int64 0 1 2\r\n\r\nIn other words, it just pushes every cell up the y-dim by 1, using nan to represent things coming off the edge of the universe.  If you look at _center_result() of xarray/core/rolling.py, that's exactly what it does with .shift().\r\n\r\nI would've expected center=True to change the window to go from i-1 to i+1.  In which case, with min_periods=1, would not render any nan value in r.mean().\r\n\r\nCould someone explain the logical flow to me?\r\n\r\nMuch obliged,\r\n\r\nChun"},"comment":{"url":"https://api.github.com/repos/pydata/xarray/issues/comments/255246621","html_url":"https://github.com/pydata/xarray/issues/1046#issuecomment-255246621","issue_url":"https://api.github.com/repos/pydata/xarray/issues/1046","id":255246621,"user":{"login":"chunweiyuan","id":5572303,"avatar_url":"https://avatars.githubusercontent.com/u/5572303?v=3","gravatar_id":"","url":"https://api.github.com/users/chunweiyuan","html_url":"https://github.com/chunweiyuan","followers_url":"https://api.github.com/users/chunweiyuan/followers","following_url":"https://api.github.com/users/chunweiyuan/following{/other_user}","gists_url":"https://api.github.com/users/chunweiyuan/gists{/gist_id}","starred_url":"https://api.github.com/users/chunweiyuan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chunweiyuan/subscriptions","organizations_url":"https://api.github.com/users/chunweiyuan/orgs","repos_url":"https://api.github.com/users/chunweiyuan/repos","events_url":"https://api.github.com/users/chunweiyuan/events{/privacy}","received_events_url":"https://api.github.com/users/chunweiyuan/received_events","type":"User","site_admin":false},"created_at":"2016-10-20T22:32:23Z","updated_at":"2016-10-20T22:32:23Z","body":"Let me exhaust a few other ideas first.  I'll definitely share my thoughts here first before making any commit.  Thanks."}}
{"actor":{"display_login":"chunweiyuan"}}