File tree Expand file tree Collapse file tree 6 files changed +66
-2
lines changed Expand file tree Collapse file tree 6 files changed +66
-2
lines changed Original file line number Diff line number Diff line change 1
1
---
2
2
toc : false
3
+ format-links : false
3
4
---
4
5
5
6
![ ] ( MIKE-IO-Logo-Pos-RGB.svg )
Original file line number Diff line number Diff line change @@ -175,6 +175,7 @@ Other methods that also return a DataArray:
175
175
* [ ` interp_like ` ] ( `mikeio.DataArray.interp_like` ) - Spatio (temporal) interpolation (see example [ Dfsu interpolation] ( ../examples/dfsu/spatial_interpolation.qmd )
176
176
* [ ` interp_time() ` ] ( `mikeio.DataArray.interp_time` ) - Temporal interpolation (see example [ Time interpolation] ( ../examples/Time-interpolation.qmd ) )
177
177
* [ ` dropna() ` ] ( `mikeio.DataArray.dropna` ) - Remove time steps where all items are NaN
178
+ * [ ` fillna() ` ] ( `mikeio.DataArray.fillna` ) - Fill missing values with a constant value
178
179
* [ ` squeeze() ` ] ( `mikeio.DataArray.squeeze` ) - Remove axes of length 1
179
180
180
181
### Conversion:
Original file line number Diff line number Diff line change @@ -177,6 +177,7 @@ Other methods that also return a Dataset:
177
177
* [ ` interp_like ` ] ( `mikeio.Dataset.interp_like` ) - Spatio (temporal) interpolation (see [ Dfsu interpolation notebook] ( ../examples/dfsu/spatial_interpolation.qmd )
178
178
* [ ` interp_time() ` ] ( `mikeio.Dataset.interp_time` ) - Temporal interpolation (see [ Time interpolation notebook] ( ../examples/Time-interpolation.qmd ) )
179
179
* [ ` dropna() ` ] ( `mikeio.Dataset.dropna` ) - Remove time steps where all items are NaN
180
+ * [ ` fillna() ` ] ( `mikeio.Dataset.fillna` ) - Fill missing values with a constant value
180
181
* [ ` squeeze() ` ] ( `mikeio.Dataset.squeeze` ) - Remove axes of length 1
181
182
182
183
### Conversion:
Original file line number Diff line number Diff line change @@ -484,6 +484,34 @@ def to_numpy(self) -> np.ndarray:
484
484
def _has_time_axis (self ) -> bool :
485
485
return self .dims [0 ][0 ] == "t"
486
486
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
+
487
515
def dropna (self ) -> "DataArray" :
488
516
"""Remove time steps where values are NaN."""
489
517
if not self ._has_time_axis :
Original file line number Diff line number Diff line change @@ -202,7 +202,7 @@ def _parse_items(
202
202
) -> list [ItemInfo ]:
203
203
if items is None :
204
204
# 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 )]
206
206
else :
207
207
if len (items ) != n_items_data :
208
208
raise ValueError (
@@ -416,6 +416,19 @@ def copy(self) -> "Dataset":
416
416
"""Returns a copy of this dataset."""
417
417
return deepcopy (self )
418
418
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
+
419
432
def dropna (self ) -> "Dataset" :
420
433
"""Remove time steps where all items are NaN."""
421
434
if not self [0 ]._has_time_axis : # type: ignore
Original file line number Diff line number Diff line change @@ -1533,7 +1533,7 @@ def test_create_dataset_with_many_items():
1533
1533
1534
1534
for i in range (n_items ):
1535
1535
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 } " ))
1537
1537
das .append (da )
1538
1538
1539
1539
ds = mikeio .Dataset (das )
@@ -1636,3 +1636,23 @@ def test_read_write_single_timestep_preserves_dt(tmp_path):
1636
1636
1637
1637
dfs2 = mikeio .open (outfn )
1638
1638
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 ])
You can’t perform that action at this time.
0 commit comments