issue_comments: 919822643
This data as json
html_url | issue_url | id | node_id | user | created_at | updated_at | author_association | body | reactions | performed_via_github_app | issue |
---|---|---|---|---|---|---|---|---|---|---|---|
https://github.com/pydata/xarray/pull/5692#issuecomment-919822643 | https://api.github.com/repos/pydata/xarray/issues/5692 | 919822643 | IC_kwDOAMm_X84202Ez | 4160723 | 2021-09-15T08:45:00Z | 2021-09-15T08:45:00Z | MEMBER | This PR introduces some minor changes in behavior (no API change), mostly related to somewhat tricky workarounds to the limitations of the “index/dimension” coordinates concept that we no longer need with explicit indexes. I'll detail them below. A couple of those changes are bug fixes so we can safely make them now. For the other changes I'm not sure how best to proceed. Keeping the current behavior may in some cases require additional implementation effort that I'm not sure it's worth doing if no one relies on this weird behavior. Any thoughts @pydata/xarray?
1.1 Indexes are now preserved when renaming dimensions or coordinates ```python ds = xr.Dataset({"x": ("x", [0, 1, 2])}) renamed = ds.rename_dims({"x": "x_new"}) Before"x" in renamed.indexes # False Now"x" in renamed.indexes # True ```
2.1 Coordinate(s) dtype is preserved when setting new (multi-)indexes ```python da = xr.DataArray( [0, 1, 3, 4], dims="x", coords={"x": [0, 1, 0, 1], "y": ("x", ["a", "b", "a", "b"])} ) print(da) <xarray.DataArray (x: 4)>array([0, 1, 3, 4])Coordinates:* x (x) int64 0 1 0 1y (x) <U1 'a' 'b' 'a' 'b'
Beforeindexed.y.dtype # dtype('O') Nowindexed.y.dtype # dtype('<U1')
Beforeindexed.y.dtype # dtype('O') Nowindexed.y.dtype # dtype('<U1') ``` 2.2 Setting a new single index for a new dimension raises an error New dimension names allowed in ```python Before (bug)da.set_index(y="x") <xarray.DataArray (x: 4)>array([0, 1, 2, 3])Coordinates:* y (y) int64 0 1 0 1Dimensions without coordinates: xNowda.set_index(y="x") ValueError: try setting an index for dimension 'y'with variable 'x' that has dimensions ('x',)```
3.1 Coordinate (level) names are preserved when (partially) resetting a multi-index ```python indexed = da.set_index(xy=["x", "y"]) indexed <xarray.DataArray (xy: 4)>array([0, 1, 2, 3])Coordinates:* xy (xy) object MultiIndex* x (xy) int64 0 1 0 1* y (xy) <U1 'a' 'a' 'b' 'b'``` ```python Beforeindexed.reset_index("x") <xarray.DataArray (xy: 4)>array([0, 1, 2, 3])Coordinates:* xy (xy) object 'a' 'a' 'b' 'b'x (xy) int64 0 1 0 1Nowindexed.reset_index("x") <xarray.DataArray (xy: 4)>array([0, 1, 2, 3])Coordinates:xy (xy) object MultiIndexx (xy) int64 0 1 0 1* y (xy) <U1 'a' 'a' 'b' 'b'``` (note: the 3.2 Single index coordinates that are reset (but kept) are not renamed ```python Beforeda.reset_index("x") <xarray.DataArray (x: 4)>array([0, 1, 2, 3])Coordinates:y (x) <U1 'a' 'a' 'b' 'b'x_ (x) int64 0 1 0 1Dimensions without coordinates: xNowda.reset_index("x") <xarray.DataArray (x: 4)>array([0, 1, 2, 3])Coordinates:x (x) int64 0 1 0 1y (x) <U1 'a' 'a' 'b' 'b'``` 3.3 Fix bug when trying to reset a non-dimension (and non-level) coordinate ```python Beforeda.reset_index("y") <xarray.DataArray (x: 4)>array([0, 1, 2, 3])Coordinates:* x (x) int64 0 1 0 1y_ (y) object 'a' 'a' 'b' 'b'Nowda.reset_index("y") ValueError: ('y',) are not coordinates with an index``` |
{ "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
966983801 |