diff --git a/metpy/xarray.py b/metpy/xarray.py index 138e03feb21..5bef9ff9a72 100644 --- a/metpy/xarray.py +++ b/metpy/xarray.py @@ -10,7 +10,6 @@ import warnings import xarray as xr -from xarray.core.accessors import DatetimeAccessor from xarray.core.indexing import expanded_indexer from xarray.core.utils import either_dict_or_kwargs, is_dict_like @@ -452,17 +451,21 @@ def wrapper(*args, **kwargs): return wrapper -# If DatetimeAccessor does not have a strftime, monkey patch one in -if not hasattr(DatetimeAccessor, 'strftime'): - def strftime(self, date_format): - """Format time as a string.""" - import pandas as pd - values = self._obj.data - values_as_series = pd.Series(values.ravel()) - strs = values_as_series.dt.strftime(date_format) - return strs.values.reshape(values.shape) - - DatetimeAccessor.strftime = strftime +# If DatetimeAccessor does not have a strftime (xarray <0.12.2), monkey patch one in +try: + from xarray.core.accessors import DatetimeAccessor + if not hasattr(DatetimeAccessor, 'strftime'): + def strftime(self, date_format): + """Format time as a string.""" + import pandas as pd + values = self._obj.data + values_as_series = pd.Series(values.ravel()) + strs = values_as_series.dt.strftime(date_format) + return strs.values.reshape(values.shape) + + DatetimeAccessor.strftime = strftime +except ImportError: + pass def _reassign_quantity_indexer(data, indexers):