Skip to content

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

Draft
aesgeorges wants to merge 2 commits into
mainfrom
BDS-1210-fix-typeError-in-read_schism_nc
Draft

Fix: pass ds_data as dictionary in read_schism_nc to circumvent xarray guard on xr.Dataset parameter#7
aesgeorges wants to merge 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.
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.

1 participant