Skip to content

Fix: pass ds_data as dictionary in read_schism_nc to circumvent xarray guard on xr.Dataset parameter#7

Merged
kjnam merged 2 commits into
mainfrom
BDS-1210-fix-typeError-in-read_schism_nc
Jul 16, 2026
Merged

Fix: pass ds_data as dictionary in read_schism_nc to circumvent xarray guard on xr.Dataset parameter#7
kjnam merged 2 commits into
mainfrom
BDS-1210-fix-typeError-in-read_schism_nc

Conversation

@aesgeorges

Copy link
Copy Markdown
Contributor

Calling sx.read_schism_nc(grid, ds_data) raises:

TypeError: Passing a Dataset as `data_vars` to the Dataset constructor is not supported.
Use `ds.copy()` to create a copy of a Dataset.

Root

In read_schism_nc, ds_data (an xr.Dataset) is passed as positional argument to SxDataset which forwards it via *args through ux.UxDataset.__init__ down to xr.Dataset.__init__, where it lands in the data_vars parameter.

Newer versions of xarray (here 2026.4.0) added an explicit isinstance guard:

if isinstance(data_vars, Dataset):
    raise TypeError(...)

The error message is misleading however, as using ds_data.copy() instead in read_schism_nc creates another xr.Dataset, triggering the isinstance guard anyway.

Fix

Passing the data as a dictionary circumvents the guard, with dimension coordinates (such as time) being preserved.

@aesgeorges
aesgeorges requested a review from kjnam July 7, 2026 17:39
Building the SxDataset from ``dict(ds_data.data_vars)`` drops the source Dataset's global attributes unless they are forwarded explicitly. This forwards the attributes and test makes sure attrs exist and come back the same.
@aesgeorges
aesgeorges marked this pull request as ready for review July 14, 2026 15:45
@kjnam
kjnam merged commit ac82ac2 into main Jul 16, 2026
2 checks passed
@aesgeorges
aesgeorges deleted the BDS-1210-fix-typeError-in-read_schism_nc branch July 16, 2026 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants