Skip to content

Commit

Permalink
Consistency dataarray and dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller committed May 10, 2024
1 parent 778c265 commit 28b2672
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
13 changes: 9 additions & 4 deletions mikeio/dataset/_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ def __call__(self, tail: bool = True) -> "DataArray":
geometry = GeometryUndefined()

return DataArray(
data=Hm0, time=self.da.time, item=item, dims=dims, geometry=geometry, dt=self.da._dt
data=Hm0,
time=self.da.time,
item=item,
dims=dims,
geometry=geometry,
dt=self.da._dt,
)


Expand Down Expand Up @@ -423,11 +428,11 @@ def is_equidistant(self) -> bool:
return len(self.time.to_series().diff().dropna().unique()) == 1

@property
def timestep(self) -> float | None:
def timestep(self) -> float:
"""Time step in seconds if equidistant (and at
least two time instances); otherwise None
least two time instances); otherwise original time step is returned.
"""
dt = None
dt = self._dt
if len(self.time) > 1 and self.is_equidistant:
first: pd.Timestamp = self.time[0]
second: pd.Timestamp = self.time[1]
Expand Down
4 changes: 2 additions & 2 deletions mikeio/dataset/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ def end_time(self) -> datetime:
return self.time[-1].to_pydatetime() # type: ignore

@property
def timestep(self) -> float | None:
def timestep(self) -> float:
"""Time step in seconds if equidistant (and at
least two time instances); otherwise None
least two time instances); otherwise original time step is returned.
"""
dt = self._dt
if len(self.time) > 1 and self.is_equidistant:
Expand Down
1 change: 1 addition & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,7 @@ def test_select_single_timestep_preserves_dt():
assert ds.timestep == pytest.approx(1800.0)
ds2 = ds.isel(time=-1)
assert ds2.timestep == pytest.approx(1800.0)
assert ds2[0].timestep == pytest.approx(1800.0)


def test_select_multiple_spaced_timesteps_uses_proper_dt(tmp_path):
Expand Down

0 comments on commit 28b2672

Please sign in to comment.