8000 Fix set_index when an existing dimension becomes a level by fujiisoup · Pull Request #3520 · pydata/xarray · GitHub
[go: up one dir, main page]

Skip to content

Fix set_index when an existing dimension becomes a level #3520

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

Merged
merged 7 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension All 1 file type selected

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
black / mypy
  • Loading branch information
fujiisoup committed Nov 13, 2019
commit 8a034c0f23f82f85546f97fcbe4a98c5ff60e316
5 changes: 3 additions & 2 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,8 +1601,9 @@ def set_index(
--------
DataArray.reset_index
"""
ds = self._to_temp_dataset().set_index(indexes, append=append,
inplace=inplace, **indexes_kwargs)
ds = self._to_temp_dataset().set_index(
indexes, append=append, inplace=inplace, **indexes_kwargs
)
return self._from_temp_dataset(ds)

def reset_index(
Expand Down
7 changes: 4 additions & 3 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def merge_indexes(
"""
vars_to_replace: Dict[Hashable, Variable] = {}
vars_to_remove: List[Hashable] = []
dims_to_replace: Dict[Hashable, Variable] = {}
dims_to_replace: Dict[Hashable, Hashable] = {}
error_msg = "{} is not the name of an existing variable."

for dim, var_names in indexes.items():
Expand Down Expand Up @@ -270,8 +270,9 @@ def merge_indexes(
for k, v in new_variables.items():
if any(d in dims_to_replace for d in v.dims):
new_dims = [dims_to_replace.get(d, d) for d in v.dims]
new_variables[k] = type(v)(new_dims, v.data, attrs=v.attrs,
encoding=v.encoding, fastpath=True)
new_variables[k] = type(v)(
new_dims, v.data, attrs=v.attrs, encoding=v.encoding, fastpath=True
)
new_coord_names = coord_names | set(vars_to_replace)
new_coord_names -= set(vars_to_remove)
return new_variables, new_coord_names
Expand Down
12 changes: 6 additions & 6 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,12 +1184,12 @@ def test_selection_multiindex_remove_unused(self):

def test_selection_multiindex_from_level(self):
# GH: 3512
da = DataArray([0, 1], dims=['x'], coords={'x': [0, 1], 'y': 'a'})
db = DataArray([2, 3], dims=['x'], coords={'x': [0, 1], 'y': 'b'})
data = xr.concat([da, db], dim='x').set_index(xy=['x', 'y'])
assert data.dims == ('xy', )
actual = data.sel(y='a')
expected = data.isel(xy=[0, 1]).unstack('xy').squeeze('y').drop('y')
da = DataArray([0, 1], dims=["x"], coords={"x": [0, 1], "y": "a"})
db = DataArray([2, 3], dims=["x"], coords={"x": [0, 1], "y": "b"})
data = xr.concat([da, db], dim="x").set_index(xy=["x", "y"])
assert data.dims == ("xy",)
actual = data.sel(y="a")
expected = data.isel(xy=[0, 1]).unstack("xy").squeeze("y").drop("y")
assert_equal(actual, expected)

def test_virtual_default_coords(self):
Expand Down
0