Skip to content

Commit 957b61e

Browse files
authored
Fix fill_value handling during unstack (#10901)
* Use get method for fill_value in unstack_once * Add unit test for default fill_value in stack()
1 parent 0faa6b7 commit 957b61e

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

xarray/core/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5433,7 +5433,7 @@ def _unstack_once(
54335433
if name not in index_vars:
54345434
if dim in var.dims:
54355435
if isinstance(fill_value, Mapping):
5436-
fill_value_ = fill_value[name]
5436+
fill_value_ = fill_value.get(name, xrdtypes.NA)
54375437
else:
54385438
fill_value_ = fill_value
54395439

xarray/tests/test_dataset.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,6 +4150,10 @@ def test_unstack_fill_value(self) -> None:
41504150
expected3 = ds.unstack("index").fillna({"var": -1, "other_var": 1}).astype(int)
41514151
assert_equal(actual3, expected3)
41524152

4153+
actual4 = ds.unstack("index", fill_value={"var": -1})
4154+
expected4 = ds.unstack("index").fillna({"var": -1, "other_var": np.nan})
4155+
assert_equal(actual4, expected4)
4156+
41534157
@requires_sparse
41544158
def test_unstack_sparse(self) -> None:
41554159
ds = xr.Dataset(

0 commit comments

Comments
 (0)