Skip to content

Commit 2c3801d

Browse files
authored
Check attrs before printing guess (#331)
* check attrs before printing * explicit skip axes and coords * better var names * use one if * apply review and add what's new
1 parent 5dd53ea commit 2c3801d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

cf_xarray/accessor.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,8 @@ def guess_coord_axis(self, verbose: bool = False) -> DataArray | Dataset:
17321732
"""
17331733
obj = self._obj.copy(deep=True)
17341734
for var in obj.coords.variables:
1735-
if obj[var].ndim == 1 and _is_datetime_like(obj[var]):
1735+
var_is_coord = any(var in val for val in obj.cf.coordinates.values())
1736+
if not var_is_coord and obj[var].ndim == 1 and _is_datetime_like(obj[var]):
17361737
if verbose:
17371738
print(
17381739
f"I think {var!r} is of type 'time'. It has a datetime-like type."
@@ -1741,6 +1742,12 @@ def guess_coord_axis(self, verbose: bool = False) -> DataArray | Dataset:
17411742
continue # prevent second detection
17421743

17431744
for name, pattern in regex.items():
1745+
var_is_axis = any(var in val for val in obj.cf.axes.values())
1746+
var_is_coord = any(var in val for val in obj.cf.coordinates.values())
1747+
if (name in _AXIS_NAMES and var_is_axis) or (
1748+
name in _COORD_NAMES and var_is_coord
1749+
):
1750+
continue # skip known axes/coordinates and prevent multiple guesses
17441751
# match variable names
17451752
if pattern.match(var.lower()):
17461753
if verbose:

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ What's New
55

66
v0.7.3 (unreleased)
77
===================
8+
- :py:meth:`Dataset.cf.guess_coord_axis` now skips known axes/coordinates and only returns a single guess per variable.
9+
Additional attributes such as `units` must be added to known axes/coordinates using :py:meth:`Dataset.cf.add_canonical_attributes`.
10+
By `Mattia Almansi`_.
811
- Increased support for ``cf_role`` variables. Added :py:attr:`Dataset.cf.cf_roles` By `Deepak Cherian`_.
912

1013
v0.7.2 (April 5, 2022)

0 commit comments

Comments
 (0)