Skip to content

Commit 4d8725c

Browse files
authored
Merge branch 'main' into pfs_filename_comma
2 parents 0bf3513 + 090c07a commit 4d8725c

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-2
lines changed

docs/index.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
toc: false
3+
format-links: false
34
---
45

56
![](MIKE-IO-Logo-Pos-RGB.svg)

docs/user-guide/dataarray.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ Other methods that also return a DataArray:
175175
* [`interp_like`](`mikeio.DataArray.interp_like`) - Spatio (temporal) interpolation (see example [Dfsu interpolation](../examples/dfsu/spatial_interpolation.qmd)
176176
* [`interp_time()`](`mikeio.DataArray.interp_time`) - Temporal interpolation (see example [Time interpolation](../examples/Time-interpolation.qmd))
177177
* [`dropna()`](`mikeio.DataArray.dropna`) - Remove time steps where all items are NaN
178+
* [`fillna()`](`mikeio.DataArray.fillna`) - Fill missing values with a constant value
178179
* [`squeeze()`](`mikeio.DataArray.squeeze`) - Remove axes of length 1
179180

180181
### Conversion:

docs/user-guide/dataset.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ Other methods that also return a Dataset:
177177
* [`interp_like`](`mikeio.Dataset.interp_like`) - Spatio (temporal) interpolation (see [Dfsu interpolation notebook](../examples/dfsu/spatial_interpolation.qmd)
178178
* [`interp_time()`](`mikeio.Dataset.interp_time`) - Temporal interpolation (see [Time interpolation notebook](../examples/Time-interpolation.qmd))
179179
* [`dropna()`](`mikeio.Dataset.dropna`) - Remove time steps where all items are NaN
180+
* [`fillna()`](`mikeio.Dataset.fillna`) - Fill missing values with a constant value
180181
* [`squeeze()`](`mikeio.Dataset.squeeze`) - Remove axes of length 1
181182

182183
### Conversion:

mikeio/dataset/_dataarray.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,34 @@ def to_numpy(self) -> np.ndarray:
484484
def _has_time_axis(self) -> bool:
485485
return self.dims[0][0] == "t"
486486

487+
def fillna(self, value: float = 0.0) -> "DataArray":
488+
"""Fill NA/NaN value.
489+
490+
Parameters
491+
----------
492+
value: float, optional
493+
Value used to fill missing values. Default is 0.0.
494+
495+
Examples
496+
--------
497+
```{python}
498+
import numpy as np
499+
import mikeio
500+
501+
da = mikeio.DataArray([np.nan, 1.0])
502+
da
503+
```
504+
505+
```{python}
506+
da.fillna(0.0)
507+
```
508+
509+
"""
510+
da = self.copy()
511+
x = da.values
512+
x[np.isnan(x)] = value
513+
return da
514+
487515
def dropna(self) -> "DataArray":
488516
"""Remove time steps where values are NaN."""
489517
if not self._has_time_axis:

mikeio/dataset/_dataset.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def _parse_items(
202202
) -> list[ItemInfo]:
203203
if items is None:
204204
# default Undefined items
205-
item_infos = [ItemInfo(f"Item_{j+1}") for j in range(n_items_data)]
205+
item_infos = [ItemInfo(f"Item_{j + 1}") for j in range(n_items_data)]
206206
else:
207207
if len(items) != n_items_data:
208208
raise ValueError(
@@ -416,6 +416,19 @@ def copy(self) -> "Dataset":
416416
"""Returns a copy of this dataset."""
417417
return deepcopy(self)
418418

419+
def fillna(self, value: float = 0.0) -> "Dataset":
420+
"""Fill NA/NaN value.
421+
422+
Parameters
423+
----------
424+
value: float, optional
425+
Value used to fill missing values. Default is 0.0.
426+
427+
"""
428+
res = {name: da.fillna(value=value) for name, da in self._data_vars.items()}
429+
430+
return Dataset(data=res, validate=False)
431+
419432
def dropna(self) -> "Dataset":
420433
"""Remove time steps where all items are NaN."""
421434
if not self[0]._has_time_axis: # type: ignore

tests/test_dataset.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ def test_create_dataset_with_many_items():
15331533

15341534
for i in range(n_items):
15351535
x = np.random.random(nt)
1536-
da = mikeio.DataArray(data=x, time=time, item=mikeio.ItemInfo(f"Item {i+1}"))
1536+
da = mikeio.DataArray(data=x, time=time, item=mikeio.ItemInfo(f"Item {i + 1}"))
15371537
das.append(da)
15381538

15391539
ds = mikeio.Dataset(das)
@@ -1636,3 +1636,23 @@ def test_read_write_single_timestep_preserves_dt(tmp_path):
16361636

16371637
dfs2 = mikeio.open(outfn)
16381638
assert dfs2.timestep == pytest.approx(10800.0)
1639+
1640+
1641+
def test_fillna() -> None:
1642+
ds = mikeio.Dataset(
1643+
{
1644+
"foo": mikeio.DataArray(np.array([np.nan, 1.0])),
1645+
"bar": mikeio.DataArray(np.array([2.0, np.nan])),
1646+
"baz": mikeio.DataArray(np.array([2.0, 3.0])),
1647+
}
1648+
)
1649+
assert np.isnan(ds["foo"].to_numpy()[0])
1650+
assert np.isnan(ds["bar"].to_numpy()[-1])
1651+
1652+
ds_filled = ds.fillna()
1653+
1654+
assert ds_filled["foo"].to_numpy()[0] == pytest.approx(0.0)
1655+
assert ds_filled["bar"].to_numpy()[-1] == pytest.approx(0.0)
1656+
1657+
# original dataset is not modified
1658+
assert np.isnan(ds["foo"].to_numpy()[0])

0 commit comments

Comments
 (0)