Skip to content

Commit

Permalink
Adds a conditional import of the DatetimeAccessor due to a change in the
Browse files Browse the repository at this point in the history
backend API of xarray v0.12.2. Fixes #1077.

Updated import method per @jthielen's suggestion
  • Loading branch information
zbruick committed Jul 2, 2019
1 parent d3c2940 commit e01052b
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions metpy/xarray.py
Expand Up @@ -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

Expand Down Expand Up @@ -475,17 +474,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):
Expand Down

0 comments on commit e01052b

Please sign in to comment.