Assign new variables by standard names
This pull request implements ds.cf.assign(**standard_variables). The new method should:
- [ ] Assign variables by standard name as
**kwargsor dictionary. - [ ] Decide on the variable short names without user input (see below).
- [ ] Either handle or reject duplicate standard names (see below).
- [ ] Workaround xarray incompatibilities, currently #513.
Also needed:
- [ ] Add documentation, where should it go?
- [ ] Add tests, including name duplicates.
I would welcome opinions on the two points below.
1. Decide about the variable short name
I propose this algorithm:
- If variable has a short name, absent from the dataset, use it.
ds.cf.assign(air_temperature=DataArray(name='tas')) -> 'tas'
- Otherwise, use the standard name as a short variable name.
ds.cf.assign(air_temperature=DataArray()) -> 'air_temperature'
- If name is already used in dataset, warn user, and add trailing underscores.
ds.cf.assign(
air_temperature=DataArray(name=tas)).cf.assign(
total_precipitation=DataArray(name=tas)) -> tas, tas_
- If name is taken by another variable in call, also add trailing underscores.
ds.cf.assign(
air_temperature=DataArray(name=tas),
total_precipitation=DataArray(name=tas)) -> tas, tas_
2. When dataset already contains standard name
ds = xr.Dataset()
ds = ds.cf.assign(air_temperature=0)
ds = ds.cf.assign(air_temperature=1)
I find it more difficult to decide what the method should do here.
- Override existing variable (same as
Dataset.assign). - Override but raise a warning.
- Assign new variable with same standard name.
- Assign new variable but raise a warning.
- Customize via keyword e.g.
existing: ignore, override, raise, warn
Note: if we allow multiple variables with the same standard name, the resulting Dataset is technically valid, and ds.cf shows several variables associated with one standard name, while ds.cf[standard_name] fails with a KeyError`.
I think the cf-xarray version of this only really makes sense when the assigned name is a standard name on one of the present variables, so (2) in your listing.
For (1), we should just forward on to Xarray, as usual.
if we allow multiple variables with the same standard name, the resulting Dataset is technically valid, and ds.cf shows several variables associated with one standard name, while ds.cf[standard_name] fails with a KeyError`
Yes, this is intentional. ds.cf[standard_name] will raise an error unless there is only one result, since that is the only way to return a DataArray. to get all, use ds.cf[[standard_name]]. Then you will get a dataset with all dataarrays with that standard name.
Codecov Report
Attention: Patch coverage is 23.07692% with 10 lines in your changes missing coverage. Please review.
Project coverage is 85.39%. Comparing base (
a9cebee) to head (e156aef). Report is 60 commits behind head on main.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| cf_xarray/accessor.py | 23.07% | 8 Missing and 2 partials :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## main #516 +/- ##
==========================================
- Coverage 85.78% 85.39% -0.39%
==========================================
Files 13 13
Lines 2364 2623 +259
Branches 183 241 +58
==========================================
+ Hits 2028 2240 +212
- Misses 303 341 +38
- Partials 33 42 +9
| Flag | Coverage Δ | |
|---|---|---|
| mypy | 41.07% <15.38%> (+2.54%) |
:arrow_up: |
| unittests | 93.09% <9.09%> (-0.90%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.