Skip to content

Commit 4485d53

Browse files
authored
Replace deprecated xarray .drop() and Dataset.dims[...] (#2734)
Clears the FutureWarnings that originate in Parcels' own code for two deprecated xarray patterns, ahead of enabling fail-on-warning (#2413): - .drop([...]) / .drop("...") -> .drop_vars(...) - Dataset.dims[name] / .dims.items() / .dims.get() -> .sizes... Leaves positional DataArray.dims[0] access untouched, since only the Dataset.dims mapping-by-name is deprecated. Towards #2682. Signed-off-by: Mike German <mike@stepsventures.com>
1 parent 953988b commit 4485d53

6 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/parcels/_datasets/remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ def open_dataset(self) -> xr.Dataset:
184184
def _preprocess_drop_time_from_mesh1(ds: xr.Dataset) -> xr.Dataset:
185185
# For some reason on the mesh "NemoNorthSeaORCA025-N006_data/coordinates.nc" there are two time dimensions (of length 1). These dimension also has broken cf-time metadata
186186
# this fixes that
187-
return ds.isel(time=0).drop(["time", "time_steps"])
187+
return ds.isel(time=0).drop_vars(["time", "time_steps"])
188188

189189

190190
def _preprocess_drop_time_from_mesh2(ds: xr.Dataset) -> xr.Dataset:
191191
# For some reason on the mesh "NemoCurvilinear_data_zonal/mesh_mask" there is a time dimension.
192-
return ds.isel(time=0).drop(["time"])
192+
return ds.isel(time=0).drop_vars(["time"])
193193

194194

195195
def _preprocess_set_cf_calendar_360_day(ds: xr.Dataset) -> xr.Dataset:

src/parcels/_datasets/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def verbose_print(*args, **kwargs):
123123
for dim_name in ds1_dims.intersection(ds2_dims):
124124
verbose_print(f" Dimension '{dim_name}':")
125125
# Sizes will differ due to DIM_SIZE, so we don't strictly compare them.
126-
verbose_print(f" {ds1_name} size: {ds1.dims[dim_name]}, {ds2_name} size: {ds2.dims[dim_name]}")
126+
verbose_print(f" {ds1_name} size: {ds1.sizes[dim_name]}, {ds2_name} size: {ds2.sizes[dim_name]}")
127127
# Check if coordinates associated with dimensions are sorted (increasing)
128128
if dim_name in ds1.coords and dim_name in ds2.coords:
129129
check_val = (

src/parcels/_sgrid/accessor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ def assert_metadata_ds_consistency(ds: xr.Dataset, metadata: SGrid2DMetadata):
9898
face, node, padding = obj.face, obj.node, obj.padding
9999

100100
try:
101-
n_nodes = ds.dims[node]
101+
n_nodes = ds.sizes[node]
102102
except KeyError: # node dimension is not in this dataset
103103
continue
104104
try:
105-
n_faces = ds.dims[face]
105+
n_faces = ds.sizes[face]
106106
except KeyError: # face dimension is not in this dataset
107107
continue
108108

src/parcels/convert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def nemo_to_sgrid(*, fields: dict[str, xr.Dataset | xr.DataArray], coords: xr.Da
319319
if "time" in coords.dims:
320320
if coords.sizes["time"] != 1:
321321
raise ValueError("Time dimension in coords must be length 1 (i.e., no time-varying grid).")
322-
coords = coords.isel(time=0).drop("time")
322+
coords = coords.isel(time=0).drop_vars("time")
323323

324324
if (
325325
len(coords.dims) == 3

tests/sgrid/test_accessor.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ def test_isel(ds, indexer, node_dim, face_dim):
151151
assert_metadata_ds_consistency(ds_trimmed, metadata)
152152

153153
# Assert that other dims haven't been affected
154-
for dim, size_before in ds.dims.items():
154+
for dim, size_before in ds.sizes.items():
155155
if dim in (node_dim, face_dim):
156156
continue
157-
size_after = ds_trimmed.dims[dim]
157+
size_after = ds_trimmed.sizes[dim]
158158
assert size_before == size_after
159159

160160

@@ -167,8 +167,8 @@ def test_isel_drop_dim(ds):
167167
assert "node_dimension1" not in ds_trimmed.dims
168168
assert "face_dimension1" not in ds_trimmed.dims
169169

170-
for dim, size_after in ds_trimmed.dims.items():
171-
size_before = ds.dims[dim]
170+
for dim, size_after in ds_trimmed.sizes.items():
171+
size_before = ds.sizes[dim]
172172
assert size_before == size_after
173173

174174

@@ -201,7 +201,7 @@ def test_isel_p1_consistency_invariant(ds, s):
201201
fnp = metadata.face_dimensions[0]
202202
node_dim = fnp.node
203203
assume(node_dim in ds.dims)
204-
n_nodes = ds.dims[node_dim]
204+
n_nodes = ds.sizes[node_dim]
205205
assume(len(range(*s.indices(n_nodes))) > 0) # exclude empty selections
206206

207207
result = ds.sgrid.isel({node_dim: s})
@@ -219,7 +219,7 @@ def test_isel_p2_data_correctness(ds, s):
219219
fnp = metadata.face_dimensions[0]
220220
node_dim, face_dim = fnp.node, fnp.face
221221
assume(node_dim in ds.dims)
222-
n_nodes = ds.dims[node_dim]
222+
n_nodes = ds.sizes[node_dim]
223223
assume(len(range(*s.indices(n_nodes))) > 0)
224224

225225
result = ds.sgrid.isel({node_dim: s})
@@ -249,14 +249,14 @@ def test_isel_p3_specification_symmetry(ds, s):
249249
fnp = metadata.face_dimensions[0]
250250
node_dim, face_dim = fnp.node, fnp.face
251251
assume(node_dim in ds.dims and face_dim in ds.dims)
252-
n_nodes = ds.dims[node_dim]
252+
n_nodes = ds.sizes[node_dim]
253253
assume(len(range(*s.indices(n_nodes))) > 0)
254254

255255
from parcels._sgrid.accessor import _derive_paired_indexer
256256

257257
_, face_indexer = _derive_paired_indexer(s, indexer_is_node=True, padding=fnp.padding, dim_size=n_nodes)
258258

259-
n_faces = ds.dims.get(face_dim)
259+
n_faces = ds.sizes.get(face_dim)
260260
if n_faces is not None:
261261
assume(len(range(*face_indexer.indices(n_faces))) > 0)
262262

tests/test_xgrid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def test_dim_with_duplicate_axis():
189189
@pytest.mark.skip("remove: see comment above")
190190
@pytest.mark.parametrize("ds", [datasets["ds_2d_left"]])
191191
def test_vertical1D_field(ds):
192-
ds = ds.drop(set(ds.data_vars) - {"grid"})
192+
ds = ds.drop_vars(set(ds.data_vars) - {"grid"})
193193
ds["depth"] = (["ZG"], np.linspace(0, 1, ds["depth"].size), {"axis": "Z"})
194194
ds["z1d"] = xr.DataArray(np.linspace(0, 10, ds["depth"].size), dims=("ZG",))
195195
ds = ds.reset_coords("z1d")

0 commit comments

Comments
 (0)