Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional import of the DatetimeAccessor due to xarray update #1078

Merged
merged 1 commit into from Jul 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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