Skip to content

Commit

Permalink
Merge pull request #642 from DHI/fix-pandas-deprecations
Browse files Browse the repository at this point in the history
Fix-pandas-deprecations
  • Loading branch information
ecomodeller authored Feb 2, 2024
2 parents cefdef3 + b9dd850 commit f6037d9
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 104 deletions.
6 changes: 2 additions & 4 deletions mikeio/dataset/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,11 @@ def _del_name_attr(self, name: str) -> None:
delattr(self, name)

@overload
def __getitem__(self, key: Hashable | int) -> DataArray:
...
def __getitem__(self, key: Hashable | int) -> DataArray: ...

# Mapping is Iterable
@overload
def __getitem__(self, key: Iterable[Hashable]) -> "Dataset":
...
def __getitem__(self, key: Iterable[Hashable]) -> "Dataset": ...

def __getitem__(self, key) -> DataArray | "Dataset":
# select time steps
Expand Down
2 changes: 2 additions & 0 deletions mikeio/dfs/_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ def time(self) -> pd.DatetimeIndex:
# this will fail if the TimeAxisType is not calendar and equidistant, but that is ok
if not self._is_equidistant:
raise NotImplementedError("Not implemented for non-equidistant files")

# TODO using 'S' works in Python 3.8, but is deprecated, 's' doesn't work in 3.8
return pd.date_range(
start=self.start_time, periods=self.n_timesteps, freq=f"{self.timestep}S"
)
Expand Down
32 changes: 21 additions & 11 deletions mikeio/dfs/_dfs0.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _write_dfs0(filename, dataset: Dataset, title="", dtype=DfsSimpleType.Float)


class Dfs0:
def __init__(self, filename:str | Path):
def __init__(self, filename: str | Path):
"""Create a Dfs0 object for reading, writing
Parameters
Expand Down Expand Up @@ -148,7 +148,7 @@ def read(self, items=None, time=None, keepdims=False) -> Dataset:
-------
Dataset
A Dataset with data dimensions [t]
"""
"""
path = Path(self._filename)
if not path.exists():
raise FileNotFoundError(f"File {path} not found")
Expand Down Expand Up @@ -244,7 +244,15 @@ def _to_dfs_datatype(dtype):
raise TypeError("Dfs files only support float or double")

@staticmethod
def _setup_header(title:str, filename:str, start_time, dt:float, is_equidistant: bool, dtype, items: Sequence[ItemInfo]):
def _setup_header(
title: str,
filename: str,
start_time,
dt: float,
is_equidistant: bool,
dtype,
items: Sequence[ItemInfo],
):
factory = DfsFactory()
builder = DfsBuilder.Create(title, "mikeio", __dfs_version__)
builder.SetDataType(1)
Expand Down Expand Up @@ -375,15 +383,14 @@ def write(
self._dt = float(self._dt)
t_seconds = self._dt * np.arange(float(self._n_timesteps))


dfs = self._setup_header(
title=self._title,
filename=self._filename,
dt = self._dt,
start_time = self._start_time,
is_equidistant = self._is_equidistant,
filename=self._filename,
dt=self._dt,
start_time=self._start_time,
is_equidistant=self._is_equidistant,
dtype=self._dtype,
items=self._items
items=self._items,
)

delete_value = dfs.FileInfo.DeleteValueFloat
Expand Down Expand Up @@ -471,7 +478,7 @@ def start_time(self):

@cached_property
def end_time(self):

if self._source.FileInfo.TimeAxis.IsEquidistant():
dt = self._source.FileInfo.TimeAxis.TimeStep
n_steps = self._source.FileInfo.TimeAxis.NumberOfTimeSteps
Expand Down Expand Up @@ -568,7 +575,10 @@ def dataframe_to_dfs0(
else:
items = [ItemInfo(name, itemtype, unit) for name in self.columns]

das = {item.name: DataArray(data=d, item=item, time=self.index) for d, item in zip(data, items)}
das = {
item.name: DataArray(data=d, item=item, time=self.index)
for d, item in zip(data, items)
}
ds = Dataset(das)
_write_dfs0(filename=filename, dataset=ds, title=title, dtype=dtype)

Expand Down
3 changes: 1 addition & 2 deletions mikeio/dfs/_dfs3.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ..spatial import Grid3D


def write_dfs3(filename: str| Path, ds: Dataset, title="") -> None:
def write_dfs3(filename: str | Path, ds: Dataset, title="") -> None:
dfs = _write_dfs3_header(filename, ds, title)
_write_dfs_data(dfs=dfs, ds=ds, n_spatial_dims=3)

Expand Down Expand Up @@ -142,7 +142,6 @@ def __init__(self, filename=None):
def __repr__(self):
out = ["<mikeio.Dfs3>"]


if self._filename:
out.append(f"geometry: {self.geometry}")

Expand Down
2 changes: 0 additions & 2 deletions mikeio/dfsu/_dfsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,6 @@ def _write(
"argument dt is deprecated, please supply data in the form of a Dataset",
FutureWarning,
)
if keep_open and not dt:
warnings.warn("argument dt missing, should be provided when keep_open=True")

filename = str(filename)

Expand Down
2 changes: 1 addition & 1 deletion mikeio/dfsu/_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _get_spectral_data_shape(self, n_steps: int, elements):
dims = [] if n_steps == 1 else ["time"]
n_freq = self.n_frequencies
n_dir = self.n_directions
shape: Tuple[int,...] = (n_dir, n_freq)
shape: Tuple[int, ...] = (n_dir, n_freq)
if n_dir == 0:
shape = (n_freq,)
elif n_freq == 0:
Expand Down
44 changes: 22 additions & 22 deletions tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def da0():
def da1():
nt = 10
start = 10.0
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)
da = mikeio.DataArray(
data=np.arange(start, start + nt, dtype=float),
time=time,
Expand All @@ -40,7 +40,7 @@ def da2():

da = mikeio.DataArray(
data=np.zeros([nt, nx]) + 0.1,
time=pd.date_range(start="2000-01-01", freq="S", periods=nt),
time=pd.date_range(start="2000-01-01", freq="s", periods=nt),
item=ItemInfo("Foo"),
geometry=mikeio.Grid1D(x0=1000.0, dx=10.0, nx=nx),
)
Expand All @@ -56,7 +56,7 @@ def da_grid2d():

da = mikeio.DataArray(
data=np.zeros([nt, ny, nx]) + 0.1,
time=pd.date_range(start="2000-01-01", freq="H", periods=nt),
time=pd.date_range(start="2000-01-01", freq="h", periods=nt),
item=ItemInfo("Foo"),
geometry=mikeio.Grid2D(x0=10.0, dx=0.1, nx=nx, ny=ny, dy=1.0, y0=-10.0),
)
Expand All @@ -72,7 +72,7 @@ def da_grid2d_proj():

da = mikeio.DataArray(
data=np.zeros([nt, ny, nx]) + 0.1,
time=pd.date_range(start="2000-01-01", freq="S", periods=nt),
time=pd.date_range(start="2000-01-01", freq="s", periods=nt),
item=ItemInfo("Foo"),
geometry=mikeio.Grid2D(
x0=1000, dx=100, nx=nx, ny=ny, dy=10, y0=2000, projection="UTM-32"
Expand All @@ -85,7 +85,7 @@ def da_grid2d_proj():
@pytest.fixture
def da_time_space():
nt = 10
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)
da = mikeio.DataArray(
data=np.zeros(shape=(nt, 2), dtype=float),
time=time,
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_verify_custom_dims():
with pytest.raises(ValueError) as excinfo:
mikeio.DataArray(
data=np.zeros([nt, nx]) + 0.1,
time=pd.date_range(start="2000-01-01", freq="S", periods=nt),
time=pd.date_range(start="2000-01-01", freq="s", periods=nt),
item=ItemInfo("Foo"),
dims=("space", "ensemble"), # no time!
geometry=mikeio.Grid1D(x0=1000.0, dx=10.0, nx=nx),
Expand All @@ -126,7 +126,7 @@ def test_verify_custom_dims():
with pytest.raises(ValueError) as excinfo:
mikeio.DataArray(
data=np.zeros([nt, nx]) + 0.1,
time=pd.date_range(start="2000-01-01", freq="S", periods=nt),
time=pd.date_range(start="2000-01-01", freq="s", periods=nt),
item=ItemInfo("Foo"),
dims=("time", "x", "ensemble"), # inconsistent with data
geometry=mikeio.Grid1D(x0=1000.0, dx=10.0, nx=nx),
Expand Down Expand Up @@ -173,7 +173,7 @@ def test_create_data_1d_default_grid():

da = mikeio.DataArray(
data=np.zeros((10, 5)),
time=pd.date_range(start="2000-01-01", freq="H", periods=10),
time=pd.date_range(start="2000-01-01", freq="h", periods=10),
item=ItemInfo("Foo"),
)
assert isinstance(da.geometry, mikeio.Grid1D)
Expand All @@ -188,7 +188,7 @@ def test_create_data_1d_default_grid():
# with pytest.warns(Warning) as w:
# mikeio.DataArray(
# data=np.zeros([nt, ny, nx]) + 0.1,
# time=pd.date_range(start="2000-01-01", freq="S", periods=nt),
# time=pd.date_range(start="2000-01-01", freq="s", periods=nt),
# item=ItemInfo("Foo"),
# )

Expand All @@ -199,7 +199,7 @@ def test_dataarray_init():
nt = 10
start = 10.0
data = np.arange(start, start + nt, dtype=float)
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)
item = ItemInfo(name="Foo")

da = mikeio.DataArray(data=data, time=time)
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_dataarray_init_no_item():

nt = 10
data = data = np.zeros([nt, 4]) + 0.1
time = time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = time = pd.date_range(start="2000-01-01", freq="s", periods=nt)

da = mikeio.DataArray(data=data, time=time)
assert da.type == EUMType.Undefined
Expand All @@ -239,7 +239,7 @@ def test_dataarray_init_no_item():

def test_dataarray_init_2d():
nt = 10
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)

# 2d with time
ny, nx = 5, 6
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_dataarray_init_2d():

def test_dataarray_init_5d():
nt = 10
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)

# 5d with named dimensions
dims = ("x", "y", "layer", "member", "season")
Expand All @@ -299,7 +299,7 @@ def test_dataarray_init_wrong_dim():
nt = 10
start = 10.0
data = np.arange(start, start + nt, dtype=float)
time_long = pd.date_range(start="2000-01-01", freq="S", periods=(nt + 1))
time_long = pd.date_range(start="2000-01-01", freq="s", periods=(nt + 1))
item = ItemInfo(name="Foo")

with pytest.raises(ValueError):
Expand All @@ -312,7 +312,7 @@ def test_dataarray_init_wrong_dim():

# time must be first dim
dims = ("x", "y", "time")
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)
with pytest.raises(ValueError):
mikeio.DataArray(data=data2d, time=time, dims=dims)

Expand All @@ -325,7 +325,7 @@ def test_dataarray_init_wrong_dim():
def test_dataarray_init_grid1d():
nt = 10
nx = 5
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)
data = np.zeros([nt, nx]) + 0.1
g = mikeio.Grid1D(nx=nx, dx=1.0)
da = mikeio.DataArray(data=data, time=time, geometry=g)
Expand All @@ -348,7 +348,7 @@ def test_dataarray_init_grid1d():
def test_dataarray_init_grid2d():
nt = 10
ny, nx = 7, 5
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)
data = np.zeros([nt, ny, nx]) + 0.1
g = mikeio.Grid2D(dx=0.5, nx=nx, ny=ny)
da = mikeio.DataArray(data=data, time=time, geometry=g)
Expand All @@ -370,7 +370,7 @@ def test_dataarray_init_grid2d():

def test_dataarray_init_dfsu2d():
nt = 10
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)
filename = "tests/testdata/north_sea_2.mesh"
msh = Mesh(filename)
g = msh.geometry
Expand Down Expand Up @@ -399,7 +399,7 @@ def test_dataarray_init_dfsu2d():

def test_dataarray_init_dfsu3d():
nt = 10
time = pd.date_range(start="2000-01-01", freq="S", periods=nt)
time = pd.date_range(start="2000-01-01", freq="s", periods=nt)
filename = "tests/testdata/basin_3d.dfsu"
dfs = mikeio.open(filename)
g = dfs.geometry
Expand Down Expand Up @@ -550,7 +550,7 @@ def test_dataarray_grid3d_indexing():

def test_dataarray_getitem_time(da_grid2d):
da = da_grid2d
# time=pd.date_range("2000-01-01", freq="H", periods=10)
# time=pd.date_range("2000-01-01", freq="h", periods=10)
da_sel = da["2000-1-1"]
assert da_sel.n_timesteps == da.n_timesteps
assert da_sel.is_equidistant
Expand Down Expand Up @@ -1224,7 +1224,7 @@ def test_write_dfs2(tmp_path):
assert g.origin == (0, 0)
da = mikeio.DataArray(
np.random.random(size=(nt, g.ny, g.nx)),
time=pd.date_range(start="2000", freq="H", periods=nt),
time=pd.date_range(start="2000", freq="h", periods=nt),
item=ItemInfo("Random"),
geometry=g,
)
Expand Down Expand Up @@ -1309,7 +1309,7 @@ def test_time_selection():
data = []
d = np.random.rand(nt)
data.append(d)
time = pd.date_range("2000-1-2", freq="H", periods=nt)
time = pd.date_range("2000-1-2", freq="h", periods=nt)
items = [ItemInfo("Foo")]
ds = mikeio.Dataset(data, time, items)

Expand Down
Loading

0 comments on commit f6037d9

Please sign in to comment.