diff --git a/src/metpy/xarray.py b/src/metpy/xarray.py index 41e9dc01a0b..ac0f3a63d7b 100644 --- a/src/metpy/xarray.py +++ b/src/metpy/xarray.py @@ -128,8 +128,8 @@ def __init__(self, data_array): # noqa: D107 @property def units(self): """Return the units of this DataArray as a `pint.Unit`.""" - if isinstance(self._data_array.data, units.Quantity): - return self._data_array.data.units + if isinstance(self._data_array.variable._data, units.Quantity): + return self._data_array.variable._data.units else: return units.parse_units(self._data_array.attrs.get('units', 'dimensionless')) @@ -747,7 +747,7 @@ def parse_cf(self, varname=None, coordinates=None): else: crs = CFProjection(proj_var.attrs) - if crs is None and not check_axis(var, 'latitude', 'longitude'): + if crs is None: # This isn't a lat or lon coordinate itself, so determine if we need to fall back # to creating a latitude_longitude CRS. We do so if there exists valid *at most # 1D* coordinates for latitude and longitude (usually dimension coordinates, but diff --git a/tests/test_xarray.py b/tests/test_xarray.py index b51982b6086..9dd4ed8b3be 100644 --- a/tests/test_xarray.py +++ b/tests/test_xarray.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: BSD-3-Clause """Test the operation of MetPy's XArray accessors.""" from collections import OrderedDict +from unittest.mock import patch, PropertyMock import numpy as np import pyproj @@ -115,6 +116,13 @@ def test_units(test_var): assert test_var.metpy.units == units.kelvin +def test_units_data(test_var): + """Test units property fetching does not touch variable.data.""" + with patch.object(xr.Variable, 'data', new_callable=PropertyMock) as mock_data_property: + test_var.metpy.units + mock_data_property.assert_not_called() + + def test_units_percent(): """Test that '%' is handled as 'percent'.""" test_var_percent = xr.open_dataset(