Skip to content

Commit ff740af

Browse files
mathausedcherianpre-commit-ci[bot]
authored
allow custom criteria for name of DataArray (#379)
* allow custom coords name for DataArray * add whatsnew * Update cf_xarray/accessor.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c6d0a44 commit ff740af

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

cf_xarray/accessor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ def _get_custom_criteria(
214214
if re.match(patterns, obj[var].attrs.get(criterion, "")):
215215
results.update((var,))
216216
# also check name specifically since not in attributes
217-
elif criterion == "name" and re.match(patterns, var):
217+
elif (
218+
criterion == "name"
219+
and isinstance(var, str)
220+
and re.match(patterns, var)
221+
):
218222
results.update((var,))
219223
return list(results)
220224

cf_xarray/tests/test_accessor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,10 @@ def test_custom_criteria():
14981498
ds["salinity"] = ("dim", np.arange(10))
14991499
assert_identical(ds.cf["salt"], ds["salinity"])
15001500

1501+
# match by name of coords for DataArray (#378)
1502+
da = xr.DataArray([0, 1], coords={"salinity": [2, 3]})
1503+
assert_identical(da.cf["salt"], da["salinity"])
1504+
15011505
# Match by standard_name regex match
15021506
ds = xr.Dataset()
15031507
ds["elev"] = ("dim", np.arange(10), {"standard_name": "sea_surface_elevBLAH"})

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ What's New
66
v0.7.6 (unreleased)
77
===================
88

9-
9+
- Allow custom criteria to match the variable name of DataArray objects (:pr:`379`). By `Mathias Hauser`_
1010

1111
v0.7.5 (Nov 15, 2022)
1212
=====================
@@ -190,4 +190,5 @@ v0.1.3
190190
.. _`Lars Buntemeyer`: https://github.com/larsbuntemeyer
191191
.. _`Luke Davis`: https://github.com/lukelbd
192192
.. _`Martin Schupfner`: https://github.com/sol1105
193+
.. _`Mathias Hauser`: https://github.com/mathause
193194
.. _`flag variables`: http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#flags

0 commit comments

Comments
 (0)